Pass Variable Through Scenes

Hi,

I have troubles passing score to other, not direct scene. I’m familiar with globals and modules, but exactly in this case I can’t adjust none.

I have game scene where all action going and after loss an “end game” scene occurs to witch score are passed with composer.setVariable. Here in “end game” scene I am saving and loading score with JSON witch allows me to show best score right away after loss. And I have scene “best score” and want to have access to saved best score in “end game”. Scenes are not direct to each other so I’m not using set get variable.

I tried _G.scoresTable = {} in “end game” scene witch allows access score with _G.scoresTable[1] in “best score” scene but after launching game “end game” must be exhausted first to have that score shown in the “best score” otherwise result is nil. With attempt to set module inside “end game” scene code I had zero result either.

Globals aren’t always bad (it is just how they are used that can be bad)… for something as simple as this, just declare _highScore = 0 in your main and then you can get/set it from anywhere in your app.

Hi @gudrutis1,

Are you using the Composer scene framework? If so, I really recommend the “composer.setVariable()” and “composer.getVariable()” functions. They let you access variables across scenes via any name/setup you choose, including just setting one table variable and adding/removing/setting children variables/keys within it.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

This certainly isn’t the only way, but I find it much easier than hand-rigging passing/access of variables from module to module in Lua, and it avoids evil (IMHO) global variables entirely.

Brent

@Brent, globals are not necessarily evil… missuse of them is!  

It is using globals rather than defining interfaces in OOP languages that is wrong.  

Globals for things like 

\_originX, \_originY = display.screenOriginX, display.screenOriginY \_W, \_H = display.contentWidth - (\_originX \* 2), display.contentHeight - (\_originY \* 2) \_centreX, \_centreY = \_originX +\_W/2, \_originY + \_H/2

save you recalculating them all the time in dynamic layouts.

After all, composer.setVariable() is just a wrapper.  Seen as this is globally scoped it is still open to the same abuse that any other global is exposed to.

I had totally forgotten that I was declared  scoresTable = {} in main, that’s why before “end game” scene I have result a nil. Anyway I need to access score from scoresTable = {} witch is in “end game” scene, this is the place where score is created. I guess if I want to have it right away after game launches I need to use JSON in “best score” scene as well, for loading it.

But what if I reach best score and will turn of the game in “end game” scene. Does that don’t mean that a variable with composer.setVariable will not be passed to the others scenes?

The variable set by “composer.setVariable()” will always be available to other Composer-enabled scenes/modules. Of course, you should update that variable to whatever value it should be, when needed, i.e. when setting a new high score.

Brent

Globals aren’t always bad (it is just how they are used that can be bad)… for something as simple as this, just declare _highScore = 0 in your main and then you can get/set it from anywhere in your app.

Hi @gudrutis1,

Are you using the Composer scene framework? If so, I really recommend the “composer.setVariable()” and “composer.getVariable()” functions. They let you access variables across scenes via any name/setup you choose, including just setting one table variable and adding/removing/setting children variables/keys within it.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

This certainly isn’t the only way, but I find it much easier than hand-rigging passing/access of variables from module to module in Lua, and it avoids evil (IMHO) global variables entirely.

Brent

@Brent, globals are not necessarily evil… missuse of them is!  

It is using globals rather than defining interfaces in OOP languages that is wrong.  

Globals for things like 

\_originX, \_originY = display.screenOriginX, display.screenOriginY \_W, \_H = display.contentWidth - (\_originX \* 2), display.contentHeight - (\_originY \* 2) \_centreX, \_centreY = \_originX +\_W/2, \_originY + \_H/2

save you recalculating them all the time in dynamic layouts.

After all, composer.setVariable() is just a wrapper.  Seen as this is globally scoped it is still open to the same abuse that any other global is exposed to.

I had totally forgotten that I was declared  scoresTable = {} in main, that’s why before “end game” scene I have result a nil. Anyway I need to access score from scoresTable = {} witch is in “end game” scene, this is the place where score is created. I guess if I want to have it right away after game launches I need to use JSON in “best score” scene as well, for loading it.

But what if I reach best score and will turn of the game in “end game” scene. Does that don’t mean that a variable with composer.setVariable will not be passed to the others scenes?

The variable set by “composer.setVariable()” will always be available to other Composer-enabled scenes/modules. Of course, you should update that variable to whatever value it should be, when needed, i.e. when setting a new high score.

Brent