I searched but couldn’t find an answer.
I thought declaring without local would make a variable global but I noticed that unless I use _G. I can’t access the variables from another module. [import]uid: 39031 topic_id: 17717 reply_id: 317717[/import]
_G.variables are commonly used by lua. [import]uid: 79135 topic_id: 17717 reply_id: 67539[/import]
A variable without _G is also considered a global variable but I couldn’t access it across two different files.
[import]uid: 39031 topic_id: 17717 reply_id: 67541[/import]
I’m wondering if this global variable is referred to as local elsewhere, which maybe causing one of your modules not to recognize it as global? You might want to check all your modules and search & check how this global variable is referenced/used in each module.
EDIT: Another thing I might try is to change the name of the global variable to something ridiculously unique, and see if it solves the problem, and if it does, then you know that the variable is used somewhere else (which you may not be fully aware of), causing the conflict.
Naomi
[import]uid: 67217 topic_id: 17717 reply_id: 67546[/import]
I use _G.variable to access music on and off from my option.lua worked fine but I would try to use local variable as much as possible to keep a clean app. But I think regarding your question the best answer comes from iolo72 [import]uid: 30314 topic_id: 17717 reply_id: 67564[/import]
^
iolo72 actually asked the question
[import]uid: 31262 topic_id: 17717 reply_id: 67569[/import]
@LeivaGames
I was the one who asked the question 
@Naomi
I will check for references.
To clarify the question, when I do this in module01.lua
myVar = 1234
I can’t access myVar from main.lua and will get an object-is-nil error.
main.lua
require("module01")
print(myVar)
But, using _G.myVar instead of myVar in module01.lua will not cause the error. [import]uid: 39031 topic_id: 17717 reply_id: 67572[/import]
Silly me I read your post wrong I thought you said “A variable without _G is also considered a global variable but cant be access across two different files” So is why I said you had the best answer lol. This being said I think thats the way ill put it I havent recently tested on my new app but on my 1st app my global variable was not working across file so I _G and it worked like a charm. [import]uid: 30314 topic_id: 17717 reply_id: 67573[/import]
I am having the same experience but even the Corona reference says otherwise.
http://developer.anscamobile.com/content/introduction#Global
Maybe I am misunderstanding the scope of “global”??? [import]uid: 39031 topic_id: 17717 reply_id: 67575[/import]
Hey iolo72, the problem might be the fact you are trying to get myVar from module01. I think main.lua is the very first file that gets processed (but then, I don’t really know how & when required modules are processed, so I could be totally off.)
Anyhow, maybe the fact you state myVar = 1234 in module01 while trying to retrieve this value in main.lua might be the problem. If you swap this around (i.e., stating myVar = 1234 in main.lua and try retrieving the value in module01), I imagine it would work.
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67584[/import]
_G. could maybe be called a “super” global
php.net/manual/en/language.variables.superglobals.php
To access a global in another module, you must use “otherModule.globalVar”
To access the _G.Global you can call it using “_G.Global”
So in order of global-ness:
_G.Var
Var
Local Var [import]uid: 79135 topic_id: 17717 reply_id: 67583[/import]
Hey anddrewscott, that’s interesting. Thanks for the link. I learn new thing every day.
I’ve been using global variable without using _G since I began my project. It’s been working perfectly fine. Only difference with my case and iolo’s is that I always define my global variable in main.lua, and have this global variable accessed from other module. No problem there.
That said, I finally decided to use _G (because I feel I understand it better now, and I feel comfortable using it). It was a very simple conversion process, because all I had was a myGlobalVariable table. All I had to do was replace myGlobalVariable. with _G. The net result for me was shortening the name of the variable. Heh.
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67586[/import]
By the way, when I want to use global variable from other module, I localize it first, like so:
[lua]-- main.lua
_G.fontName1 = “myCustomFontHere”
–other module that uses the custom font
local fontName1 = _G.fontName1
– After that, I simply use fontName1 (without _G. in front of it.)[/lua]
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67587[/import]
@anddrewscott
Thanks for the explanation.
@Naomi
Yeah, that’s what I’ve been doing. I really wish I don’t have to use global variables but it’s proving to be very difficult!
[import]uid: 39031 topic_id: 17717 reply_id: 67639[/import]
Hey, iolo, when I was cleaning after my lunch just now, a thought popped in to my head about this topic. Maybe you could try placing print statements inside module01 right after myVar is given the value of 1234, and also in main.lua like so:
[lua]-- in module01.lua
myVar = 1234
print("in module01, myVar is now given value of " … myVar)
– in main.lua
print (“I’m about to require module01 in main.lua”)
require(“module01”)
print (“module01 is already required at this point in main.lua”)
print("main.lua says the value of myVar is " … myVar)
– at the very end of main.lua, add this too
print(“the end of main.lua is reached”)[/lua]
Run the project and see the print statements on terminal. I’d imagine this could tell you if main.lua should’ve already known the value of myVar. Try this one with _G too. Comparing the difference would probably tell you a lot about the difference between _G based global variable vs “regular” global variable. (I haven’t tried this, but I thought you might want to give it a shot in your quest.)
Also, one more thing. main.lua may want myVar declared before module01 is required in (even if you don’t give myVar any value when you declare it in main.lua). It’s just that, _G might automatically be forward declared while non _G based global variables aren’t (and perhaps that’s the only difference between _G based global variable and other ones – who knows.)
Cheers,
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67813[/import]
Thanks, Naomi. I understand that _G variable works regardless of where it was declared or used, without having to use require(“module”), which is a lazy way to go about when using global vars.
I just looked at what I wrote and the example I gave in the reply (#6) is incorrect (sorry, maybe I was high…)
What I meant to say was this:
module01.lua
function myFunction()
myVar = 1234
print("myVar")
end
main.lua
-- this will run myFunction and print the value of myVar
local module01 = module01.myFunction()
-- this will cause an error
print("this value is nil" .. myVar)
If myVar is switched with _G.myVar, everything prints. [import]uid: 39031 topic_id: 17717 reply_id: 67820[/import]
Hey iolo, that’s interesting. Have you tried this?
module01.lua
[lua]function myFunction()
myVar = 1234
end[/lua]
main.lua
[lua]-- declare myVar as global variable
myVar;
local module01 = module01.myFunction()
print("this is the value of myVar right now: " … myVar)[/lua] [import]uid: 67217 topic_id: 17717 reply_id: 67823[/import]
myVar; throws a syntax error.
This works though.
module01.lua
module(..., package.seeall)
function myFunction()
myVar = 1234
end
main.lua
local module01 = require("module01")
local myFunction = module01.myFunction()
-- explicitly tell which module this global var belongs to
print("this is the value of myVar right now: " .. module01.myVar)
[import]uid: 39031 topic_id: 17717 reply_id: 67841[/import]
By the way, returning back to the original question – I’m no longer sure if not having “local” in front of a variable makes it global. Not that I will stop placing “local” in front of variable, but it sure is confusing. This test project makes it look like myVar does not become global when it is used inside a function.
I also tried using _G. in front of myVar, and I got an interesting result:
main.lua
[lua]local module01 = require (“module01”)
print("myVar after module01 is required: " … tostring(_G.myVar)) – prints nil
module01.myFunction()
print("myVar after module01.myFunction() is called: " … tostring(_G.myVar)) – prints 1234
myVar = module01.myFunction()
print("myVar after myVar = module01.myFunction() is called: " … tostring(_G.myVar)) – prints 1234[/lua]
module01.lua
[lua]module(…, package.seeall) – it must include package.seeall for this to work
function myFunction()
_G.myVar = 1234
return myVar
end[/lua]
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67866[/import]
Hey, iolo, you got me curious, and here’s what I tried, and this one works too.
main.lua
[lua]local module01 = require (“module01”)
print("myVar after module01 is required: " … tostring(myVar)) – prints nil
module01.myFunction()
print("myVar after module01.myFunction() is called: " … tostring(myVar)) – prints nil
myVar = module01.myFunction()
print("myVar after myVar = module01.myFunction() is called: " … tostring(myVar)) – prints 1234[/lua]
module01.lua
[lua]module(…)
function myFunction()
myVar = 1234
return myVar
end[/lua]
By looking at the print statement, I can see how simply calling the function won’t set the value of myVar. Also, adding local in front of function myFunction() in module01 will also break the code.
Incidentally, this one works too for module01.lua :
[lua]module(…)
function myFunction()
local myVar = 1234
return myVar
end[/lua]
For the time being, myFunction will need to remain global for main.lua to recognize it.
The thing is, I’ve read this blog post: http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
I’ve also read this post: http://lua-users.org/wiki/LuaModuleFunctionCritiqued
But I still haven’t worked out how I may make the local functions in an external module to be recognized by another module. I know it’s my homework looming over my head… but I sure would like to see what main.lua and module01.lua would look like when myFunction is a local function in module01. (If anyone is reading this and can easily demonstrate how it may be achieved, I sure would love to see it.)
Naomi
[import]uid: 67217 topic_id: 17717 reply_id: 67862[/import]