Most people want to simply reload a scene and they look for a magical tool to do that. One might think that simply calling storyboard.gotoScene(“currentscenename”) (where “currentscenename” is the name of your current scene, like “level1”) but that simply doesn’t work. There is what seems to be the magical answer in storyboard.reloadScene() and it does what it says it does, but it’s not magical.
You have to know what you’re doing to make it work. It’s not magic and takes planning and a strong understanding of how Lua modules load and unload, what parts get executed and when and on top of that understanding when storyboard scene events execute.
Because every game/app is different and what you have to do to restart your scene is different it’s very hard, if not impossible to provide an “easy to understand template” or say in a few words how to make this work.
If you have to go down this path to avoid an intermediate scene then you need to take into consideration.
- Code that is executed in the main chunk is run once… ever… unless the module is un-required then re-required. The storyboard.removeScene() does this, but you really shouldn’t remove the scene that’s on the screen and wait until it’s transitioned off. If you’re reloading, you’re not transitioning anything off screen, so it’s kind of a bad practice to remove it. Therefore, code in the main chunk cannot be expected to reset your level.
2. scene:createScene() is only called if the scene’s view (group) is destroyed. This happens when the scene is purged after leaving the scene if you have automatic purging on, if the scene is off screen and a low memory event happens or if the scene is off screen and you call storyboard.purgeScene() or storyboard.purgeAll(). The last call won’t purge the active scene. According to the docs, if you intend to have createScene() called on reload, you must call purgeScene() from the scene:exitScene() or scene:willExitScene() function. For safety, I would do it as the very last thing in exitScene.
3. The only guarenteed functions to execute on a reload is scene:exitScene() and then scene:enterScene() (well the willExitScene and willEnterScene get called). Therefore you have to:
a. in your main chunk, define the variables that need to survive the reload but do not depend on their initialization as they will keep their last values during the reload.
b. create your visual assets in scene:createScene() for their initial locations.
c. in willEnterScene() initialize any variables that need to be reset on a reload or initial load. Position any graphical elements that could have moved that need to go back to their starting position.
d. in enterScene() start up any timers, transitions, Runtime listeners, physics, audio, etc.
e. in exitScene() undo anything you did in enterScene and the last thing call storyboard.purgeScene() if you want createScene() to re-execute.
Then if you’re lucky you can use storyboard.reloadScene() to actually reload the scene. If you pass any parameters to the scene on your initial gotoScene(), those are not passed back in. You have to save/set those values in your willEnterScene initialization code.