Question about modules

Hi, I was recently introduced to the concept of modules, which was writing certain code or functions in a file, and including it wherever you need to in different files to avoid repetition. What I would like to know is, can you use variables that don’t exist in your module, but that do exist in the file that you require the module?

For example, in this code that roaminggamer gave me, the turretM module has no variables declared called turrets or target:

turretM.lua

local lifetime = 5000 local projectileCount = 3 local spread = 7 local bulletSpeed = 300 local turretM = {} local function onCollision( self, event ) if( event.other.myType == "bullet" ) then return false end display.remove(self) return false end function turretM.fire( turrets, target ) for i = 1, #turrets do local turret = turrets[i] local vec = ssk.math2d.diff( turret, target ) local angle0 = ssk.math2d.vector2Angle(vec) local angle = angle0 - spread \* (projectileCount-1)/2 for j = 1, projectileCount do local vec2 = ssk.math2d.angle2Vector( angle, true ) vec2 = ssk.math2d.normalize(vec2) vec2 = ssk.math2d.scale( vec2, bulletSpeed ) local bullet = ssk.display.newImageRect( target.group, turret.x, turret.y, "arrow.png", { size = 20, rotation = angle, collision = onCollision, myType = "bullet", fill = \_Y\_ }, { isSensor = true, radius = 10} ) bullet:toBack() bullet:setLinearVelocity( vec2.x, vec2.y ) bullet.timer = display.remove timer.performWithDelay( lifetime, bullet ) angle = angle + spread end end end return turretM

But, they do exist in main.lua:

require "ssk2.loadSSK" \_G.ssk.init( {} ) require "physics" physics.start( ) physics.setGravity(0,0) local turretM = require "turretM" local turrets = {} local character local background = display.newGroup() local content = display.newGroup() character = display.newCircle(100, 100, 20) physics.addBody(character, "dynamic", {isSensor = true}) local ox = fullw/5 for i = 1, 5 do local turret = ssk.display.newImageRect( content, left + (i-1) \* ox + ox/2, bottom - 75, "rg256.png", { size = 100, fill = \_W\_ } ) turrets[#turrets+1] = turret end local function activateTurret() local target = ssk.display.newCircle( content, character.x, character.y, { size = 40, fill = \_G\_}, { radius = 10 } ) --target.timer = display.remove --timer.performWithDelay(50, target) turretM.fire(turrets, target) end ssk.display.newRect( background, centerX, centerY, { w = fullw, h = fullh, fill = \_GREY\_ } ) timer.performWithDelay(500, activateTurret, 0)

So, is this the case? Can variables in modules not have to exist until they are required in another file?

Corona has several techniques that can be used to pass data between modules.

If you’re using Composer, it has two methods: getVariable() and setVariable(). These variables are saved inside the Composer module and any scene can access or set them.

Next, the method I used the most is non-Global globals. Basically you create a module that’s just a data table, require it in all modules and then access data easily. See: https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Composer also allows you to pass parameters between scenes as part of the composer.gotoScene() call. Inside the new scene, this table of key-value pairs is available as part of the “event” table that’s passed to scene:create() and scene:show().

But since you’re learning modules, and not all modules are scenes, the “Goodbye Globals” method is a pretty good way to manage your scene wide data.

Rob

Corona has several techniques that can be used to pass data between modules.

If you’re using Composer, it has two methods: getVariable() and setVariable(). These variables are saved inside the Composer module and any scene can access or set them.

Next, the method I used the most is non-Global globals. Basically you create a module that’s just a data table, require it in all modules and then access data easily. See: https://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Composer also allows you to pass parameters between scenes as part of the composer.gotoScene() call. Inside the new scene, this table of key-value pairs is available as part of the “event” table that’s passed to scene:create() and scene:show().

But since you’re learning modules, and not all modules are scenes, the “Goodbye Globals” method is a pretty good way to manage your scene wide data.

Rob