values across scenes

Hey,

I am new to programing and I would need help to figure out a logical issue. It regards values across different scenes.

I have an option-screen scene and a menu/play-scene. In the option screen a value can be toggled from true to false through a button, this value is global so it will have its effects in game once exited the option screen and got back to the play screen.

Everything works fine to this point, but I’d like to add an ulterior function in the play scene that will be called if and only if the original value has been toggled in the option screen. So the play scene should ‘check’ in the scene:show phase wether that global value has been changed when returning from the option screen compared to when option screen was entered.

How do I express in coding terms? Thanks I hope this is clear enough…

First, with regards to using globals, in particular for new programmers, they are dangerous and lead to problems.  You might find this tutorial to be of use.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Now for your issue at hand, you will need another variable that would hold a simple true or false, like:

didSettingsChange

In your settings screen, initialize it to false.  If your toggle handlers, if it’s changed, then set didSettingsChange to true.  Then in your scenes scene:show() event in the did phase do a:

if didSettingsChange then

     didSettingsChange = false – reset it

     – do whatever you need to do because the settings changed

end

or something similar to that (avoiding globals of course using the technique in the tutorial).

Rob

Thanks Rob!

what do you exately mean by this?

If your toggle handlers, if it’s changed, then set didSettingsChange to true

You mean I should change didSettingsChange to true everytime the toggle is switched?

'cause I’d like to have it changed only if when exiting the optionmenu scene the button is NOT in the position it was when entered the optionmenu scene.

In other words didSettingsChange should not be variated regardless of how many times the button gets switched inside of the option screen.

Thanks for your time…

You can use the same concept either way.  You just have to program it the way you want it.  You might want to save the original state of each setting when you enter your settings scene, then right before you leave the scene, check to see if the settings are different than what they were when you entered and then set your flag for your other scene.

You’re creativity is up to you!

Rob

thanks a lot Rob.

I resolved by creating two new differents variables and put them respectively in show and hide scene. Since I equaled both of them to the variable of which value can be toggled in optionscreen menu they have possibly different values themselves when exiting the scene. I can now make a comparison of the two for my purpose in the other scene.

Thanks a lot again!

First, with regards to using globals, in particular for new programmers, they are dangerous and lead to problems.  You might find this tutorial to be of use.

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Now for your issue at hand, you will need another variable that would hold a simple true or false, like:

didSettingsChange

In your settings screen, initialize it to false.  If your toggle handlers, if it’s changed, then set didSettingsChange to true.  Then in your scenes scene:show() event in the did phase do a:

if didSettingsChange then

     didSettingsChange = false – reset it

     – do whatever you need to do because the settings changed

end

or something similar to that (avoiding globals of course using the technique in the tutorial).

Rob

Thanks Rob!

what do you exately mean by this?

If your toggle handlers, if it’s changed, then set didSettingsChange to true

You mean I should change didSettingsChange to true everytime the toggle is switched?

'cause I’d like to have it changed only if when exiting the optionmenu scene the button is NOT in the position it was when entered the optionmenu scene.

In other words didSettingsChange should not be variated regardless of how many times the button gets switched inside of the option screen.

Thanks for your time…

You can use the same concept either way.  You just have to program it the way you want it.  You might want to save the original state of each setting when you enter your settings scene, then right before you leave the scene, check to see if the settings are different than what they were when you entered and then set your flag for your other scene.

You’re creativity is up to you!

Rob

thanks a lot Rob.

I resolved by creating two new differents variables and put them respectively in show and hide scene. Since I equaled both of them to the variable of which value can be toggled in optionscreen menu they have possibly different values themselves when exiting the scene. I can now make a comparison of the two for my purpose in the other scene.

Thanks a lot again!