WoW:Lua variable scoping: Difference between revisions

add breadcrumb
(New, complete guide on Lua scoping)
 
(add breadcrumb)
Line 1: Line 1:
{{tocright}}
{{tocright}}
{{breadcrumb2|Interface Customization|Lua}}
[[Addons]] in World of Warcraft share a single [[execution environment]]. As a result, all addons have access to the same global variables. If one addon creates a global variable named "foo," then all other addons can use (or overwrite) that same variable. This allows for some really useful things to happen; the most frequently used tactic that takes advantage of this feature is [[HOWTO:_Hook_a_Function|hooking]]. Unfortunately, this can also cause some problems in addons. They will conflict if they both try to use the same name for a variable. As a result, addon authors must ensure that their addons don't conflict by doing one (or more) of the following:
[[Addons]] in World of Warcraft share a single [[execution environment]]. As a result, all addons have access to the same global variables. If one addon creates a global variable named "foo," then all other addons can use (or overwrite) that same variable. This allows for some really useful things to happen; the most frequently used tactic that takes advantage of this feature is [[HOWTO:_Hook_a_Function|hooking]]. Unfortunately, this can also cause some problems in addons. They will conflict if they both try to use the same name for a variable. As a result, addon authors must ensure that their addons don't conflict by doing one (or more) of the following:
#Using assuredly-unique names for variables, such as MyAddon_MyFrame_OnLoad
#Using assuredly-unique names for variables, such as MyAddon_MyFrame_OnLoad
Line 6: Line 8:


== What is Scope? ==
== What is Scope? ==
No, it's not just a mouthwash. A variable's ''scope'' defines how visible it is to the rest of the program. The feature exists in most high-level languages, and Lua is no exception.
No, it's not just a mouthwash. A variable's ''scope'' defines how visible it is to the rest of the program. The feature exists in most high-level languages, and [[Lua]] is no exception.


Variables can exist in two forms: "global," or "local." A global variable is accessible to everything and is not limited. This is the default functionality in Lua. This is unlike many other languages, such as PHP, C, and BASIC, where a variable is always local unless declared global. This difference is important to note. A global variable is not limited to its file, function, or code block:
Variables can exist in two forms: "global," or "local." A global variable is accessible to everything and is not limited. This is the default functionality in Lua. This is unlike many other languages, such as PHP, C, and BASIC, where a variable is always local unless declared global. This difference is important to note. A global variable is not limited to its file, function, or code block:
Line 190: Line 192:
= Conclusion =
= Conclusion =
Understanding Lua's use of variable scope is vital to understanding the process in which Lua decides which variable to use. Correct use of limited scope will allow your addon to be less likely to conflict with others, and minimize global namespace pollution.
Understanding Lua's use of variable scope is vital to understanding the process in which Lua decides which variable to use. Correct use of limited scope will allow your addon to be less likely to conflict with others, and minimize global namespace pollution.
[[Category:Interface Customization]]
[[Category:Lua functions| Lua Scope]]
Anonymous user