Newbie question about modules

Hi, I use Corona for a short time , I’m studying this framework by creating small games and solving the problems that encounter. My programming approach is procedural, I don’t use class, ecc…

I did some research on the web and on the forums, read some manuals, but I am not yet clear on the proper use of the modules.

In my projects I use Composer to divide the game into scenes. In addition to tidy the projects and not to have scene lua files long thousands of line, I got used to group the functions used by each scene into some modules.

At the beginning of each scene I use “require” to call modules. The modules do not contain more that the functions one after the other.

I’ve noticed that I can not hold functions as a local within the modules but actually have to be global.
I can’t put local functions in modules? What is the correct way to use modules? I have to remove module in scene:destroy or it is done automatically?

Example:
[lua]


– menu.lua


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

require(“Example”)



function scene:create( event )

local sceneGroup = self.view

– Called when the scene’s view does not exist.

– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

end


function scene:show( event )
local sceneGroup = self.view
local phase = event.phase

if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen

– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.

–//ALL THIS FUNCTIONS ARE IN Example MODULE//–
gameExample()

example()

exampleSwipe()

exampleTilt()

end
end


function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase

if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end


function scene:destroy( event )
local sceneGroup = self.view

– Called prior to the removal of scene’s “view” (sceneGroup)

– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc.

sceneGroup:removeSelf( )
sceneGroup=nil

end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene
[/lua]

and this is Example module:

[lua]

function gameExample()
–CODE–
end

function example()
–CODE–
end

function exampleSwipe()
–CODE–
end

function exampleTilt()
–CODE–
end

[/lua]

Sorry for Google Translate

The way I do this is to have a table that holds my functions:

local M = {} -- M is for module.... function M.gameExample()       --CODE-- end function M.example()      --CODE-- end   return M

Then in your scenes:

local example = require(“Example”)

– and to call the function:

example.gameExample()

Rob

Clear, thanks Rob! I have to remove the modules manually in Scene:destroy?

No, you do not.  Code doesn’t really use much in the way of memory.  Lua is efficient in that if you require the same module in multiple other modules, it only loads it once anyway.  After the first “require”, the others are just providing a local reference in that scene to the module.

If for some reason your module was huge and you felt you needed to do it, you would probably want to un-require it in the scene:destroy() call, but you really don’t have to do this unless for some reason your scene was allocating a ton of memory from display objects and audio that you were not keeping a handle too so that they can be removed later.

Rob

Perfect, thanks again :slight_smile:

The way I do this is to have a table that holds my functions:

local M = {} -- M is for module.... function M.gameExample()       --CODE-- end function M.example()      --CODE-- end   return M

Then in your scenes:

local example = require(“Example”)

– and to call the function:

example.gameExample()

Rob

Clear, thanks Rob! I have to remove the modules manually in Scene:destroy?

No, you do not.  Code doesn’t really use much in the way of memory.  Lua is efficient in that if you require the same module in multiple other modules, it only loads it once anyway.  After the first “require”, the others are just providing a local reference in that scene to the module.

If for some reason your module was huge and you felt you needed to do it, you would probably want to un-require it in the scene:destroy() call, but you really don’t have to do this unless for some reason your scene was allocating a ton of memory from display objects and audio that you were not keeping a handle too so that they can be removed later.

Rob

Perfect, thanks again :slight_smile: