Purging a scene causes global display objects to be removed from memory

I have a problem whereby when I call storyboard.purgeScene(), my global display objects seem to be removed. When I say ‘global’, they I actually store them in a user-defined storyboard table, to avoid using actual global variables:

My image database is stored in external file toy_database.lua:
[lua] local storyboard = require “storyboard”;

storyboard.globalVars.toys = {
ball1 = {
src = ‘img/stock-vector-soccer-ball-trimmed.png’,
}
}[/lua]

main.lua:
[lua] local storyboard = require “storyboard”;

storyboard.globalVars = {};

require “toy_database_toybox”;[/lua]

I load some images in my main.lua:
[lua] storyboard.globalVars.toys[“ball1”].imgPtr = display.newImageRect(storyboard.globalVars.toys[“ball1”].src, w, h);[/lua]

Then I go to another scene:
[lua] storyboard.gotoScene(“mainFloorScene”);[/lua]

where everything is fine, the display objects are accessable. I purge that scene and goto another scene:
[lua] storyboard.gotoScene( “openBoxScene” )
[in didExitScene()]: storyboard.purgeScene( “mainFloorScene” )[/lua]

Everything is fine here too. Finally I go back to the first scene, purging the current scene on the way:
[lua] storyboard.gotoScene( “mainFloorScene”, “fade”, 400 );
[in didExitScene()]: storyboard.purgeScene( “openBoxScene” )[/lua]

Back in the first scene the image objects have been removed. If I comment out the line:
[lua] storyboard.purgeScene( “openBoxScene” )[/lua]

it works fine, so I have a workaround but I’d like this behaviour explained. I expected a scene purge to only remove display objects local to that scene.

What’s going on?
[import]uid: 133169 topic_id: 32171 reply_id: 332171[/import]

yes, I also stumbled upon this behaviour when dealing with pooled/preloaded sprite animations.
instead of pooling once and re-use the sprites after reload of the game view, everything is destroyed after purging the game scene. the workaround is to destroy the pooling tables before purging the scene and re-pooling on reload… [import]uid: 70635 topic_id: 32171 reply_id: 128068[/import]

Hi, thanks for the reply.

I’m not sure what you mean. Are you saying your workaround was to avoid having pre-loaded assets altogether? Just load them as needed? [import]uid: 133169 topic_id: 32171 reply_id: 128131[/import]

hi!
no, exactly the opposite :wink: take a look at this:
http://www.ardentkid.com/blog/2012-10-03/layering-display-and-asset-pooling

I expected to be able to hold the pooled sprites in memory, but purging the scene kicked my sprites to nirvana _before_ I was able to re-pool them. So I have to destroy the complete pool when leaving the game scene and reloading it when a new game is started… [import]uid: 70635 topic_id: 32171 reply_id: 128132[/import]

yes, I also stumbled upon this behaviour when dealing with pooled/preloaded sprite animations.
instead of pooling once and re-use the sprites after reload of the game view, everything is destroyed after purging the game scene. the workaround is to destroy the pooling tables before purging the scene and re-pooling on reload… [import]uid: 70635 topic_id: 32171 reply_id: 128068[/import]

Hi, thanks for the reply.

I’m not sure what you mean. Are you saying your workaround was to avoid having pre-loaded assets altogether? Just load them as needed? [import]uid: 133169 topic_id: 32171 reply_id: 128131[/import]

hi!
no, exactly the opposite :wink: take a look at this:
http://www.ardentkid.com/blog/2012-10-03/layering-display-and-asset-pooling

I expected to be able to hold the pooled sprites in memory, but purging the scene kicked my sprites to nirvana _before_ I was able to re-pool them. So I have to destroy the complete pool when leaving the game scene and reloading it when a new game is started… [import]uid: 70635 topic_id: 32171 reply_id: 128132[/import]