Use a value in an other scene

Hi !

I’m creating a game and on the playGame.lua I create a score (which is in reality a timer).

I would like to display this score in the GameOver.lua, is this possible ? And how ?

Thank you for your answer ! (if you want me to be more precise, ask me questions)

There are a few different ways to handle this, but using composer.setVariable and composer.getVariable is one of them

Hi,
Do you want to save the variable so it’d also be accessible when an app is closed and re-opend?
Try this: https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

If you don’t meed to get the number back every time you open the app, you could just use a global variable instead of a local variable.
I hope this helps :slight_smile:

If you want to  add/retrieve variables without using globals, and if you want to save values between application runs, I’ll give a shameless plug for my plugin GBC Data Cabinet.

Please don’t use globals. There are so many options to avoid it.

Use a simple data table that you require in each module:  http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

@JonPM mentioned using Composer’s key-value store .setVariable(), .getVariable()

Composer scenes can pass parameters that are available in scene:create() and scene:show():

composer.gotoScene( "newscene", { time = 500, effect="crossFade", params = { key1=value1, key=value2, key3=value3 } )

and in “newscene”'s scene:create() and scene:show()

local params = event.params local key1 = params.key1 print( key1 ) -- prints value 1

@schizoid2K’s data cabinet works for many.

Rob

Thank you evry body ! :wink:

That’s OK for me ! :smiley:

Bye !

Lucas

You can give my module a go if you’d like. Very easy to use. 

http://sh.st/ITwPy

Please, help me for my other problem here : https://forums.coronalabs.com/topic/62743-game-if-time-then-new-object-problem/?p=325622

Thak you !

There are a few different ways to handle this, but using composer.setVariable and composer.getVariable is one of them

Hi,
Do you want to save the variable so it’d also be accessible when an app is closed and re-opend?
Try this: https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

If you don’t meed to get the number back every time you open the app, you could just use a global variable instead of a local variable.
I hope this helps :slight_smile:

If you want to  add/retrieve variables without using globals, and if you want to save values between application runs, I’ll give a shameless plug for my plugin GBC Data Cabinet.

Please don’t use globals. There are so many options to avoid it.

Use a simple data table that you require in each module:  http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

@JonPM mentioned using Composer’s key-value store .setVariable(), .getVariable()

Composer scenes can pass parameters that are available in scene:create() and scene:show():

composer.gotoScene( "newscene", { time = 500, effect="crossFade", params = { key1=value1, key=value2, key3=value3 } )

and in “newscene”'s scene:create() and scene:show()

local params = event.params local key1 = params.key1 print( key1 ) -- prints value 1

@schizoid2K’s data cabinet works for many.

Rob

Thank you evry body ! :wink:

That’s OK for me ! :smiley:

Bye !

Lucas

You can give my module a go if you’d like. Very easy to use. 

http://sh.st/ITwPy

Please, help me for my other problem here : https://forums.coronalabs.com/topic/62743-game-if-time-then-new-object-problem/?p=325622

Thak you !