Lore archive · Warcraft Wiki, 2023 dump
Function arguments
Last wiki revision 2019-06-18
Functions can have arbitrarily defined arguments defined in the opening line and subsequently used within the body of the function. Your parameters can be called any valid Lua name.
Below is a simple function without arguments, followed by another function that calls on the first:
function MyAddon_Mimic DEFAULT_CHAT_FRAME:AddMessage("Hello World!") end function MyAddon_Call MyAddon_Mimic end
Now the same function with arguments (you can define any number of arguments):
function MyAddon_Mimic(arg1) DEFAULT_CHAT_FRAME:AddMessage("Hello " .. arg1) end function MyAddon_Call MyAddon_Mimic("Buddy!") end
Whereas the first example would've simply printed "Hello World!" to the chat window, the second example will now print "Hello Buddy!" However, arg1 is a pretty non-discriptive argument name (and we only used one). Let's try something more interesting:
function MyAddon_Mimic(who, send) local posX, posY = GetPlayerMapPosition(who); if send==1 then DEFAULT_CHAT_FRAME:AddMessage(who .. " is currently at location " .. posX .. "," .. posY) end end function MyAddon_Call MyAddon_Mimic("player", 1) MyAddon_Mimic("party1", 1) MyAddon_Mimic("party2", 0) end
Here, our little addon will now print the location of our player and our first party member, but while it will get the location of party member 2, it won't print it.
With patch 2.0 and the upgrade to lua 5.1, the '...' arg is much more powerful and less memory intensive than it was previously. Here's some handy tips on how to use this new functionality.
Previously, the ... arg would create a table 'args' if called. This allocated a table into memory, and thus became a source of garbage churn. A very common pitfall of this came from hooking: local f = somefunc somefunc = function(...) -- do some crap return f(unpack(args)) end Now if this function is called often, or there are many hooks like this, there will be a significant hit on performance. With lua 5.1 we can do this hook with no memory wasted local f = somefunc somefunc = function(...) -- do some crap return f(...) end
The new functionality also allows for a nice clean post hook to be made. In this example we need the first arg passed to the function, and we wish to maintain a full proper hook. That means every arg is passed to the hooked function, and every return from that is returned back from our function. local f = somefunc local ph = function(a1, ...) -- do something with a1 return ... end somefunc = function(a1, ...) return ph(a1, f(a1, ...)) end
As with hooking, iteration over arbitrary number of args would create a table and thus waste memory. With lua 5.1 we can do this cleanly in a for loop function f(...) for i=1,select("#", ...) do local x = select(i, ...) -- do something with x end end
Clean recursive functions are simple with ... function r(x, ...) local y = x*x if select("#", ...) > 0 then return y, r(...) else return y end end
Source
https://warcraft.wiki.gg/wiki/Function_arguments · retrieved 2023-11-13 · CC BY-SA 4.0
Converted from wikitext with templates, tables and images removed, so some sentences may be missing values.
This archive predates WoW: Forever. For collected gameplay facts see the Forever database. Search the archive.