Variable Scope - Can't pass variables between scenes

I’m not sure if this is in the right forum, apologies if it isn’t.

I’m using the director class to move between my scenes. The app is a game, following a standard format of menus, with level lists.

I have a variable, which is set inside main.lua, called currentLevel. It’s set to 1 by default. When the user clicks a level from the level select screen I change this variable to the number of the level selected.

This variable isn’t being passed through to my game.lua file though. Inside game.lua the currentLevel variable is set to ‘1’ - which I find very strange.

Any ideas? Code below:
main.lua:

currentLevel = 1 print("The current level from main: " .. currentLevel)

gamemenu.lua:

[code] local objectTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
audio.stop( gameMusic )

– Go to level
currentLevel = params.level
print("The current level from menu: " … currentLevel)
director:changeScene( “loadthegame” )
end
end[/code]

loadthegame.lua (in full)

[code]module(…, package.seeall)

– Main function - MUST return a display.newGroup()
function new()
print("The current level from loader: " … currentLevel)
local localGroup = display.newGroup()

local theTimer
local loadingImage

local showLoadingScreen = function()
loadingImage = display.newImageRect( “loading.png”, 480, 320 )
loadingImage.x = 240; loadingImage.y = 160

localGroup:insert( loadingImage )

local goToLevel = function()
director:changeScene( “thegame” )
end

theTimer = timer.performWithDelay( 1000, goToLevel, 1 )
end

showLoadingScreen()

unloadMe = function()
if theTimer then timer.cancel( theTimer ); end
end

– MUST return a display.newGroup()
return localGroup
end[/code]

The outputs the following in the terminal:

The current level from main: 1 The current level from menu: 6 The current level from loader: 1 [import]uid: 62042 topic_id: 15570 reply_id: 315570[/import]

Bump - just changed thread title, hopefully get more reads/responses :slight_smile: [import]uid: 62042 topic_id: 15570 reply_id: 57493[/import]

I think the quickest thing to try would be to use “_G.currentLevel” at lines 7 and 8 of gamemenu.lua. You’re creating the global currentLevel in main.lua, but to change/access it from another module I’m pretty sure you have to use _G, otherwise you’re just creating another variable named currentLevel that is visible only in the module you created it in. You’d also probably need to use the _G. prefix in your level select code and wherever you read the level # to know which one to load.

There have been some good blog articles on this site detailing global, local, scope etc. I guess there is some controversy with using _G. but in your case it might be the quickest fix without having to rewrite everything.

A better approach might be to have another module where you create and store variables you need to use across modules, like currentLevel, and have functions that get and set those variables. Then you can just call those functions from any module.

[import]uid: 9422 topic_id: 15570 reply_id: 57499[/import]

i think _G. is not the only way here, but its definitely the one i use because its easy [import]uid: 16142 topic_id: 15570 reply_id: 57501[/import]

Can you point me to one of these articles please? I did have a look, but couldn’t find one.

Personally I think things like this should be first port of call to new developers, it’s something that gets very frustrating and wastes a lot of time that really shouldn’t. Maybe the Corona staff should add it to the ‘getting started’ documentation? [import]uid: 62042 topic_id: 15570 reply_id: 57505[/import]

I’ve bookmarked a couple relating to local vs global variables for me to look at again later. I’m sure there are lots more… I hope you’ll find the following helpful.

http://blog.anscamobile.com/2011/07/local-variables-lua/

Scroll down to Lua: Best Practices section:
http://developer.anscamobile.com/content/performance-and-optimization

Good luck.
[import]uid: 67217 topic_id: 15570 reply_id: 57509[/import]