I've not heard of LUA before but I just looked over "" I found on google and it doesn't look so different to other languages?
A global variable must be declared outside of any function, if a variable is declared inside a function it is "local" to that function and cannot be used outside of that function.
It looks like a duplicate global variable in LUA will simply overwrite the previously declared variable, so if the game client declares the global variable:
and you later declare:
then you will no doubt find the end of the game displaying "10", so as you already stated, best to use only unique variables. Most IDEs that you write code within will warn you of a conflict before they compile the program but LUA doesn't seem to declare variables in the same way other languages do?
In other languages you have to declare the type of variable, like whether it's an int, double, string, boolean, etc., but it appears all variables in LUA are tables, whether populated with integers, strings, booleans or whatever?
Anyway, to declare your global variable, you would simply have outside of any function:
Code:
MyAddonGlobalSpace = {}
then later, within a function you can populate it with something like:
Code:
function read_controller(button)
if button == A then
MyAddonGlobalSpace = "up"
end
else if button == B then
MyAddonGlobalSpace = "down"
end
end
Then if you later called:
Code:
DEFAULT_CHAT_FRAME:AddMessage(MyAddonGlobalSpace)
The output would depend on which button was pressed within the function.
Or you can simply initialise the variable when you first declare it:
Code:
MyAddonGlobalSpace = {"up","down","left","right"}
Note that in any other language, using "else if" would mean you must initialise the variable when you declare it (since it's possible that neither condition exists), whereas when using just "else" you wouldn't.
Andrew
Bookmarks