A Few Quick Questions On Understanding Modules

This one should be simple for people here to answer, and I know that I should already know this…but…

I’m trying to modularise my code and I need some clarification one one or two things before I really start.

I have a levelSetup.lua module file as follows:

[lua]local M = {}

local _W = display.contentWidth
local _H = display.contentHeight

local function addBorders()

local ground = display.newRect(0,0,_W,20)
ground.x = _W * 0.5; ground.y = _H - (ground.height * 0.5)
ground:setFillColor(0, 0, 0)

local ceiling = display.newRect(0,0,_W,20);
ceiling.x = _W * 0.5; ceiling.y = 0 - (ceiling.height * 0.5)
ceiling:setFillColor(0, 0, 0)

local leftWall = display.newRect(0,0,10,_H)
leftWall.x = 0 - (leftWall.width * 0.5); leftWall.y = _H * 0.5
leftWall:setFillColor(0, 0, 0)

local rightWall = display.newRect(0,0,10,_H)
rightWall.x = _W + (rightWall.width * 0.5); rightWall.y = _H * 0.5
rightWall:setFillColor(0, 0, 0)

end

M.addBorders = addBorders

return M[/lua]
When I call levelSetup.addBackground() from my level1.lua, it returns the four display objects (ground, ceiling, leftWall, rightWall) correctly. My questions are:

_1. When these objects are created and passed to level1.lua, are they created as local display objects there?

  1. Do I have to (or can I) assign each object to a local variable name somehow so that I can the manipulate each later on?

  2. I have a local display group named mainGroup in Level1.lua and when the objects are returned from the levelSetup.addBackground() call, I want to add the four objects to the mainGroup. How is this achieved?_

Sorry for the seemingly ridiculous questions here but I’d rather ask and look a little stupid then spend the next few days implementing things incorrectly, then asking these same questions and then spending another fews days correcting mistakes.
[import]uid: 74503 topic_id: 17880 reply_id: 317880[/import]

Hi insert.code,

It sounds like you’re on the right track. Here’s your answers:

  1. Yes, but only if you put them into local variables (see question 2).

  2. Yes, otherwise they will only be localized to the levelSetup module and you’ll have no control over them (like moving them, deleteing them, etc.). Make sure you “catch” all of them by using a line like…

[lua]local myGround, myCeiling, myLeftWall, myRightWall = levelSetup.addBorders()[/lua]

If you don’t catch them all, then you will have created them without having any reference to them.

  1. Very simple:

[lua]–make sure you declare the mainGroup beforehand
local mainGroup = display.newGroup()

local myGround, myCeiling, myLeftWall, myRightWall = levelSetup.addBorders()

–insert values into group
mainGroup:insert(myGround)
mainGroup:insert(myCeiling)
mainGroup:insert(myLeftWall)
mainGroup:insert(myRightWall)[/lua]
Best,

Matt
W2MD [import]uid: 10211 topic_id: 17880 reply_id: 68259[/import]

Hi Matt, thanks for the reply.

I’ve tried adding the code above, but I seem to be having issues. After assigning the objects to local variables, if I try something simple like:

[lua]myGround.x = 150[/lua]

I get a Runtime error: attempt to index local ‘myGround’ (a nil value)

It also won’t let me insert objects to the mainGroup - again because it’s showing a nil

Any ideas? [import]uid: 74503 topic_id: 17880 reply_id: 68264[/import]

is myGround local variable? If so it`s the issue no?

[import]uid: 89165 topic_id: 17880 reply_id: 68271[/import]

Yes, I assume so. It was set up using the code suggested above:

[lua]local myGround, myCeiling, myLeftWall, myRightWall = levelSetup.addBorders()[/lua]

… [import]uid: 74503 topic_id: 17880 reply_id: 68274[/import]

Sorry.

Let`s wait W2MD as he might know explain.

But for now iNSERT.CODE did you try make them global removing the Local keyword and using W2MF sugested code? If so did it work as you expected?
Cheers,
Rodrigo. [import]uid: 89165 topic_id: 17880 reply_id: 68275[/import]

Can anyone else throw any light on this? I’d be very grateful.

Just thought, it may be worth mentioning that I’m using the new Stroyboard API for scene management. [import]uid: 74503 topic_id: 17880 reply_id: 68385[/import]

At the end of your addBorders() function, add this line:

return ground, ceiling, leftWall, rightWall[/code]The reason why your locals are 'nil' is because your function wasn't returning anything (functions return nil by default). [import]uid: 52430 topic_id: 17880 reply_id: 68421[/import]

Hey, Jonathan, that does it for me! I mean, I’ve been puzzling over how I’m supposed to deal with external module/function. The problem was, I didn’t think of returning more than one object/variable from a function. I didn’t realize I could return multiple of variables/objects like that. Woohoo! My coding method just expanded.

Cheers,
Naomi

P.S. iNSERT.CODE, I’m sorry about jumping in with something that really doesn’t add to your thread. I just couldn’t help myself. [import]uid: 67217 topic_id: 17880 reply_id: 68426[/import]

@Jonathanbeebe - Works, of course (was it ever in doubt?) - it seems so blindingly obvious when someone else points it out?

@Naomi - No problem. I feel the same when the lights turn on. Besides, I think positivity and enthusiasm can only add to a forum and community anyway.

Thanks for everyone’s input on this - I’m (slowly) getting my head around modularising now. [import]uid: 74503 topic_id: 17880 reply_id: 68434[/import]