storyboard createScene cant access params passed when entering the scene the second time

In my app I have a home scene and a game scene. The user enters the game scene by tapping a button in the home scene. When that happens I pass parameters to the game scene which I am able to access perfectly well in game’s createScene.

Here is the code that creates the params and passes them:

[lua] local options =
{
effect = “slideLeft”,
time = 250,
params =
{
val = 1,
}
}
storyboard.gotoScene( “game”, options )
[/lua]

No problems here. I am able to access event.params.val in game’s createScene.

However, when the user exits the game scene and returns to the home scene and then reenters the game scene the params are no longer there/accessible.

Here’s the code for leaving the game scene and entering the home scene:

[lua] local homeOptions =
{
effect = “slideRight”,
time = 250,
}

storyboard.gotoScene( “home”, homeOptions )[/lua]

I can’t understand why the params are accessible on the first run but unaccessible on the second run. 
Does the community have any ideas?

I’m having similar problem. Params is not getting passed always. Often times it happens on the first time entering a scene, other times when later entering scene. So more random than your case it seems.

I was reading this the other day and he also mentions this: http://developer.coronalabs.com/forum/2013/01/09/tips-tricks-learned-while-developing-shaqdown

Whether the createScene event is triggered or not depends upon whether you are using purgeScene or createScene.

purgeScene:

When you use purgeScene to remove a previously visited scene only the display object’s view is removed. They are no longer visible on the screen but the scene is still in memory. So the next time you visit the scene the createScene event is not triggered.

removeScene:

When you use removeScene to remove a previously visited scene the display object is removed from memory. So the next time you visit the scene the createScene event IS triggered.

This is one reason why it’s best to insert your display objects into the scene group. When the scene’s group is removed, all the display objects in that group are removed. This of course does not apply to native widgets. Native widgets (or objects) can not be placed into display groups and must be removed (nativeObject:removeSelf()) manually. I am woking on a blog post that will explain how to wok with native widgets and Storyboard scenes.

So I hope that answers your question about your params. I know I kinda went on a tangent but it was all related. Your params are not accessible the second time around if you are purging the scene.

Take a look at this great blog post about Storyboard scenes:

http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

Also look at this equally great post about getting rid of your Globals. You don’t have to use the param feature to pass data from scene to scene - I suggest trying this approach instead. I use it in all my projects now.

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

And play around by putting print statements in your code. I learned a lot from doing that.

Thanks

Thanks for your answers. 
 

@eddierush what was causing the abovementioned problem was that the gotoscene(“game” …) was being fired two times the second time I was moving to the game scene . Simply put, I was not removing my scenes at the correct places and I was not cleaning up my objects correctly. Your comments on the scene group was really helpful in removing objects correctly.

Actually, purgeScene will cause the scene’s createScene() event to fire a second time.  What doesn’t fire is anything that’s in the scene’s “main chunk” - like at the top of the scene

Yes Rob - I stand corrected. Thanks. I placed print statements in all the scene event handlers (and even outside the event handlers) and used the purgeScene method in both views. This is the Terminal log I got (That’s why they call you a Jedi)

2013-06-16 19:35:47.173 Corona Simulator[21447:707] main: GOING TO view1

2013-06-16 19:35:47.174 Corona Simulator[21447:707] view1: OUTSIDE

2013-06-16 19:35:47.176 Corona Simulator[21447:707] view1: createScene()

2013-06-16 19:35:47.197 Corona Simulator[21447:707] view1: enterScene()

2013-06-16 19:35:49.661 Corona Simulator[21447:707] main: GOING TO view2

2013-06-16 19:35:49.662 Corona Simulator[21447:707] view1: exitScene()

2013-06-16 19:35:49.662 Corona Simulator[21447:707] view2: OUTSIDE

2013-06-16 19:35:49.663 Corona Simulator[21447:707] view2: createScene()

2013-06-16 19:35:49.690 Corona Simulator[21447:707] view2: enterScene()

2013-06-16 19:35:49.690 Corona Simulator[21447:707] view1: destroyScene()

2013-06-16 19:35:50.847 Corona Simulator[21447:707] main: GOING TO view1

2013-06-16 19:35:50.849 Corona Simulator[21447:707] view2: exitScene()

2013-06-16 19:35:50.850 Corona Simulator[21447:707] view1: createScene()

2013-06-16 19:35:50.877 Corona Simulator[21447:707] view1: enterScene()

2013-06-16 19:35:50.878 Corona Simulator[21447:707] view2: destroyScene()

Thanks again :slight_smile:

I’m having similar problem. Params is not getting passed always. Often times it happens on the first time entering a scene, other times when later entering scene. So more random than your case it seems.

I was reading this the other day and he also mentions this: http://developer.coronalabs.com/forum/2013/01/09/tips-tricks-learned-while-developing-shaqdown

Whether the createScene event is triggered or not depends upon whether you are using purgeScene or createScene.

purgeScene:

When you use purgeScene to remove a previously visited scene only the display object’s view is removed. They are no longer visible on the screen but the scene is still in memory. So the next time you visit the scene the createScene event is not triggered.

removeScene:

When you use removeScene to remove a previously visited scene the display object is removed from memory. So the next time you visit the scene the createScene event IS triggered.

This is one reason why it’s best to insert your display objects into the scene group. When the scene’s group is removed, all the display objects in that group are removed. This of course does not apply to native widgets. Native widgets (or objects) can not be placed into display groups and must be removed (nativeObject:removeSelf()) manually. I am woking on a blog post that will explain how to wok with native widgets and Storyboard scenes.

So I hope that answers your question about your params. I know I kinda went on a tangent but it was all related. Your params are not accessible the second time around if you are purging the scene.

Take a look at this great blog post about Storyboard scenes:

http://www.coronalabs.com/blog/2012/03/27/storyboard-scene-events-explained/

Also look at this equally great post about getting rid of your Globals. You don’t have to use the param feature to pass data from scene to scene - I suggest trying this approach instead. I use it in all my projects now.

http://www.coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

And play around by putting print statements in your code. I learned a lot from doing that.

Thanks

Thanks for your answers. 
 

@eddierush what was causing the abovementioned problem was that the gotoscene(“game” …) was being fired two times the second time I was moving to the game scene . Simply put, I was not removing my scenes at the correct places and I was not cleaning up my objects correctly. Your comments on the scene group was really helpful in removing objects correctly.

Actually, purgeScene will cause the scene’s createScene() event to fire a second time.  What doesn’t fire is anything that’s in the scene’s “main chunk” - like at the top of the scene

Yes Rob - I stand corrected. Thanks. I placed print statements in all the scene event handlers (and even outside the event handlers) and used the purgeScene method in both views. This is the Terminal log I got (That’s why they call you a Jedi)

2013-06-16 19:35:47.173 Corona Simulator[21447:707] main: GOING TO view1

2013-06-16 19:35:47.174 Corona Simulator[21447:707] view1: OUTSIDE

2013-06-16 19:35:47.176 Corona Simulator[21447:707] view1: createScene()

2013-06-16 19:35:47.197 Corona Simulator[21447:707] view1: enterScene()

2013-06-16 19:35:49.661 Corona Simulator[21447:707] main: GOING TO view2

2013-06-16 19:35:49.662 Corona Simulator[21447:707] view1: exitScene()

2013-06-16 19:35:49.662 Corona Simulator[21447:707] view2: OUTSIDE

2013-06-16 19:35:49.663 Corona Simulator[21447:707] view2: createScene()

2013-06-16 19:35:49.690 Corona Simulator[21447:707] view2: enterScene()

2013-06-16 19:35:49.690 Corona Simulator[21447:707] view1: destroyScene()

2013-06-16 19:35:50.847 Corona Simulator[21447:707] main: GOING TO view1

2013-06-16 19:35:50.849 Corona Simulator[21447:707] view2: exitScene()

2013-06-16 19:35:50.850 Corona Simulator[21447:707] view1: createScene()

2013-06-16 19:35:50.877 Corona Simulator[21447:707] view1: enterScene()

2013-06-16 19:35:50.878 Corona Simulator[21447:707] view2: destroyScene()

Thanks again :slight_smile: