Best way to use local variables in the main chunk" area of a Storyboard module?

Hi guys,

First ,I need to admit that I am VERY new to Storyboard (used Director before) One thing I used to do is preset all my local variables at the top of a scene (main chunk) I am trying to do the same think with Storyboard but it seems that when I re-enter a scene the second time, that section seems to be ignored. For instance I have something like (endScreen scene):

– recall the amount of gems from  GGData box

local gemNB = settings:get( “gemNB” ) or 0

Then later on the CreateScene () function, I display the gem amount (gemNB) in a text object. It works the first time (the gem amount is taken from the file “settings” I then switch to the game screen and play the game and get more gems which are saved in the settings. BUT then I when re-enter the endScreen, the gem text display the previous gem amount not the new (higher one) I put some print and I can see the "local gemNB = settings:get( “gemNB” ) or 0 " is not revisited the second time.

I guess my question is: How to deal with those “main chunk” variables? Do I need to put them in the enterScreen() instead?

Thanks again and sorry for the newbie question. It is true that after 2+ years with Corona, I am still a newbie in term of Storyboard:)

Mo

Hey, Mo, you might want to try declaring the variable up top and then assign value to it in enterScene function, like so:

[lua]

local gemNB – this comes up top

function scene:enterScene( event )

    gemNB = settings:get( “gemNB” ) or 0

end

[/lua]

Naomi

WOW! it worked. Thanks again, you always come to the rescue :slight_smile:

So if I understand, I cannot count on local variables in the “main chunk” to be available during the scene? That’s seems strange to me coming from Director. I really liked when I was able to just put all my variables at the top of the module and have access to them during the all life of the module (scene)!

By the way I am following your advice and using tx.name1, bt.name1 and img.name1,2,3 to avoid getting into the 200 variables limit :slight_smile: Thanks for that tip.

Mo

Yeah, I’ve run into the same issue when I first started using storyboard.  I remember reading about the reason why we want to assign value in enterScene function, but I don’t remember where I read it…

I don’t think it’s about local variables in the “main chunk” not being available during the scene, but rather, it’s something to do with what section of the code is being processed when re-entering the scene.

Anyhow, I’m glad I could help.

Naomi

Read the comments about each of the Storyboard Scene Events.

Hope this helps

[lua]



– mainmenu.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()


– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
    local screenGroup = self.view
    

    –  print( “\n1: createScene event”)
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    print( “1: enterScene event” )
    
    – == If there was a previous screen like the help - totally remove it from memory

    — But no longer needed if you set storyboard.purgeOnSceneChange = true

    if storyboard.getPrevious() ~= nil then
        --print("previous screen in mainmenu " …  storyboard.getPrevious());
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end
   
end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
    
    print( “1: exitScene event” )
   
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
    print( “((destroying scene 1’s view))” )
end


– Called BEFORE scene has moved onscreen:
–      This event requires build 2012.782 or later.

function scene:willEnterScene( event )
    local screenGroup = self.view
    
end


– Called AFTER scene has finished moving offscreen:
–This event requires build 2012.782 or later.

function scene:didExitScene( event )
    local screenGroup = self.view
end


– Called if/when overlay scene is displayed via storyboard.showOverlay()
–This event requires build 2012.797 or later.

function scene:overlayBegan( event )
    local screenGroup = self.view
    local overlay_name = event.sceneName  – name of the overlay scene
end


– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
–This event requires build 2012.797 or later.

function scene:overlayEnded( event )
    local screenGroup = self.view
    local overlay_name = event.sceneName  – name of the overlay scene
end

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “willEnterScene” event is dispatched before scene transition begins
scene:addEventListener( “willEnterScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “didExitScene” event is dispatched after scene has finished transitioning out
scene:addEventListener( “didExitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )

– “overlayBegan” event is dispatched when an overlay scene is shown
scene:addEventListener( “overlayBegan”, scene )

– “overlayEnded” event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( “overlayEnded”, scene )

return scene

[/lua]

Larry Meadows

DoubleSlashDesign.com / TinyTapApps.com

Thank you guys! Yes thank you Larry for that. I forgot about those great comments on the storyboard template. Still I am really not sure where to set all my local variables to be used in the enterScene (which I am assuming is where the game loop has to reside) Like I said before, with Director I used to simply pre-declare all my variables and functions at top of the files ("main chunk) and done with it. It seems that with Storyboard, it does not work that way. I may have to looking into the code exchange and see if I can find a good example of the use of storyboard to give me an idea how app are structure in term of variables declaration for instance.

In any event, THANK YOU to both of you. I really appreciated the speed of the response since i am kind of stuck until i better understand what is going on with Storyboard. I am also looking at the terrific tutorials here about storyboard (unfortunately nothing yet about variables declaration)

Mo

Create your variables at the top of the file.

You can preset them to some value if you like just know that once the scene is created the first time the LUA file stays in memory and that bit of code is not executed again.

Now that is only true if you do not remove the scene from memory by navigating to another scene.

There are 2 ways you can do this

something new that Corona has added is set the new property  purgeOnSceneChange = true

[lua]

– load first scene

local storyboard = require( “storyboard” )
storyboard.purgeOnSceneChange = true    –

storyboard.gotoScene( “mainmenu”, “fade”, 100 )
[/lua]

or  using the code below

[lua]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    – == If there was a previous screen like the help - totally remove it from memory
    if storyboard.getPrevious() ~= nil then
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end

end.

[/lua]

Both items should force your scene to be totally removed and reloaded each time you navigate to and from the scenes option 1 is new, I just happened to see it in the documentation so I have not used it yet.

I usually put option 2 in all my storyboard scenes and it seems to work flawlessly…

Hope this helps.

Larry

Here is why there is a difference between Director and Storyboard.

When Lua “require”'s a module, the main chunk is executed once and only once until the module is unloaded completely.  This is a very important feature as it’s how modules retain their data and state throughout your app. Both Directory and storyboard behave this way.  The difference is when Director does a complete unload after it changes the scene.  That it is does an un-require on the scene so the next it it loads it, you get a fresh start.

Storyboard treats scenes a bit differently in that it want’s to keep the scene required and manage it’s assets as memory dictates.  When you leave a scene and it gets purged (either by you purging it using storyboard.purgeScene() or storyboard.purgeAll() or by the system running low on memory)  it just removes assets, it does not un-require the scene.

That is what…  storyboard.removeScene() does.   It un-requires the scene.  If you use removeScene() then your main chunk will re-load from scratch each time.

But this creates extra load on the app to have to dump and reload the scene every time.  You are better off initializing variables in createScene or enterScene where appropriate.

Thank you guys! I appreciate the help. @Larry: so both options would make the app start from scratch at every scene changes. This should allow me to put all my local variables at the top of the module. Correct? @Rob: cool! If I decide to ease up the load on the memory (so using purge instead of remove scene) where is it best to put all those variables use in the game loop? (Create or enter scene) OR does it depend? I will think as Naomi suggested, enter scene since to be best since create scene event will not be called if the scene exist already. Sorry for all these newbie question :frowning: THANKS for taking the time. Mo

It depends.

Cool! Mo

OK, there is still one thing I still don’t understand about all of this.

Suppose I create a local foward reference called ‘enemy’ at the top of a storyboard scene.  Then I assign to enemy a display object.  Then upon exiting the scene I call “enemy:removeSelf().”

Here’s what I don’t get: if I were to then set enemy to nil to clear the lua table, then upon re-entering the scene, enemy would no longer exist (not at all, but particularly not as a local varaible), correct?  Then if I were to once again assign a display object to enemy, I would be creating enemy as a global variable, which would be very bad.

If i do not set enemy to nil, then I carry on with enemy stuck in memory holding a lua table (not as bad as above, but not desirable).  Also, this guide (http://www.coronalabs.com/blog/2013/04/02/cleaning-up-display-objects-andlisteners/)  seems to suggest that if I were to overwrite enemy then I would lose some memory that cannot be regained. (or is this specific to global variables?)

So what is the best practice here?

The variable still exists.  It just contains the value nil instead of a pointer to a table.  That variable is still local.  Still scoped in your module’s main chunk.

Of course  you have to create a new enemy and assign it to your enemy variable.

Awesome, thank you.

Hey, Mo, you might want to try declaring the variable up top and then assign value to it in enterScene function, like so:

[lua]

local gemNB – this comes up top

function scene:enterScene( event )

    gemNB = settings:get( “gemNB” ) or 0

end

[/lua]

Naomi

WOW! it worked. Thanks again, you always come to the rescue :slight_smile:

So if I understand, I cannot count on local variables in the “main chunk” to be available during the scene? That’s seems strange to me coming from Director. I really liked when I was able to just put all my variables at the top of the module and have access to them during the all life of the module (scene)!

By the way I am following your advice and using tx.name1, bt.name1 and img.name1,2,3 to avoid getting into the 200 variables limit :slight_smile: Thanks for that tip.

Mo

Yeah, I’ve run into the same issue when I first started using storyboard.  I remember reading about the reason why we want to assign value in enterScene function, but I don’t remember where I read it…

I don’t think it’s about local variables in the “main chunk” not being available during the scene, but rather, it’s something to do with what section of the code is being processed when re-entering the scene.

Anyhow, I’m glad I could help.

Naomi

Read the comments about each of the Storyboard Scene Events.

Hope this helps

[lua]



– mainmenu.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()


– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
    local screenGroup = self.view
    

    –  print( “\n1: createScene event”)
end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    print( “1: enterScene event” )
    
    – == If there was a previous screen like the help - totally remove it from memory

    — But no longer needed if you set storyboard.purgeOnSceneChange = true

    if storyboard.getPrevious() ~= nil then
        --print("previous screen in mainmenu " …  storyboard.getPrevious());
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end
   
end

– Called when scene is about to move offscreen:
function scene:exitScene( event )
    
    print( “1: exitScene event” )
   
end

– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
    print( “((destroying scene 1’s view))” )
end


– Called BEFORE scene has moved onscreen:
–      This event requires build 2012.782 or later.

function scene:willEnterScene( event )
    local screenGroup = self.view
    
end


– Called AFTER scene has finished moving offscreen:
–This event requires build 2012.782 or later.

function scene:didExitScene( event )
    local screenGroup = self.view
end


– Called if/when overlay scene is displayed via storyboard.showOverlay()
–This event requires build 2012.797 or later.

function scene:overlayBegan( event )
    local screenGroup = self.view
    local overlay_name = event.sceneName  – name of the overlay scene
end


– Called if/when overlay scene is hidden/removed via storyboard.hideOverlay()
–This event requires build 2012.797 or later.

function scene:overlayEnded( event )
    local screenGroup = self.view
    local overlay_name = event.sceneName  – name of the overlay scene
end

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “willEnterScene” event is dispatched before scene transition begins
scene:addEventListener( “willEnterScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “didExitScene” event is dispatched after scene has finished transitioning out
scene:addEventListener( “didExitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )

– “overlayBegan” event is dispatched when an overlay scene is shown
scene:addEventListener( “overlayBegan”, scene )

– “overlayEnded” event is dispatched when an overlay scene is hidden/removed
scene:addEventListener( “overlayEnded”, scene )

return scene

[/lua]

Larry Meadows

DoubleSlashDesign.com / TinyTapApps.com

Thank you guys! Yes thank you Larry for that. I forgot about those great comments on the storyboard template. Still I am really not sure where to set all my local variables to be used in the enterScene (which I am assuming is where the game loop has to reside) Like I said before, with Director I used to simply pre-declare all my variables and functions at top of the files ("main chunk) and done with it. It seems that with Storyboard, it does not work that way. I may have to looking into the code exchange and see if I can find a good example of the use of storyboard to give me an idea how app are structure in term of variables declaration for instance.

In any event, THANK YOU to both of you. I really appreciated the speed of the response since i am kind of stuck until i better understand what is going on with Storyboard. I am also looking at the terrific tutorials here about storyboard (unfortunately nothing yet about variables declaration)

Mo

Create your variables at the top of the file.

You can preset them to some value if you like just know that once the scene is created the first time the LUA file stays in memory and that bit of code is not executed again.

Now that is only true if you do not remove the scene from memory by navigating to another scene.

There are 2 ways you can do this

something new that Corona has added is set the new property  purgeOnSceneChange = true

[lua]

– load first scene

local storyboard = require( “storyboard” )
storyboard.purgeOnSceneChange = true    –

storyboard.gotoScene( “mainmenu”, “fade”, 100 )
[/lua]

or  using the code below

[lua]

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    – == If there was a previous screen like the help - totally remove it from memory
    if storyboard.getPrevious() ~= nil then
        storyboard.purgeScene(storyboard.getPrevious())
        storyboard.removeScene(storyboard.getPrevious())
    end

end.

[/lua]

Both items should force your scene to be totally removed and reloaded each time you navigate to and from the scenes option 1 is new, I just happened to see it in the documentation so I have not used it yet.

I usually put option 2 in all my storyboard scenes and it seems to work flawlessly…

Hope this helps.

Larry