Lua functions
More actions
These are standard Lua language functions available in most Lua parsers. Arguably, we could just refer to the Lua web site, but a few function slightly differently in Blizzard's implementation. They are all documented here for consistency.
Lua in the World of Warcraft API
Note that the World of Warcraft API does not implement all standard Lua libraries. Notably, operating system and file I/O libraries are not present.
Lua Language Primitives
These are part of the core Lua programming language, described here.
- for var=start,end,step - Loop var from start to end, skipping step each time (default 1)
- for var in array - Loop contents of array, assigning values to var
- for idx,val in array - Loop contents of array, assigning indicies and values to idx and val, respectively
- (Plus "if", "else", etc. Not all are documented here, obviously.)
Lua Functions
These functions are part of the LUA programming language, described here.
- assert(value) - asserts a value is non-nil, otherwise shows dialog and outputs debug diagnostic information if you have a debugger attached to wow.exe
- collectgarbage() - Forces garbage collection. (Added in 1.10.1)
- date(format, time) - Returns the current date according to the user's machine.
- error("error message",level) - Throws an error with the given error message. Use pcall() (see below) to catch errors.
- gcinfo() - Returns the number of kB of add-on memory in use and the current garbage collection threshold (in kB).
- getfenv(function or integer) - Returns the table representing the stack frame of the given function or stack level.
- getmetatable(obj, mtable) - Returns the metatable of the given table or userdata object.
- loadstring("Lua code") - Parse the string as Lua code and return it as a function reference.
- next(table, index) - Returns the next key, value pair of the table, allowing you to walk over the table.
- pcall(func, arg1, arg2, ...) - Returns a boolean value indicating successful execution of func and the error message or func's results as additional values.
- setfenv(function or integer, table) - Sets the table representing the stack frame of the given function or stack level.
- setmetatable(obj, mtable) - Sets the metatable of the given table or userdata object.
- time(table) - Returns time in seconds since epoch (00:00:00 Jan 1 1970)
- type(var) - Returns the type of variable as a string, "number", "string", "table", "function" or "userdata".
- unpack(table) - Returns the contents of its argument as separate values.
- xpcall(func, err) - Returns a boolean indicating successful execution of func and calls err on failure, additionally returning func's or err's results.
Math Functions
Most of these functions are shorthand references to the LUA math library (which is available via "math.", see MathLibraryTutorial for more info).
The trigonometry functions are not just references; they have degree→radian conversion wrappers. Blizzard's versions work with degrees. Lua's standard math library works with radians.
- __pow(x, y) - returns x to the power of y
- abs(value) - Returns the absolute value of the number.
- acos(value) - Returns the arc cosine of the value in degrees.
- asin(value) - Returns the arc sine of the value in degrees.
- atan(value) - Returns the arc tangent of the value in degrees.
- atan2(y, x) - Returns the arc tangent of Y/X in degrees.
- ceil(value) - Returns the ceiling of value.
- cos(degrees) - Returns the cosine of the degree value.
- deg(radians) - Returns the degree equivalent of the radian value.
- exp(value) - Returns the exponent of value.
- floor(value) - Returns the floor of value.
- frexp(num) - Extract mantissa and exponent from a floating point number.
- ldexp(value, exponent) - Load exponent of a floating point number.
- log(value) - Returns the logarithm of value.
- log10(value) - Returns the base-10 logarithm of value.
- max(value[, values...]) - Returns the numeric maximum of the input values.
- min(value[,values...]) - Returns the numeric minimum of the input values.
- mod(value,modulus) - Returns floating point modulus of value.
- rad(degrees) - Returns the radian equivalent of the degree value.
- random([ [lower,] upper]) - Returns a random number (optionally bounded integer value)
- randomseed(seed) - Seed the pseudo random number generator
- sin(degrees) - Returns the sine of the degree value.
- sqrt(value) - Return the square root of value.
- tan(degrees) - Returns the tangent of the degree value.
String Functions
These string functions are shorthand references to the LUA string library (which is available via "string.", see StringLibraryTutorial for more info),
- format(formatstring[, value[, ...]]) - Return a formatted string using values passed in.
- gsub(string,pattern,replacement[, limitCount]) - Globally substitute pattern for replacement in string.
- strbyte(string[, index]) - Returns the internal numeric code of the i-th character of string
- strchar(asciiCode[, ...]) - Returns a string with length equal to number of arguments, with each character assigned the internal code for that argument.
- strfind(string, pattern[, initpos[, plain]]) - Look for match of pattern in string, optionally from specific location or using plain substring.
- strlen(string) - Return length of the string.
- strlower(string) - Return string with all upper case changed to lower case.
- strrep(seed,count) - Return a string which is count copies of seed.
- strsub(string, index[, endIndex]) - Return a substring of string starting at index
- strupper(string) - Return string with all lower case changed to upper case.
- tonumber(arg[, base]) - Return a number if arg can be converted to number. Optional argument specifies the base to interpret the numeral. Bases other than 10 accept only unsigned integers.
- tostring(arg) - Convert arg to a string.
Table Functions
These table functions are shorthand references to the LUA table library (which is available via "table.", see TableLibraryTutorial for more info),
- foreach(table,function) - Execute function for each element in table.
- foreachi(table,function) - Execute function for each element in table, indices are visited in sequential order.
- getn(table) - Return the size of the table when seen as a list.
- ipairs(table) - Returns an iterator of type integer to traverse a table.
- pairs(table) - Returns an iterator to traverse a table.
- setn(table, n) - Sets the size of the table to n.
- sort(table[, comp]) - Sort the elements in the table in-place, optionally using a custom comparator.
- tinsert(table[, pos], value) - Insert value into the table at position pos (defaults to end of table)
- tremove(table[, pos]) - Remove and return the table element at position pos (defaults to last entry in table)
Bit Functions
These Bit functions are shorthand references to the LUA BitLib library (which is available via "bit."). They provide access to C-style bitwise manipulation operators. The BitLib-library is not part of the Lua standard library, but was implemented in WoW with patch 1.9
- bnot(a) - returns the one's complement of a
- band(w1,...) - returns the bitwise and of the w's
- bor(w1,...) - returns the bitwise or of the w's
- bxor(w1,...) - returns the bitwise exclusive or of the w's
- lshift(a,b) - returns a shifted left b places
- rshift(a,b) - returns a shifted logically right b places
- arshift(a,b) - returns a shifted arithmetically right b places
- mod(a,b) - returns the integer remainder of a divided by b
Since LUA is a scripting language, using these functions to "compress" your data structures is fairly slow work. Unless you have a very large database and need to conserve RAM usage, save your information in several, individual variables.
Also See
- Our page on Lua. It has plenty of links to Lua reference material.