49
edits
mNo edit summary |
No edit summary |
||
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOWYSIWYG__{{wowlua}} | __NOWYSIWYG__{{wowlua}} | ||
This is the main reference for | This is the main reference for [[Lua]] in [[w:c:World of Warcraft|World of Warcraft]]. Note: these are mostly standard Lua functions available in most Lua environments. Can also just refer to the [http://www.lua.org Lua.org] web site for Lua 5.1, but a few functions differ in Blizzard's implementation. All are all documented here for consistency, and to allow for commentary here. | ||
== Reference == | == Reference == | ||
The [[w:c:World of Warcraft|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 [http://www.lua.org/manual/5.1/manual.html here]. | The [[w:c:World of Warcraft|World of Warcraft]] Lua runtime does not provide all of the 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 [http://www.lua.org/manual/5.1/manual.html here]. | ||
=== Core === | === Core === | ||
| Line 124: | Line 124: | ||
=== String Functions === | === String Functions === | ||
* 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: | * 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: | ||
<pre> | |||
-- This... | |||
local s = string.format(input, arg1, arg2, ...) | |||
</pre> | |||
<pre> | |||
-- ...can be written as this | |||
local s = input:format(arg1, arg2, ...) -- input gets passed as the first argument, replicating the above code, as per the colon syntax | |||
</pre> | |||
To make this work for string constants, you have to use parentheses. "%d":format(arg1) is not valid Lua code, you have to do | To make this work for string constants, you have to use parentheses. "%d":format(arg1) is not valid Lua code, you have to do | ||
<pre> | |||
("%d"):format(arg1) | |||
</pre> | |||
Since this uses the string table, any function placed inside the string table works the same. The following is valid: | Since this uses the string table, any function placed inside the string table works the same. The following is valid: | ||
<pre> | |||
function string.print(a) | |||
return print(a) | |||
end | |||
("test"):print() | |||
</pre> | |||
Though you should try to avoid populating the string table with your own functions. | Though you should try to avoid populating the string table with your own functions. | ||
| Line 143: | Line 153: | ||
** There's no practical reason for doing this, but it's kinda fun to know these useless things. | ** There's no practical reason for doing this, but it's kinda fun to know these useless things. | ||
** '''Notice''' that [[API wipe|table.wipe]] (and [[API wipe|wipe]]) will remove itself and all other methods assigned in this manner. | ** '''Notice''' that [[API wipe|table.wipe]] (and [[API wipe|wipe]]) will remove itself and all other methods assigned in this manner. | ||
<pre> | |||
tab = {} | |||
-- this works, of course. | |||
tinsert(tab, 1, value) -- change the value at index 1 to value. | |||
-- as does this | |||
tab.insert = tinsert | |||
tab:insert(1, value) -- change the value at index 1 to value (note the ":"). | |||
</pre> | |||
== See also == | == See also == | ||