Open main menu
Home
Random
Log in
Settings
About AddOn Studio
Disclaimers
AddOn Studio
Search
Editing
WoW:Lua variable scoping
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Complex scope == Yes, nothing's ever that simple, of course. Above, we have only looked at limiting scope one level at a time. Unfortunately, addons often are complex enough that an author will mix several levels of scope. But how does Lua know which local variable to use? Well, as stated before, Lua uses a rule system called "least visibility." That means, the variable which is "most local" to the current scope will be the one used. Global variables are only used in the event that a local variable does not exist at the current scope. The following block shows a very complex example which isolates the various cases that may occur. Note we create the function "p" as a synonym for DEFAULT_CHAT_FRAME:AddMessage because it is a long name to write. More importantly, note that we make this function file-local. Remember, in Lua, functions are variables too. As a result, it is equally possible to make a function local as well. In this case, the function is file-local, meaning that it will be used only within this file, and not be accessible outside of this file. This is often a good idea, especially with very simplistic names such as "p" which other authors may also choose. local function p(msg) DEFAULT_CHAT_FRAME:AddMessage(tostring(msg)); end n = 0; -- Global variable function foo(n) -- In comments, assuming parameter n = 1 p(n); -- Prints 1 local n = n + 1; -- Creates a new n in block scope, equal to n + 1 p(n); -- Prints 2 if (true) then -- Entering a new code block p(n); -- Prints 2 local n = 15; -- Creates a more local n, now p(n); -- Prints 15 for x = 1, 10 do -- Note x is implicitly declared as a local variable now, it is not accessible outside the for block -- Note each iteration is a separate block! p(n); -- Prints 15 EVERY TIME (even on 2nd+ loop) local n = x; p(n); -- Prints 1,2,...,10 (depending on iteration) end p(n); -- Prints 15 (n in for block is out of scope) p(x); -- Prints nil (x is local to the for block) end p(n); -- Prints 2 end foo(1); p(n); -- Prints 0 (using global n, as there is no local n) Now, is it a best-practice to use the same variable name for every variable you get your hands on? Most certainly not, but it does provide some extra functionality that can be useful. In general, you should avoid using names that are the same, especially as much as the above function foo(), where it can become very difficult to determine which version of "n" you are using, but it is useful on occasion to be able to localize variables. Always remember, however, that the most local variable will always be used, where possible. Global variables are a last resort.
Summary:
Please note that all contributions to AddOn Studio are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
AddOn Studio Wiki:Copyrights
for details).
Submissions must be written by you, or copied from a public domain or similar free resource (see
AddOn Studio Wiki:Copyrights
for details).
Cancel
Editing help
(opens in new window)
Close
Loading editor…