Best place to put functions in Composer scene?

Hey all, just wanted to double-check myself for best-practice here.

In a composer scene, where is the best place to put my functions like handleButtonEvent, etc. to make them universally accessible?

  1. In the forward declaration section under something like local functionname()?

  2. At the top of the scene:show function?

  3. Someplace else or another way?

FYI, some of these functions need to be available to various parts of the scene, so I was just curious what the defined best-practice was here.

Thanks!

I try and structure my code to avoid forward-declarations of functions. So if I have a touch listener that will be above the code that creates the object, and if the touch listener calls another function that will be above the listener. Any generic functions that could be called from a variety of places will go right at the top, or if they get called by multiple scenes, in a separate module. Sometimes it’s unavoidable though.

My general structure is something like this:

Import libraries & modules

Declare scene variables & objects

Generic functions

Specific functions

Touch & other listeners

GameLoop

Creation functions (called from scene:create if lots to do) 

Composer functions

Nick has some great advice. Since Lua is a single pass compiler, you have to to declare things before you can use them. Lua also is very “scope” driven.  That is, if you declare a something inside a function, it’s only available to that function. It’s always best to write your functions in the main chunk before any of the Composer event handling functions.

A great example that shows orginization is our latest full app template: Match 3 Space RPG. See https://marketplace.coronalabs.com/asset/match-3-space-rpg

Rob

  1.  I move most of my code into modules and call that code from scenes.

(I mean that I like my scene files to be very small and light-weight).

  1. I follow the format shown in this file:

http://github.com/roaminggamer/RG_FreeStuff/blob/master/AskEd/2016/10/redcross/scenes/_template.lua

Note: I go one step further and split the will/did phases of show(), hide() into separate methods.

local composer = require( "composer" ) local scene = composer.newScene() ---------------------------------------------------------------------- -- Locals & Forward Declarations ---------------------------------------------------------------------- ---------------------------------------------------------------------- -- Scene Methods ---------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:willShow( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:didShow( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:willHide( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:didHide( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- -- Custom Scene Functions/Methods ---------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line --------------------------------------------------------------------------------- -- This code splits the "show" event into two separate events: willShow and didShow -- for ease of coding above. function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willShow( event ) elseif( willDid == "did" ) then self:didShow( event ) end end -- This code splits the "hide" event into two separate events: willHide and didHide -- for ease of coding above. function scene:hide( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willHide( event ) elseif( willDid == "did" ) then self:didHide( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

I try and structure my code to avoid forward-declarations of functions. So if I have a touch listener that will be above the code that creates the object, and if the touch listener calls another function that will be above the listener. Any generic functions that could be called from a variety of places will go right at the top, or if they get called by multiple scenes, in a separate module. Sometimes it’s unavoidable though.

My general structure is something like this:

Import libraries & modules

Declare scene variables & objects

Generic functions

Specific functions

Touch & other listeners

GameLoop

Creation functions (called from scene:create if lots to do) 

Composer functions

Nick has some great advice. Since Lua is a single pass compiler, you have to to declare things before you can use them. Lua also is very “scope” driven.  That is, if you declare a something inside a function, it’s only available to that function. It’s always best to write your functions in the main chunk before any of the Composer event handling functions.

A great example that shows orginization is our latest full app template: Match 3 Space RPG. See https://marketplace.coronalabs.com/asset/match-3-space-rpg

Rob

  1.  I move most of my code into modules and call that code from scenes.

(I mean that I like my scene files to be very small and light-weight).

  1. I follow the format shown in this file:

http://github.com/roaminggamer/RG_FreeStuff/blob/master/AskEd/2016/10/redcross/scenes/_template.lua

Note: I go one step further and split the will/did phases of show(), hide() into separate methods.

local composer = require( "composer" ) local scene = composer.newScene() ---------------------------------------------------------------------- -- Locals & Forward Declarations ---------------------------------------------------------------------- ---------------------------------------------------------------------- -- Scene Methods ---------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:willShow( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:didShow( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:willHide( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:didHide( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- -- Custom Scene Functions/Methods ---------------------------------------------------------------------- --------------------------------------------------------------------------------- -- Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line --------------------------------------------------------------------------------- -- This code splits the "show" event into two separate events: willShow and didShow -- for ease of coding above. function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willShow( event ) elseif( willDid == "did" ) then self:didShow( event ) end end -- This code splits the "hide" event into two separate events: willHide and didHide -- for ease of coding above. function scene:hide( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willHide( event ) elseif( willDid == "did" ) then self:didHide( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene