Updating Clock Settings with LoadSave

Hello,

I am struggling a bit with getting my clock to update after a change in my game settings.  

I have a countdown clock that works fine on my play.lua page  I have a settings.lua page where the user can choose that the clock to be set to 60 seconds, 90 seconds or 120 seconds.  

When I launch the app all works well.  When I go to the settings page and change from say 120 seconds to 60 seconds and then save to the settings.json file with loadsave, that all works correctly too. 

My problem is that when I go back to the play.lua page, the clock does not automatically update from the 120 seconds to the 60 seconds.  I know the correct data is saved and loaded into the scene as I checked it with a print function but it is not displaying that data.  It displays the original data of 120 seconds and only updates to the correct 60 seconds when I click the timer reset button.  

I thought that each time the scene loads it would read through the code again from the top and display the appropriate text as it does when I launch the app.  Forgive my newbieness but is it because I have all of the code in the scene:create section and it only runs once unless a touch event or some other function occurs? 

Thanks in advance for any guidance! 

Christi 

Once you require a module (which is what Composer does with your scene file), it will not re-require it and code in the main chunk will not re-execute. This is why we suggest that things like timers and such be done from scene:show()/scene:hide() since those functions will be called on entry and exit of the scene. You can have your code in a function and have scene:show() call your function. Remember each scene:show() is called twice: once before the scene comes on screen one after. Look at the event.phase and either do your code in the “will” phase or the “did” phase but not both.

Depending on the complexity of your app and the amount of work you’re doing in the main chunk, it might be easier to un-require the module. Composer does this for you when you call composer.removeScene(). The trick is you can’t remove the scene you’re in. In the scene with your settings, you can call composer.removeScene() for the scene you’re getting ready to go to.

Rob

Rob,

Thank you so much for your response.  I used your suggestion of composer.removeScene() in my Settings scene and all works great now.  Thank you!  

In regards to the other option you suggested, I started my Corona learning by using Kwik and am now learning to code without it.  I am still a bit confused about what to place in scene:create and scene:show.  Do you have a suggested tutorial with real life code examples?  Thanks as always for your help!

Christi 

There are a couple of sample apps you can look at:

https://github.com/coronalabs/sample-game-project

https://github.com/coronalabs-samples/business-app-sample

https://github.com/coronalabs-samples/CoronaCannon

There is also a Composer sample that comes with the SampleCode we ship with Corona.

Look at this blog post:  https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

It’s a round up of many of our Composer resources including the Guide, tutorials, videos and more.

Finally look at our Getting Started guide: https://docs.coronalabs.com/guide/programming/index.html

Chapters 4 and 5 are about converting a non-Composer app to a Composer based app.

Rob

Fabulous!!  Thanks so much Rob!!!  :slight_smile:

Once you require a module (which is what Composer does with your scene file), it will not re-require it and code in the main chunk will not re-execute. This is why we suggest that things like timers and such be done from scene:show()/scene:hide() since those functions will be called on entry and exit of the scene. You can have your code in a function and have scene:show() call your function. Remember each scene:show() is called twice: once before the scene comes on screen one after. Look at the event.phase and either do your code in the “will” phase or the “did” phase but not both.

Depending on the complexity of your app and the amount of work you’re doing in the main chunk, it might be easier to un-require the module. Composer does this for you when you call composer.removeScene(). The trick is you can’t remove the scene you’re in. In the scene with your settings, you can call composer.removeScene() for the scene you’re getting ready to go to.

Rob

Rob,

Thank you so much for your response.  I used your suggestion of composer.removeScene() in my Settings scene and all works great now.  Thank you!  

In regards to the other option you suggested, I started my Corona learning by using Kwik and am now learning to code without it.  I am still a bit confused about what to place in scene:create and scene:show.  Do you have a suggested tutorial with real life code examples?  Thanks as always for your help!

Christi 

There are a couple of sample apps you can look at:

https://github.com/coronalabs/sample-game-project

https://github.com/coronalabs-samples/business-app-sample

https://github.com/coronalabs-samples/CoronaCannon

There is also a Composer sample that comes with the SampleCode we ship with Corona.

Look at this blog post:  https://coronalabs.com/blog/2015/08/11/tutorial-treasury-composer/

It’s a round up of many of our Composer resources including the Guide, tutorials, videos and more.

Finally look at our Getting Started guide: https://docs.coronalabs.com/guide/programming/index.html

Chapters 4 and 5 are about converting a non-Composer app to a Composer based app.

Rob

Fabulous!!  Thanks so much Rob!!!  :slight_smile: