Trying to build external modules - help!

Hi there, pros!

I followed the tutorial on external modules and it was not so hard to get it.

But, is it possible to send a display.newGroup that has several of different objects within it?

I tried to modify this code snippet, but it crashes…

module(..., package.seeall)  
  
--------------------------------------------------  
  
function newScoreDisplay( x, y )  
  
 -- Create a text object to hold the score  
 local scoreDisplay = display.newText( "0", 0, 0, "Helvetica-Bold", 18 )  
 scoreDisplay:setTextColor( 255, 255, 255 )  
 scoreDisplay.x = x or 0  
 scoreDisplay.y = y or 0  
  
 -- start core at zero  
 scoreDisplay.scoreCount = 0  
  
 -- Function to change the score  
 function scoreDisplay:change( newScore )  
 self.text = string.format("%08.0f", newScore)  
 end  
  
 -- return the score object to wherever function was called:  
 return scoreDisplay  
end  

Any help, is appreciated !

Regards, Joakim [import]uid: 81188 topic_id: 21260 reply_id: 321260[/import]

Hi there

External modules are very useful, but module(…, package.seeall) was deprecated some time ago and also means that in many cases that you have to define functions as global - which isn’t always a good thing to do.

The preferred way to implement external modules is to create a local table within the module that defines all local variables and functions, and then returns the table to be used locally. Jon Beebe wrote a Blog post on it here:

http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

With regards to returning a display group, this is easily done. Here’s an example:

[lua]-- myExternalModule.lua

local M = {}
local _W = display.contentWidth
local _H = display.contentHeight
– ADD BORDERS

local function addBorders()

– Create display group

local myGroup = display.newGroup()

– Create display objects for borders

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)

– Insert display objects into group

myGroup:insert( ground )
myGroup:insert( ceiling )
myGroup:insert( leftWall )
myGroup:insert( rightWall )

– Return the group

return myGroup

end

M.addBorders = addBorders

return M[/lua]

Then from your main.lua (or other scene), you could then do this:

[lua]-- main.lua

– Require in myExternalModule.lua

local myExternalModule = require(“myExternalModule”)

– Call the addBorders function which returns the display group (myGroup) and assigns it locally

local borders = myExternalModule.addBorders()[/lua]
Hope that all makes sense :slight_smile: [import]uid: 74503 topic_id: 21260 reply_id: 84223[/import]

Great @insert.insert code…

Perfect sample and I will test it directly. One other question, How can I access functions within my main.lua from the module? I saw an sample that created a lua table, and it did work - but theres maybe a easier way?

Best regards, Joakim
[import]uid: 81188 topic_id: 21260 reply_id: 84242[/import]

Ahaaa, it just did the opposite from my module - then I could access the function within the main.lua…bad or good?
Joakim [import]uid: 81188 topic_id: 21260 reply_id: 84244[/import]

Doing it the same way will be fine. Just assign things locally when returning/passing between modules and you’ll be fine.

Glad I could help. [import]uid: 74503 topic_id: 21260 reply_id: 84290[/import]

Question for @iNSERT.CODE: After the following line:

local borders = myExternalModule.addBorders()

how do you then remove myGroup? [import]uid: 104085 topic_id: 21260 reply_id: 107625[/import]

I think I answered my own question. I moved the declaration for myGroup above the local function addBorders() declaration. Then, I created a function called removeBorders() in the external module file that included the line:

myGroup:removeSelf()  

I also assigned the new function to M like this:

M.removeBorders = removeBorders  

In main.lua, I called the function like this:

local removeBorders = myExtMod.removeBorders()  

And, it seemed to work. The borders are created and then they disappear. Hope this helps someone else. Also, if you see something wrong with this, please let me know. [import]uid: 104085 topic_id: 21260 reply_id: 108177[/import]