Originally Posted by seyan777
The question is, can you change the value of outer variable even if you declare a new local variable with an outer one's name within a function?
|
Not easily, if at all. It is easier and recommended to have different names for variables because of this reason. For simplicity's sake, I will rename the variables and add comments.
Lua Code:
local outVar = 1 -- this variable is global scope to the entire file
local addonName, pvtTable = ... -- both of these are local to ALL Lua files within the same addon
local function TestFunction() -- this function is global scope to the entire file
-- assign a local variable that has the scope of just this function,
-- and give it the value of the file's version. Naming is irrelevant
local outVar = outVar -- value == 1
outVar = 2 -- we are still talking about the local variable, == 2
-- the global outVar is still == 1 and we haven't overwritten it
end
local TestFunction2() -- this is what you want
local innerVar = outVar -- innerVar == 1
outVar = 2 -- outVar == 2, no really this time!
end
function TestFunction3() -- this function is global across the entirety of WoW (careful!)
-- global names are 99.99% bad
-- you have no idea who else named a function or variable exactly the same
-- which will have conflicts and crashes and unexpected results
end