storyboard.removeScene() / local variable / storyboard.removeAll() trouble

Hello,

I have a local variable “i” that is given a number which ads up.

If I leave the scene and return to the scene the “i” is not reset to it default value from the top of the code.

Actually none of the code that is outside of the storyboard scene bits is run again.

The module seems to be stored in memory.

Cool, except for the fact that in my main menu I call a storyboard.removeAll() in the enter scene part.

It does not seem to remove the other scene.

So I tried a storyboard.removeScene(sceneName) and if used it works the scene gets cleared. 

Cool, except I get the message 

[lua]

oefening1-iPhone was not purged because it’s view (display group) does not exist. This means it has already been purged or the view was never created.    

[/lua]

So somehow it thinks it didn’t do anything, but the variable “i” has now been reset. SO I get the behaviour I want. Corona just doesn’t seem to know it.

Also shouldn’t storyboard.removeAll() do the same thing? Just without needing to state the scenename?

Also I thought variables that are made local were cleaned by default by storyboard.

using version 2088.

Storyboard scene’s are Lua modules.  That means the storyboard code is doing a require(“yourscenename”) to bring in yourscenename.lua.  If when doing that require, we see that you’ve already loaded the module, we return the pointer to the module to you but do not reload the module.  Any code sitting in the main chunk of that module only executes the very first time  you load it.  This is why “i” isn’t resetting.

When you call storyboard.removeScene(),  you are in effect “unrequiring” the module, it is removed from the table and discarded.  When you goto that scene again, it is required fresh and all of the code in the main chunk is executed again.

I don’t know why you’re getting that message, but if it’s working, it’s doing its job.   You should never purge or remove a scene that is currently on the screen.  I prefer to call removeScene() just before I go to it, or second method is to call remove after I’m in the new scene with a little delay (but its a bit tricker since you have to know where you just came from).

In an ideal world, you would not worry about setting the value of “i” in the main chunk.  You would set it in enterScene() if it’s something that is supposed to be reset every time you enter the scene.  Storyboard was designed to keep old scenes around so you don’t have to waste CPU cycles re-creating them all the time.  Therefore, in the ideal world, you are never sure if createScene() will be called or not.  The only guarantee is that enterScene() and exitScene() gets called.  Though using purgeScene/removeScene and their All siblings will guarantee that createScene() gets called.

Now for the error, is it possible that your enterScene() with the purge/remove is getting called twice?  Perhaps you have a button with a touch handler and it’s not checking for the “began” or “ended” phase?

Rob

Thanks for the explanation.

The main menu scene (it is a scene you always return to after a finishing certain part of the app) calls storyboard.removeAll() in the enter scene portion of its code. 

That way I thought I was sure to remove all previous scenes. There is no button. It just gets called when the scene loads.

I called the removeAll the same way I called the removeScene. I do have 

I actually want to unrequire all my scenes. My apps are relatively simple so loading them from scratch, is done almost instantly.

By unrequiring them I know that the scene is “clean”.

Isn’t storyboard.removeAll() the same as storyboard.removeScene() except you remove all the scenes?

[lua]

function scene:enterScene( event )

    local group = self.view

    -----------------------------------------------------------------------------

    –      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

    -----------------------------------------------------------------------------

storyboard.removeScene(“oefening1-iPhone”)

–]]

end

[/lua]

Storyboard scene’s are Lua modules.  That means the storyboard code is doing a require(“yourscenename”) to bring in yourscenename.lua.  If when doing that require, we see that you’ve already loaded the module, we return the pointer to the module to you but do not reload the module.  Any code sitting in the main chunk of that module only executes the very first time  you load it.  This is why “i” isn’t resetting.

When you call storyboard.removeScene(),  you are in effect “unrequiring” the module, it is removed from the table and discarded.  When you goto that scene again, it is required fresh and all of the code in the main chunk is executed again.

I don’t know why you’re getting that message, but if it’s working, it’s doing its job.   You should never purge or remove a scene that is currently on the screen.  I prefer to call removeScene() just before I go to it, or second method is to call remove after I’m in the new scene with a little delay (but its a bit tricker since you have to know where you just came from).

In an ideal world, you would not worry about setting the value of “i” in the main chunk.  You would set it in enterScene() if it’s something that is supposed to be reset every time you enter the scene.  Storyboard was designed to keep old scenes around so you don’t have to waste CPU cycles re-creating them all the time.  Therefore, in the ideal world, you are never sure if createScene() will be called or not.  The only guarantee is that enterScene() and exitScene() gets called.  Though using purgeScene/removeScene and their All siblings will guarantee that createScene() gets called.

Now for the error, is it possible that your enterScene() with the purge/remove is getting called twice?  Perhaps you have a button with a touch handler and it’s not checking for the “began” or “ended” phase?

Rob

Thanks for the explanation.

The main menu scene (it is a scene you always return to after a finishing certain part of the app) calls storyboard.removeAll() in the enter scene portion of its code. 

That way I thought I was sure to remove all previous scenes. There is no button. It just gets called when the scene loads.

I called the removeAll the same way I called the removeScene. I do have 

I actually want to unrequire all my scenes. My apps are relatively simple so loading them from scratch, is done almost instantly.

By unrequiring them I know that the scene is “clean”.

Isn’t storyboard.removeAll() the same as storyboard.removeScene() except you remove all the scenes?

[lua]

function scene:enterScene( event )

    local group = self.view

    -----------------------------------------------------------------------------

    –      INSERT code here (e.g. start timers, load audio, start listeners, etc.)

    -----------------------------------------------------------------------------

storyboard.removeScene(“oefening1-iPhone”)

–]]

end

[/lua]