Storyboard not reinitializing physics when PurgeOnSceneChange is used

Hello,

I’m working on a game that will eventually have a lot of graphics and physics assets, so I want to clear out everything when I switch between levels (or when I restart a level).

I’ve set up some print statements in each of the storyboard functions (as well as at the top of the lua file), and I notice that by default, Storyboard only runs the initial declarations and CreateScene once, then prefers to just EnterScene and ExitScene when switching between lua files. DestroyScene isn’t called unless I call PurgeScene or set PurgeOnSceneChange to true, which means all my assets are still floating around in memory (probably consuming resources). However, if I do purge a scene, when I try to reload that scene, I’m not seeing the print statement I placed at the top of the lua file show up. It seems Storyboard goes directly to CreateScene if the lua file has been previous loaded, skipping any initial declaration statements (such as require physics), which is causing problems when I try to reinitialize stuff in CreateScene the second time the file loads.

Am I missing something simple, or is this something I really need to worry about. I eventually want to have multiple levels, but if DestroyScene isn’t called unless I manually purge the scene, and if there’s no way to reinitialize all the stuff in the header, this could lead to a big headache.

Any help is appreciated!

Anthony

Okay, I seem to have solved my own problem. In the template code comments, it says that storyboard.removeScene() must be called to totally remove everything and start over. Since this has to be called from outside of the scene I want removed, I had to use a custom parameter in the storyboard.gotoScene() function to tell my new scene what the old scene is to remove:

Inside the game level that I want cleared:

storyboard.purgeOnSceneChange = true local thisLevel = "level1" local function resetLevel() local options = { effect = "fade", time = 500, params = { lastLevel = thisLevel} } storyboard.gotoScene( "menu", options ) end

Inside the menu (or intermediate scene):

local lastLevel = nil function scene:createScene( event ) local params = event.params lastLevel = params.lastLevel end function scene:enterScene( event ) storyboard.removeScene(lastLevel) end

I noticed during testing that enterScene gets called twice when the menu is initially created, so trying read the lastLevel parameter there results in an error in the console stating the params is nil (probably from the second time enterScene is called). Reading the lastLevel parameter in createScene doesn’t cause this issue because it is only called once.

One final observation is the order the storyboard functions are called (this could help somebody having problems cleaning up between scene changes):

Corona Simulator[:] WARNING: Simulator does not support multitouch events Corona Simulator[:] \<\<==== INITIAL LOAD - LEVEL 1 =====\> Corona Simulator[:] \<\<==== CREATE SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== ENTER SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== EXIT SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== INITIAL LOAD - MENU =====\> Corona Simulator[:] \<\<==== CREATE SCENE - MENU =====\> Corona Simulator[:] \<\<==== EXIT SCENE - MENU =====\> Corona Simulator[:] \<\<==== ENTER SCENE - MENU =====\> Corona Simulator[:] level1 Corona Simulator[:] \<\<==== DESTROY SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== ENTER SCENE - MENU =====\> Corona Simulator[:] level1 Corona Simulator[:] \<\<==== EXIT SCENE - MENU =====\>

Okay, I seem to have solved my own problem. In the template code comments, it says that storyboard.removeScene() must be called to totally remove everything and start over. Since this has to be called from outside of the scene I want removed, I had to use a custom parameter in the storyboard.gotoScene() function to tell my new scene what the old scene is to remove:

Inside the game level that I want cleared:

storyboard.purgeOnSceneChange = true local thisLevel = "level1" local function resetLevel() local options = { effect = "fade", time = 500, params = { lastLevel = thisLevel} } storyboard.gotoScene( "menu", options ) end

Inside the menu (or intermediate scene):

local lastLevel = nil function scene:createScene( event ) local params = event.params lastLevel = params.lastLevel end function scene:enterScene( event ) storyboard.removeScene(lastLevel) end

I noticed during testing that enterScene gets called twice when the menu is initially created, so trying read the lastLevel parameter there results in an error in the console stating the params is nil (probably from the second time enterScene is called). Reading the lastLevel parameter in createScene doesn’t cause this issue because it is only called once.

One final observation is the order the storyboard functions are called (this could help somebody having problems cleaning up between scene changes):

Corona Simulator[:] WARNING: Simulator does not support multitouch events Corona Simulator[:] \<\<==== INITIAL LOAD - LEVEL 1 =====\> Corona Simulator[:] \<\<==== CREATE SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== ENTER SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== EXIT SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== INITIAL LOAD - MENU =====\> Corona Simulator[:] \<\<==== CREATE SCENE - MENU =====\> Corona Simulator[:] \<\<==== EXIT SCENE - MENU =====\> Corona Simulator[:] \<\<==== ENTER SCENE - MENU =====\> Corona Simulator[:] level1 Corona Simulator[:] \<\<==== DESTROY SCENE - LEVEL 1 =====\> Corona Simulator[:] \<\<==== ENTER SCENE - MENU =====\> Corona Simulator[:] level1 Corona Simulator[:] \<\<==== EXIT SCENE - MENU =====\>