How To Access Local Variables/ Functions From Scene Overlay?

This might sound like an obvious question, but how do you call variables and functions that are local to an overlay from the current scene? I know you can pass variables from the current scene to the overlay, but how do you pass variables from the overlay to the current scene? It works if the variables are global, but I know global variables are bad so how then do you pass local variables? 

I tried making the function from

local function myFunction()

to

function scene.myFunction()

and then accessing it like this

composer.showOverlay( "sceneOverlay" ) local sceneOverlay = composer.getScene( "sceneOverlay" ) sceneOverlay.myFunction()

but I don’t see this explained anywhere so is this the right way of doing things? And will the overlay’s scene:destroy still clear the function? I tested this and for some reason I was still able to call the sceneOverlay.myFunction() even though I did composer.hideOverlay() with recycling not true. Can someone please explain.

Avoid the issue.  Store or exchange the values via a commonly required module.

Sorry I don’t have a better answer, but I must admit I’ve never tried reaching into a overlay.  I rarely reach into scenes that way and rarely use overlays, so you can see how that combines to never doing this. :slight_smile:

Having said that, the overlay is nothing more than a scene so you should be able to reach in and get any function or variable you have assigned to the scene object.  So, don’t make the variables ‘local’.  Make them fields on the scene objects.

local composer = require( "composer" ) local scene = composer.newScene() -- Variable field (2 out of 3 are right) scene.myVar = {} scene.myVar.age = 100 scene.myVar.sex = "male" scene.myVar.answer = "awesome" -- Function field scene.myFunc = function () end -- myVar and myFunc can both be accessed externally.

Ah okay, so assigning to the scene object is the right way? but then why when I do

composer.hideOverlay()

I can still access the overlay function, but when I add

function scene:destroy( event ) scene.myFunc = nil end

only then does the function become nil. Why does that happen? Shouldn’t the function have been destroyed along with the other scene objects without me having to explicitly state?

Ohh I think I figured it out. The scene.view is what gets destroyed, so when I make a function 

scene.function

it should actually be 

scene.view.function

haha I think that was the problem.

^Cool

Actually this is a really cool thing that Composer does with Overlays.

Back in the Storyboard days, when you closed an overlay, it would trigger the parent’s hideScene function as well as the overlay scene’s hideScene function (or whatever they were named). Composer only calls scene:hide() for the overlay. The parent scene doesn’t get any notification that the overlay is closing.  So we added a feature.

In both scene:show() and scene:hide() and possibly scene:create(), the event table includes a member called parent which just so happens to be the actual “scene” object from the parent.

This means you can do things in the parent like:

local scene = composer.newScene() scene.currentLevel = 10 function scene:pause()      -- code to pause the scene end function scene:resume()      -- code to resume the scene end

Then inside the overlay’s scene:show() during the will phase:

function scene:show( event )      local parent = event.parent      local phase = event.phase      if "will" == phase then          parent:pause()      else  -- did phase           print( parent.currentLevel )      end end function scene:hide( event )     local parent = event.parent     local phase = event.phase     if "did" == phase then         parent:resume()     end end

I’m using a similar technique in the game I’m making. I have modules/classes for my enemy ships. They have their own code for managing taking damage and handling their own death and cleanup. It they are killed they use Composer to get the scene object of the game and call a function I’ve attached to the scene object to handle updating the score.

Now you can’t access “local” variables and functions this way. You would have to add it to the scene object, then the overlay would have access to it.

Rob

Avoid the issue.  Store or exchange the values via a commonly required module.

Sorry I don’t have a better answer, but I must admit I’ve never tried reaching into a overlay.  I rarely reach into scenes that way and rarely use overlays, so you can see how that combines to never doing this. :slight_smile:

Having said that, the overlay is nothing more than a scene so you should be able to reach in and get any function or variable you have assigned to the scene object.  So, don’t make the variables ‘local’.  Make them fields on the scene objects.

local composer = require( "composer" ) local scene = composer.newScene() -- Variable field (2 out of 3 are right) scene.myVar = {} scene.myVar.age = 100 scene.myVar.sex = "male" scene.myVar.answer = "awesome" -- Function field scene.myFunc = function () end -- myVar and myFunc can both be accessed externally.

Ah okay, so assigning to the scene object is the right way? but then why when I do

composer.hideOverlay()

I can still access the overlay function, but when I add

function scene:destroy( event ) scene.myFunc = nil end

only then does the function become nil. Why does that happen? Shouldn’t the function have been destroyed along with the other scene objects without me having to explicitly state?

Ohh I think I figured it out. The scene.view is what gets destroyed, so when I make a function 

scene.function

it should actually be 

scene.view.function

haha I think that was the problem.

^Cool

Actually this is a really cool thing that Composer does with Overlays.

Back in the Storyboard days, when you closed an overlay, it would trigger the parent’s hideScene function as well as the overlay scene’s hideScene function (or whatever they were named). Composer only calls scene:hide() for the overlay. The parent scene doesn’t get any notification that the overlay is closing.  So we added a feature.

In both scene:show() and scene:hide() and possibly scene:create(), the event table includes a member called parent which just so happens to be the actual “scene” object from the parent.

This means you can do things in the parent like:

local scene = composer.newScene() scene.currentLevel = 10 function scene:pause()      -- code to pause the scene end function scene:resume()      -- code to resume the scene end

Then inside the overlay’s scene:show() during the will phase:

function scene:show( event )      local parent = event.parent      local phase = event.phase      if "will" == phase then          parent:pause()      else  -- did phase           print( parent.currentLevel )      end end function scene:hide( event )     local parent = event.parent     local phase = event.phase     if "did" == phase then         parent:resume()     end end

I’m using a similar technique in the game I’m making. I have modules/classes for my enemy ships. They have their own code for managing taking damage and handling their own death and cleanup. It they are killed they use Composer to get the scene object of the game and call a function I’ve attached to the scene object to handle updating the score.

Now you can’t access “local” variables and functions this way. You would have to add it to the scene object, then the overlay would have access to it.

Rob