Typo fixing using AWB
m (catfix) |
(Typo fixing using AWB) |
||
| Line 11: | Line 11: | ||
One stumbling block for Lua programmers coming from other languages is the fact that all variables are global unless specified otherwise. Many times, the use of global variables is necessary: saved variables are created via the global environment; all API functions and UI frames are in the global namespace. However, globals come at a cost of both performance, and naming conflicts. | One stumbling block for Lua programmers coming from other languages is the fact that all variables are global unless specified otherwise. Many times, the use of global variables is necessary: saved variables are created via the global environment; all API functions and UI frames are in the global namespace. However, globals come at a cost of both performance, and naming conflicts. | ||
Globals in Lua are actually fields in a table called _G. In order to access a global variable, Lua internally translates the variable name to _G["variable name"]. Local variables, on the other hand, are stored on a stack. Stack access is always faster than indexing a table. In code where performance is important (for instance an [[ | Globals in Lua are actually fields in a table called _G. In order to access a global variable, Lua internally translates the variable name to _G["variable name"]. Local variables, on the other hand, are stored on a stack. Stack access is always faster than indexing a table. In code where performance is important (for instance an [[UIHANDLER OnUpdate|OnUpdate handler]]), it's often beneficial to create a local alias to a global function: | ||
local Foo = Foo | local Foo = Foo | ||
| Line 25: | Line 25: | ||
=== Hook functions safely === | === Hook functions safely === | ||
See also: [[HOWTO: | See also: [[HOWTO: Hook functions in a safer way]] | ||
You should do your best to write your addon in such a way that you don't need to hook functions. Even when it is necessary, most of the time [[ | You should do your best to write your addon in such a way that you don't need to hook functions. Even when it is necessary, most of the time [[API hooksecurefunc|hooksecurefunc]] is sufficient. However, there are occasional cases where "traditional" hooking must be used. When you do, remember to pass all parameters and return all results. Example: | ||
local OrigFunc = Func | local OrigFunc = Func | ||
| Line 133: | Line 133: | ||
=== Use local event handler parameters === | === Use local event handler parameters === | ||
With the introduction of WoW 2.0, [[ | With the introduction of WoW 2.0, [[Widget Handlers|widget event handlers]] now provide local parameters. As mentioned in [[#Use_local_variables|a previous section]], accessing local values is more efficient than accessing globals. Here are a few examples of the old way and how they should be implemented now: | ||
==== Code directly in XML ==== | ==== Code directly in XML ==== | ||