This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standard Lua functions, available in most Lua environments. Arguably, we could just refer to the Lua web site, but a few functions differ slightly in Blizzard's implementation. They are all documented here for consistency.

Reference Edit

The World of Warcraft Lua runtime does not provide all standard Lua functions. Notably, the operating system and file I/O libraries are not present. Nearly all of these functions are part of the default Lua 5.1 runtime environment, described here.

Core Edit

assert(value[, errormsg]) - returns 'value' if not false. otherwise throws a Lua error using 'message'.
collectgarbage() - Forces garbage collection. (added 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.
newproxy(boolean or proxy) - Creates a userdata with a sharable metatable.
pcall(func, arg1, arg2, ...) - Returns a true value indicating successful execution of 'func' and return values, otherwise false and the error message.
select(index, list) - Returns the number of items in list or the value of the item in list at index.
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[, start][, end]) - 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 Edit

Most of these functions are shorthand references to the Lua math library (which is available via "math.", seeMathLibraryTutorial 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.

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 natural logarithm (log base e) 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)
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 Edit

These string functions are shorthand references to the Lua string library, which is available via "string.". SeeStringLibraryTutorial 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.
strmatch(string, pattern[, initpos]) - Similar to strfind but only returns the matches, not the string positions.
strrep(seed,count) - Return a string which is count copies of seed.
strrev(string) - Reverses a string; alias of string.reverse.
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.

These are custom string functions available in WoW but not normal Lua.

strlenutf8(string) - Returns the number of characters in a UTF8-encoded string.
strtrim(string[, chars]) - Trim leading and trailing spaces or the characters passed to chars from string.
strsplit(delimiter, string) - Return a list of substrings separated by occurrences of the delimiter.
strjoin(delimiter, string, string[, ...]) - Join string arguments into a single string, separated by delimiter.
strconcat(...) - Returns a concatenation of all number/string arguments passed.
tostringall(...) - Converts all arguments to strings and returns them in the same order that they were passed.

Table Edit

These table functions are shorthand references to the Lua table library (which is available via "table.", seeTableLibraryTutorial for more info).

Be also aware that many table functions are designed to work with tables indexed only with numerical indexes, starting with 1 and without holes (like {[1] = "foo", [3] = "bar"} --- recognize that [2] will be nil in this table). When working with any other tables behavior is not defined and might lead to unexpected results. Not being aware of this fact is one major causes for bugs in code written in Lua.

foreach(table,function) - Execute function for each element in table. (deprecated, used pairs instead)
foreachi(table,function) - Execute function for each element in table, indices are visited in sequential order. (deprecated, used ipairs instead)
getn(table) - Return the size of the table when seen as a list. This is deprecated, it is replaced by the # operator. Instead of table.getn(table), use #(table).
ipairs(table) - Returns an iterator of type integer to traverse a table.
pairs(table) - Returns an iterator to traverse a table.
sort(table[, comp]) - Sort the elements in the table in-place, optionally using a custom comparator.
tContains(table, value) - returns true if value is contained within table. This is not standard Lua, but added by Blizzard.
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)
wipe(table) - Restore the table to its initial value (like tab = {} without the garbage). This is not standard Lua, but added by Blizzard.

Bit Edit

World of Warcraft includes the Lua BitLib library (which is available via "bit."), which provides access to C-style bitwise manipulation operators. It has been available since Patch 1.9. This library uses the WoW binary's native integer size. (e.g. on 32-bit builds, bit.lshift(2147483648, 1) = 0, because 1000 0000 0000 0000 0000 0000 0000 0000 -> 0000 0000 0000 0000 0000 0000 0000 0000)

Note: 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.

bit.bnot(a) - returns the one's complement of a
bit.band(w1,...) - returns the bitwise and of the w's
bit.bor(w1,...) - returns the bitwise or of the w's
bit.bxor(w1,...) - returns the bitwise exclusive or of the w's
bit.lshift(a,b) - returns a shifted left b places
bit.rshift(a,b) - returns a shifted logically right b places
bit.arshift(a,b) - returns a shifted arithmetically right b places
bit.mod(a,b) - returns the integer remainder of a divided by b

Coroutine Edit

Coroutine functions should be used sparingly because of the amount of memory they use.

coroutine.create(f)
coroutine.resume(co [, val1, ...])
coroutine.running()
coroutine.status(co)
coroutine.wrap(f)
coroutine.yield(...)

Notes Edit

Some WoW specific Lua notes.

String Functions Edit

  • All strings have their metatable set to index the global string table, so any string function may be called through it with the colon syntax:
  1. -- This...
  2. local s = string.format(input, arg1, arg2, ...)
  3.  
  4. -- ...can be written as this
  5. local s = input:format(arg1, arg2, ...) -- input gets passed as the first argument, replicating the above code, as per the colon syntax

To make this work for string constants, you have to use parentheses. "%d":format(arg1) is not valid Lua code, you have to do

  1. ("%d"):format(arg1)

Since this uses the string table, any function placed inside the string table works the same. The following is valid:

  1. function string.print(a)
  2. return print(a)
  3. end
  4. ("test"):print()

Though you should try to avoid populating the string table with your own functions.

Table Functions Edit

  • Any function that takes a table as its first argument can be assigned to a method in said table and used thusly.

    • There's no practical reason for doing this, but it's kinda fun to know these useless things.
    • Notice that table.wipe (and wipe) will remove itself and all other methods assigned in this manner.
  1. tab = {}
  2.  
  3. -- this works, of course.
  4. tinsert(tab, 1, value) -- change the value at index 1 to value.
  5.  
  6. -- as does this
  7. tab.insert = tinsert
  8. tab:insert(1, value) -- change the value at index 1 to value (note the ":").

See also Edit

  • Lua, which has plenty of links to other reference material.
  • API types, with a list of Lua types and 'psudo-types' used in WoW.

lua function的更多相关文章

  1. Lua function 函数

    Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...

  2. call lua function from c and called back to c

    Just a simple example: --The  c file: #include <stdio.h> #include "lua.h" #include & ...

  3. First Lua function running in C

    这是我在C里面跑出来的第一个Lua 文件, 纪念一下. 1.Set up envirnonment: Mac下面 Lua的src (即include) 和lib(binary)是分开的, 所以需要分别 ...

  4. lua执行字节码的过程介绍

    前面一篇文章中介绍了lua给下面代码生成最终的字节码的整个过程,这次我们来看看lua vm执行这些字节码的过程. foo = "bar" local a, b = "a& ...

  5. lua解释执行脚本流程

    #include "lua.hpp" #include <iostream> using namespace std; #pragma comment(lib, &qu ...

  6. Lua热更系统

    1.介绍 使用脚本开发游戏业务逻辑其中一个好处就是代码可线上热更,不停机修复bug.而热更代码的写法与需要被热更的文件的代码又有着密切的关系,本文介绍一种热更方法. 2.热更原理 Lua提供一个叫re ...

  7. 《The Evolution of Lua》读书笔记 1

    lua的优点: 可移植性 容易嵌入 体积小 高效率 这些优点都来自于lua的设计目标:简洁.从Scheme获得了很多灵感,包括匿名函数,合理的语义域概念   lua前身: 巴西被禁运,引入计算机软件和 ...

  8. 用VC编译lua源码,生成lua语言的解释器和编译器

    用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...

  9. Lua与C的交互

    Lua 与 C 的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1.  ...

随机推荐

  1. hdu 4433

    一道dp题,虽然知道是dp,但是不会做: 学习了ACM_cxlove大神的代码,终于明白了: 搬运工: dp[i][j][k]表示 前i个已经完全匹配,而这时候,第i+1个已经加了j位,第i+2位已经 ...

  2. [收藏转贴]struct探索·extern "C"含义探索 ·C++与C的混合编程·C 语言高效编程的几招

    一.C/C++语言 struct深层探索 1.自然对界 struct是一种复合数据类型,其构成元素既可以是基本数据类型(如 int.long.float等)的变量,也可以是一些复合数据类型(如 arr ...

  3. C# 读取网络txt文件 并写在本地txt文件中

    public void WriteStream() { try { stirng ImagesPath = ImagesPath + "\\2013-09-27.txt"; Htt ...

  4. JavaScript字符串的操作

    <script> var s=new String(); var s="Hello World"; alert(s.toLowerCase(s));//转小写 aler ...

  5. 【Android 复习】:第02期:引导界面(二)使用ViewPager实现欢迎引导页面

    一.实现的效果图 也许是养成了这样一个习惯,每次看别人的代码前,必须要先看实现的效果图达到了一个什么样的效果,是不是跟自己想要实现的效果类似,有图才有真相嘛,呵呵.           二.编码前的准 ...

  6. asp.net 前后台交互

    转自:http://www.cnblogs.com/softpower/archive/2010/11/16/1878769.html 1.如何在JavaScript访问C#函数? 2.如何在Java ...

  7. 设置Div多行文本超出时,以省略号代替

    这个文章不错 http://www.css88.com/archives/5206 css中有一个属性: text-overflow,可以设置文本超出指定长度后的文本截取样式. 下面是从 w3shco ...

  8. 《C语言程序设计现代方法》第2章 编程题

    7 编写一个程序,要求用户输入一个美金数量,然后显示出如何使用最少的20美元.10美元.5美元和1美元来付款. 提示:将付款金额除以20,确定20美元的数量,然后从付款金额中减去20美元的总金额.对其 ...

  9. win7常用键

    (1)xp和win7中都可以使用Alt+Tab中进行标签切换,win7中添加了Wins+Tab可以进行3D标签切换. (2)你知道怎样一次过调整显示器亮度.音量大小,打开无线网,还能够看到本本电池电量 ...

  10. nyoj 经典的连续字串和

    import java.util.Scanner; public class 字串和 { public static void main(String[] args) { // TODO Auto-g ...