I have a scene where initial variables are set in the beginning and then changed throughout the script and in modules. I know that once Corona creates a scene and goes to another, it keeps the old scene in memory for performance reasons.
However, when I get back to this scene it doesn’t function as expected because none of the variables are what they are initially supposed to be. How should I go about this? How do you guys handle this problem?
I know @roaminggamer and I differ in opinion on this. But you should use scene:create() to create all objects that are for the scene that are created before game play. If you’re spawning enemies, you shouldn’t do that when ready.
Because Lua does not re-execute the outermost chunk of code after the initial load, you can’t put something like:
local score = 0
and expect score to change when you go back to that scene. The proper way, as @roaminggamer pointed out is to use scene:show()'s “will” phase to reposition all moved objects back to their baseline, reset values like your score to 0. However this takes quite a bit of work to get everything. Most developers will go the route of using a cut scene and during that cut scene, remove the game scene before going back. That scene removal will cause the outer chunk to re-execute like coming back to the scene the first time.
I agree that create() should create objects (and destroy() should remove them).
Personally, I would create functions like initLevel() or whatever to “start” or “restart” the level. This could be called from show() or from a custom start point.
Not sure if this is covered in a tutorial or not but if not it should be - as cut scenes add additional unload/reload overhead that is really not required.
Not meaning to hijack here… but @rob I would appreciate some Corona input to my post of 2 days ago that seems to have been missed. Thanks
I rather not use the cut scene method. The thing is, I alter these variables inside modules where there is no show() happening but I did end up creating an init() function in the modules that needed it and then I called those init functions from the main scene. I’ll make sure to call init in the show() function.
Thanks again!
Ps. I’m finding that many times when I create a runtime listener or a new timer, I always need to make sure to cancel them or remove them when leaving the scene. It would be a nice feature if Corona handled all that when a scene is to be destroyed, it would just cancel all timers and remove all listeners that were created in that scene. Just a thought.
I know @roaminggamer and I differ in opinion on this. But you should use scene:create() to create all objects that are for the scene that are created before game play. If you’re spawning enemies, you shouldn’t do that when ready.
Because Lua does not re-execute the outermost chunk of code after the initial load, you can’t put something like:
local score = 0
and expect score to change when you go back to that scene. The proper way, as @roaminggamer pointed out is to use scene:show()'s “will” phase to reposition all moved objects back to their baseline, reset values like your score to 0. However this takes quite a bit of work to get everything. Most developers will go the route of using a cut scene and during that cut scene, remove the game scene before going back. That scene removal will cause the outer chunk to re-execute like coming back to the scene the first time.
I agree that create() should create objects (and destroy() should remove them).
Personally, I would create functions like initLevel() or whatever to “start” or “restart” the level. This could be called from show() or from a custom start point.
Not sure if this is covered in a tutorial or not but if not it should be - as cut scenes add additional unload/reload overhead that is really not required.
Not meaning to hijack here… but @rob I would appreciate some Corona input to my post of 2 days ago that seems to have been missed. Thanks
I rather not use the cut scene method. The thing is, I alter these variables inside modules where there is no show() happening but I did end up creating an init() function in the modules that needed it and then I called those init functions from the main scene. I’ll make sure to call init in the show() function.
Thanks again!
Ps. I’m finding that many times when I create a runtime listener or a new timer, I always need to make sure to cancel them or remove them when leaving the scene. It would be a nice feature if Corona handled all that when a scene is to be destroyed, it would just cancel all timers and remove all listeners that were created in that scene. Just a thought.