Lets say I have menu.lua containing:
myName = "Tom';
I then use Director to go to screen 1 and want:
print(myName);
This will show nil. I can however use:
_G.myName = "Tom";
to set the variable in menu.
But why doesn't the first example work. I thought everything not local in lua went into _G?
In short, if I set: myName = "Tom'; on a file, why isn't it accessible forever by all other pages?
Thanks
Tom
a global variable declared inside a file is global local to that file... so if that variable is not declared in that file you should use _G to get the real global global. :)
is it confusing ?
this sample will give you a better idea
main.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
| local file2 =require("file2")
--global variable a
a = 10
file2:fn1()
--prints 10 -- we din't set _G.a in fn1
print(a)
file2:fn2()
--prints 25-- we set _G.a in fn2
print(a) |
Thanks for the help on this. I understand a bit more about how scope between files works now. Cheers!
Tom