demoted headings
(demoted headings) |
|||
| Line 3: | Line 3: | ||
With the low barrier to entry, writing mods has become very popular. While this has greatly benefited the WoW community, it does have a dark side. There are idiosyncrasies in both the [[Lua|Lua language]] and the [[World of Warcraft API]] that many authors overlook. This can lead to poorly written programs as measured by performance, memory usage, interference with other addons and the default UI, etc. The techniques gathering on this page will help help addon authors, experienced and otherwise, make the most out of WoW's environment safely and efficiently. | With the low barrier to entry, writing mods has become very popular. While this has greatly benefited the WoW community, it does have a dark side. There are idiosyncrasies in both the [[Lua|Lua language]] and the [[World of Warcraft API]] that many authors overlook. This can lead to poorly written programs as measured by performance, memory usage, interference with other addons and the default UI, etc. The techniques gathering on this page will help help addon authors, experienced and otherwise, make the most out of WoW's environment safely and efficiently. | ||
= General Scripting = | == General Scripting == | ||
These tips relate to Lua scripting in general. They are offered to help you write safer and more efficient code, and are applicable to non-WoW scripts as well. | These tips relate to Lua scripting in general. They are offered to help you write safer and more efficient code, and are applicable to non-WoW scripts as well. | ||
== Use local variables == | === Use local variables === | ||
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. | ||
| Line 23: | Line 23: | ||
These problems are easily solved by declaring functions and variables local. There are other approaches as well, but this is by far the simplest. | These problems are easily solved by declaring functions and variables local. There are other approaches as well, but this is by far the simplest. | ||
== Hook functions safely == | === Hook functions safely === | ||
See also: [[HOWTO:_Hook_functions_in_a_safer_way]] | See also: [[HOWTO:_Hook_functions_in_a_safer_way]] | ||
| Line 49: | Line 49: | ||
return unpack(ret) | return unpack(ret) | ||
== Make efficient use of conditionals == | === Make efficient use of conditionals === | ||
If you have a series of if conditionals, test the most efficient ones first. For example: | If you have a series of if conditionals, test the most efficient ones first. For example: | ||
| Line 71: | Line 71: | ||
end | end | ||
=== Shortcut evaluation === | ==== Shortcut evaluation ==== | ||
Lua uses shortcut evaluation of conditionals. This means it only evaluates enough of the condition to know for certain whether it's true or false. In the case of or, the whole condition is known to be true as soon as one operand is true. For and, the whole condition is known to be false as soon as one operand is false. You can take advantage of this to add a bit of efficiency: | Lua uses shortcut evaluation of conditionals. This means it only evaluates enough of the condition to know for certain whether it's true or false. In the case of or, the whole condition is known to be true as soon as one operand is true. For and, the whole condition is known to be false as soon as one operand is false. You can take advantage of this to add a bit of efficiency: | ||
| Line 83: | Line 83: | ||
end | end | ||
= API & XML = | == API & XML == | ||
This section applies specifically to the World of Warcraft UI environment. | This section applies specifically to the World of Warcraft UI environment. | ||
== Use local event handler parameters == | === Use local event handler parameters === | ||
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: | 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 ==== | ||
Old | Old | ||
<OnEvent> | <OnEvent> | ||
| Line 110: | Line 110: | ||
</OnEvent> | </OnEvent> | ||
=== XML calling Lua function === | ==== XML calling Lua function ==== | ||
Old XML | Old XML | ||
<OnEvent> | <OnEvent> | ||
| Line 139: | Line 139: | ||
end | end | ||
=== Lua only === | ==== Lua only ==== | ||
Old | Old | ||