Difference between global variables, _G.var and var?

Ah, here’s another test. It looks like if I don’t place local in front of a variable in main.lua, it will become global.

main.lua
[lua]local module01 = require (“module01”)
myVar = 0;
print(“calling module01.myFunction() in main.lua while myVar is still 0”)
module01.myFunction() – prints value of myVar inside myFunction = 0

myVar = 1234;
print(“calling module01.myFunction() in main.lua after myVar is set to 1234”)
module01.myFunction() – prints value of myVar inside myFunction = 1234[/lua]

module01.lua
[lua]module(…, package.seeall) – removing package.seeall causes error
function myFunction() – placing local in front of the function causes error

print("value of myVar inside myFunction = " … tostring(myVar))

end[/lua] [import]uid: 67217 topic_id: 17717 reply_id: 67872[/import]

iolo, you really got me following this quest. I had to try one more test, and here’s the result. module02 would recognize the value of myVar set by module01 even when main.lua doesn’t recognize it. So… what is myVar? If it’s global variable, then this must be the difference between super global (_G) and regular global (and regular global appears to be smaller in scope).

Anyhow, here’s the test & result:

main.lua
[lua]print("in main.lua myVar = " … tostring(myVar)) – prints myVar = nil
local module02 = require (“module02”)
module02.myFunction() – prints myVar = 1234
print("in main.lua myVar = " … tostring(myVar)) – prints myVar = nil[/lua]
module01.lua
[lua]module(…, package.seeall)
function myFunction()
myVar = 1234
return myVar
end[/lua]

module02.lua
[lua]module(…, package.seeall)
local module01 = require(“module01”)
function myFunction()
myVar = module01.myFunction()
print("in module02 myVar = " … tostring(myVar))
end[/lua]

Now I’ve got to go back to my game project and chant focus, focus, focus as my mantra. No more meandering (even if I can learn so much from it) until my game is done.

Cheers,
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67974[/import]

Wow, that’s a lot of testing!

You can’t access a local function in a module from another module even if you require it because it is localized only to the module that it’s declared in.

By using this method however (http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/) it lets you localize functions and use it in another module. I have been using this and it works great. I also fixed a memory leak by switching.

At this point, using _G’s makes things a lot easier than trying to use locals when passing data among modules. As long as I nil out the _G’s after use seems to control memory leaks as well. I just gotta be careful not to go overboard with too many _G’s. [import]uid: 39031 topic_id: 17717 reply_id: 67976[/import]

Hey, iolo, when my game is done (meaning, when I have everything implemented and only last thing remaining is the final polishing), I will definitely look into localizing functions described in the link noted in post #22. I’ve read it before, and I couldn’t quickly implement it – it’s probably because I haven’t tried sorting it out properly in smaller test project first.

I’m also still learning how to properly pass parameters. Net result is, I don’t create public/global functions in external modules. My functions are either a public/global function established in main.lua or a local function restricted to each external module. This must be part of the reasons I was able to purge memory leak concerns from my project.

Good to hear you’ve come to terms with _G. anyhow.

Cheers,
Naomi [import]uid: 67217 topic_id: 17717 reply_id: 67988[/import]

Creating Dynamic Variables

I would like to create dynamic local vairables on the fly. Say, create a loop 1 - 10, creating image1, image2, image3,… Is there a way to do this in Corona? [import]uid: 6288 topic_id: 17717 reply_id: 76459[/import]

Use ‘for’ loop and store each variable into a table:

local image = {}  
for i = 1, 10 do  
image[i] = myVar  
end  

You can insert a function into the loop to create different variables. [import]uid: 39031 topic_id: 17717 reply_id: 76540[/import]