Remove/recycle a scene

Hi,

when I should use composer.removeScene() or composer.recycleOnSceneChange = true ? I’m looking for practical examples. 

ldurniat

surely that depends on your use case?

I prepare update for my first game THE SQUARE. What is important I rewrite whole project.

One of early  video of my game 

https://www.youtube.com/watch?v=jzQbf94Q6g0

Edit: I find this and thisuseful for me.

In an ideal world you would only remove scenes if you’re needing to reduce the amount of memory your app is using.  Composer wants to cache previous scenes so that when you go back to them, they don’t have to reload all the images from storage and re-create all the textures. As a developer you should be able to detect when a person comes back to a scene and take the opportunity to reposition anything that moved that needs to reset.

This leads to the main reason people want to remove scenes. It takes work to properly reset a scene. It’s simply easier to delete the scene and let the scene:create() function rebuild the scene. This stems from the fact that Lua doesn’t reprocess already loaded modules and people execute code in the main chunk of the module and they don’t know to reset that data too. Here is a simple example:

-- near the top of game.lua -- this code isn't inside any scene function -- It will only execute the first time the scene loads. local score = 0

Later during game play you add some values to score and lets say it now has the value of 100. You leave game.lua and then later come back to game.lua.  The value of score is still 100, not 0. Because that block of code does not re-execute. As a developer you’re expected to reset the value to 0 in your scene:show()'s “will” phase.  The more complex the game, the more work it takes to reset the scene.

When you remove the scene, the module gets un-required and you get a fresh start.  

Finally this is a big distinction between removeScene() and recycleOnSceneChange = true.  composer.removeScene() will un-require the scene. Setting composer.recycleOnSceneChange = true just causes the view group to be removed and scene:create() will be called on re-entry. However code in the main chunk will not be re-executed.

Rob

@Rob Miracle: Awesome answer :slight_smile: Thanks.

surely that depends on your use case?

I prepare update for my first game THE SQUARE. What is important I rewrite whole project.

One of early  video of my game 

https://www.youtube.com/watch?v=jzQbf94Q6g0

Edit: I find this and thisuseful for me.

In an ideal world you would only remove scenes if you’re needing to reduce the amount of memory your app is using.  Composer wants to cache previous scenes so that when you go back to them, they don’t have to reload all the images from storage and re-create all the textures. As a developer you should be able to detect when a person comes back to a scene and take the opportunity to reposition anything that moved that needs to reset.

This leads to the main reason people want to remove scenes. It takes work to properly reset a scene. It’s simply easier to delete the scene and let the scene:create() function rebuild the scene. This stems from the fact that Lua doesn’t reprocess already loaded modules and people execute code in the main chunk of the module and they don’t know to reset that data too. Here is a simple example:

-- near the top of game.lua -- this code isn't inside any scene function -- It will only execute the first time the scene loads. local score = 0

Later during game play you add some values to score and lets say it now has the value of 100. You leave game.lua and then later come back to game.lua.  The value of score is still 100, not 0. Because that block of code does not re-execute. As a developer you’re expected to reset the value to 0 in your scene:show()'s “will” phase.  The more complex the game, the more work it takes to reset the scene.

When you remove the scene, the module gets un-required and you get a fresh start.  

Finally this is a big distinction between removeScene() and recycleOnSceneChange = true.  composer.removeScene() will un-require the scene. Setting composer.recycleOnSceneChange = true just causes the view group to be removed and scene:create() will be called on re-entry. However code in the main chunk will not be re-executed.

Rob

@Rob Miracle: Awesome answer :slight_smile: Thanks.