Custom Functions in Composer

Simple questions:

In the Intro to the Composer API tutorial, and also in the documentation, the author mentions custom functions that are tied to the “scene” object. 

local composer = require( "composer" ) local scene = composer.newScene() ... -- Custom function for resuming the game (from pause state) function scene:resumeGame() --code to resume game end 

Would it be safe to set up ALL custom functions for a scene as above? 

Say the above example is in “scene 1.” Would Composer properly unload the function when “scene 1” is unloaded, or would it persist across all scenes?

It is unclear whether the scene object is unique to each scene, or if it persists across all scenes. 

Table methods exist on the instance and storyboard/Composer createScene creates a new instance every time. So to answer your question: Methods defined on a scene will exist on that scene only.

Thank you for your explanation. I understand everything within the stock Composer functions are on an instance by instance basis, I’m asking about custom functions created outside of said “stock” functions.

I’m asking this because you state that the new instance is created via “scene:create()” and some functions may exist prior to scene:create(). I’d imagine that “composer.newScene()” is what creates the new, unique instance; that’s what I’m trying to determine.

For example:

local composer = require( "composer" ) local scene = composer.newScene() function scene:customFunctionA() --do stuff end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end ...

In other words, does the “customFunctionA” function above only exist for scene 1? 

If so, would it be properly unloaded upon scene destroy?

if you do a composer.removeScene(“SceneName”) that will unload the entire module. From the docs:

composer.removeScene( sceneName, shouldRecycle )

If shouldRecycle is omitted or set to false (default), the scene will be removed entirely, including its scene object.

So yes your customFunctionA will be destroyed when the scene object is and it only exists on the scene where it was implemented not others.

If you set shouldRecycle to true however, then the scene object is not destroyed only the scene.view is. If you call gotoScene or loadScene on that same scene than the first part of your code will not execute inluding your customFunctionA implementation as the scene object already exists with your custom function. Only the scene:create procedure will be called to recreate the view.

I can’t believe I missed that! You were absolutely right. Thank you very much. 

If you do something like this:

function composer:myCoolFunction()

end

That would add a function to the composer object and any composer based scene would have access to that function.  In this case if you removeScene(), the module where you define it, that code would in theory go away.

However, when you use our scene template:

local scene = composer.newScene()

The scene needs to be declared as a local (so it doesn’t stomp on other scenes).  Since its local, it’s only visible to that scene (and any overlay scene, since the parent scene object is passed to the overlay).

So only that scene can access that code anyway. 

Rob

@Rob,

Yeah, adding to the composer object sounds risky, as one could easily dispose of the originating/defining module.  

Yeah, I was hoping/figuring that would be the case but, in my mind, I kept treating the scene object as a global and thought I’d somehow clash with other scenes or orphan code in the process. 

Thank you for your response.

Table methods exist on the instance and storyboard/Composer createScene creates a new instance every time. So to answer your question: Methods defined on a scene will exist on that scene only.

Thank you for your explanation. I understand everything within the stock Composer functions are on an instance by instance basis, I’m asking about custom functions created outside of said “stock” functions.

I’m asking this because you state that the new instance is created via “scene:create()” and some functions may exist prior to scene:create(). I’d imagine that “composer.newScene()” is what creates the new, unique instance; that’s what I’m trying to determine.

For example:

local composer = require( "composer" ) local scene = composer.newScene() function scene:customFunctionA() --do stuff end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- Initialize the scene here. -- Example: add display objects to "sceneGroup", add touch listeners, etc. end ...

In other words, does the “customFunctionA” function above only exist for scene 1? 

If so, would it be properly unloaded upon scene destroy?

if you do a composer.removeScene(“SceneName”) that will unload the entire module. From the docs:

composer.removeScene( sceneName, shouldRecycle )

If shouldRecycle is omitted or set to false (default), the scene will be removed entirely, including its scene object.

So yes your customFunctionA will be destroyed when the scene object is and it only exists on the scene where it was implemented not others.

If you set shouldRecycle to true however, then the scene object is not destroyed only the scene.view is. If you call gotoScene or loadScene on that same scene than the first part of your code will not execute inluding your customFunctionA implementation as the scene object already exists with your custom function. Only the scene:create procedure will be called to recreate the view.

I can’t believe I missed that! You were absolutely right. Thank you very much. 

If you do something like this:

function composer:myCoolFunction()

end

That would add a function to the composer object and any composer based scene would have access to that function.  In this case if you removeScene(), the module where you define it, that code would in theory go away.

However, when you use our scene template:

local scene = composer.newScene()

The scene needs to be declared as a local (so it doesn’t stomp on other scenes).  Since its local, it’s only visible to that scene (and any overlay scene, since the parent scene object is passed to the overlay).

So only that scene can access that code anyway. 

Rob

@Rob,

Yeah, adding to the composer object sounds risky, as one could easily dispose of the originating/defining module.  

Yeah, I was hoping/figuring that would be the case but, in my mind, I kept treating the scene object as a global and thought I’d somehow clash with other scenes or orphan code in the process. 

Thank you for your response.