Help with Director and Global variables when changing scenes

Hello all,

I’ve been trying to figure this one out and can’t seem to get it. I just want to change the value of a global variable “a” from scene to scene using director by reloading a file. I check the forum don’t seem to get this concept very well. Can some one please give me an example on how to do this? Thank you. Any help will be appreciated. Here’s an example.


level1.lua

module(…, package.seeall)

a=1 —define global variable a in level1.lua

–Director change scene to level2.lua.


Level2. lua

module(…, package.seeall)

local btn1=display.NewImage(…)

–director change scene back to level1.lua when btn1 is touch and change the global variable “a” in level1.lua from a=1 to a=2

–in level2.lua I am basically reloading level1.lua when the button is touch and change the variable a=1 in the file level1.lua to a=2


Level3. lua

module(…, package.seeall)

local btn2=display.NewImage(…)
–director change scene back to level1.lua when btn2 is touch and change the global variable “a” in the file level1.lua from a=2 to a=3

–in level3.lua I am basically reloading the file level1.lua when btn2 is touch and change the variable this time a=2 in level1.lua to a=3.

Any help would be appreciated.

[import]uid: 104131 topic_id: 21123 reply_id: 321123[/import]

It sounds to me like you are setting “a” to 1 in level1.lua, then in level2.lua you want to change “a” to 2 then go back to level1.lua and have a = 2 at that point. Then if you’re in level3.lua, you want to change “a” to 3 so that when you go back to level1.lua a=3.

If that’s the case, in level1.lua where you say:

a = 1

it’s going to reset every time you goto leve1.lua. It seems to me the easy fix is to set:

a = 1

in main.lua, change it in level2.lua and level3.lua so when you get to level1.lua, it has the value you want.

[import]uid: 19626 topic_id: 21123 reply_id: 83544[/import]

Yeah. The simplest way is to declare it in main. But if you really want it visible everywhere you need to use super-globals, not just globals…

_G.myVar = 1

You only need to specify _G when you first declare. After that it’s just myVar… (or a, or b, or whatever you want.) If you really need a far reaching variable declared in one of your levelx.lua files that’s the most comprehensive way to go.

Generally speaking it’s better to keep all of your globals in main though; much easier to manage everything, especially if you need to save/load to SQL or some other file type. [import]uid: 41884 topic_id: 21123 reply_id: 83551[/import]

Thank you for the quick response. You are correct. That is exactly what I want to do. Just don’t know how to defind the variable “a” such that when the scene change it change the value of “a” also in level1.lua. Take a look of what I came up with. Thanks again for your help.


main.lua

a=1 – defind global “a”

level1.lua

module(…, package.seeall)

–Director change scene to level2.lua. when bnt1 touch
local nextScene=function(event)
if event.phase=“ended” then

director:changeScene(“level2”, “flip”)
end

btn1:addEventListener(“touch”, nextScene)


Level2. lua

module(…, package.seeall)

local btn2=display.NewImage(…)

local nextScene=function(event)
if event.phase=“ended” then

a=2 --change the value for variable “a” which located in level1.ua when the scene changes
director:changeScene(“level1”, “flip”)
end

btn2:addEventListener(“touch”, nextScene)

–director change scene back to level1.lua when btn2 is touch and change the global variable “a” in level1.lua from a=1 to a=2


Level3. lua

module(…, package.seeall)

local btn3=display.NewImage(…)

local nextScene=function(event)
if event.phase=“ended” then

a=3 --change the value for variable “a” in level1.lua
director:changeScene(“level1”, “flip”)
end

btn3:addEventListener(“touch”, nextScene)
–director change scene back to level1.lua when btn3 is touch and change the global variable “a” in the file level1.lua from a=2 to a=3

–in level3.lua I am basically reloading the file level1.lua when btn3 is touch and change the variable this time a=2 in level1.lua to a=3.
[import]uid: 104131 topic_id: 21123 reply_id: 83556[/import]

First off, when you’re typing code use < code > and < / code > (without the spaces) at the beginning and end to make it look like actual code to anyone reading your post. :slight_smile:

Anyway, if you only have the one “a” variable, just…

[code]-- main.lua
– Declare a as a global
_G.a = 1

– If you want a to increment constantly instead of change to a specific number
– In whatever level.lua files you like
a = a + 1[/code] [import]uid: 41884 topic_id: 21123 reply_id: 83616[/import]

Thanks Richard9. Sorry for the delay in responding. I took a little break from it all. It works exactly as you describe. And thanks also for the tip on how to display codes in the forum. I learned that it’s not good to use _G to define global variables. It causes memory issues. Hopefully some one will post an example on how to avoid global variables. Thanks again for the response.

[import]uid: 104131 topic_id: 21123 reply_id: 86656[/import]

No worries.

There’s nothing particularly wrong with _G. as far as I know. Since it’s the highest level variable it’s always in memory until you specify otherwise, and I presume it has a bigger performance hit if you use it a lot per second. But in limited use it’s just fine - I think it’s perfect for game control variables, player data, etc.

The alternative to _G is to use get functions…but I still use a combo depending on what it is.

For example, if you have a secondary lua file using the preferred method…

[code]–other.lua
local M = {}

local myVars = {}
myVar.level = 2

local getLevel = function()
local level = myVar.level

return level
end

M.myFunction = myFunction

return M[/code]

You could then call that from another lua file like this…

local level = other.getLevel()

The trick is that you would need to write functions for updating that variable as well.

Generally speaking though _G. is just fine, especially if you use it occasionally (i.e.: not once a frame, but on level load or something is probably no big deal.) I wouldn’t use _G to store entire level structures but small tables of data are fine.

EDIT: I guess you could also just define the table myVar as M.myVar and deal with it normally! But maybe the above example gave you some ideas. [import]uid: 41884 topic_id: 21123 reply_id: 86663[/import]