WoW:Lua functions: Difference between revisions

no edit summary
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 the UI Lua runtime in World of Warcraft. Note these are mostly standard Lua functions available in most Lua environments. Arguably could just refer to the [http://www.lua.org Lua.org] web site, but a few functions differ in Blizzard's implementation. They are all documented here for consistency and to allow for commentary.
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:
-- This...
<pre>
local s = string.format(input, arg1, arg2, ...)
-- This...
local s = string.format(input, arg1, arg2, ...)
-- ...can be written as this
</pre>
local s = input:format(arg1, arg2, ...)  -- input gets passed as the first argument, replicating the above code, as per the colon syntax
 
<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
("%d"):format(arg1)
<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:
function string.print(a)
<pre>
  return print(a)
function string.print(a)
end
    return print(a)
("test"):print()
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.
tab = {}
<pre>
tab = {}
-- this works, of course.
 
tinsert(tab, 1, value) -- change the value at index 1 to value.
-- this works, of course.
tinsert(tab, 1, value) -- change the value at index 1 to value.
-- as does this
 
tab.insert = tinsert
-- as does this
tab:insert(1, value) -- change the value at index 1 to value (note the ":").
tab.insert = tinsert
tab:insert(1, value) -- change the value at index 1 to value (note the ":").
</pre>


== See also ==
== See also ==