Module question: Which style is better, using a local variable and a function call to return its value, or...

When using modules which style is better? Using a local variable and a function call to return its value, or using a global variable which is localized to its module?

  
--------------------------------------------------------------------------------------------------------------------------  
--------------------------------------------------------------------------------------------------------------------------  
-- TYPE1: playfield is a local variable in setupGame.lua and main.lua gets its value via a function call. e.g. local board = TOTsF.playField()  
-----------------------------------------  
-- setupGame.lua  
  
module(..., package.seeall)  
  
local playfield  
  
function setupPlayField()  
 playfield = display.newImage("GameBoard.png")  
 playfield.y = 240  
end  
  
function playField()  
 return playfield  
end  
  
--------------------------------------------------------------------------------------------------------------------------  
-- main.lua  
  
local TOTsF = require("setupGame")  
  
function putPieceOnBoard(x, y)  
 -- get position of playfield on screen  
 local board = TOTsF.playField()  
 print("stageBounds xMin: ".. board.stageBounds.xMin .."; xMax: ".. board.stageBounds.xMax)  
 print("stageBounds yMin: ".. board.stageBounds.yMin .."; yMax: ".. board.stageBounds.yMax)  
end  
  
--------------------------------------------------------------------------------------------------------------------------  
--------------------------------------------------------------------------------------------------------------------------  
--------------------------------------------------------------------------------------------------------------------------  
-- TYPE2: playfield is a global variable defined in setupGame.lua and main.lua gets its value via a module call. e.g. TOTsF.playField  
-----------------------------------------  
-- setupGame.lua  
  
module(..., package.seeall)  
  
function setupPlayField()  
 playfield = display.newImage("GameBoard.png")  
 playfield.y = 240  
end  
  
-----------------------------------------  
-- main.lua  
  
local TOTsF = require("setupGame")  
  
function putPieceOnBoard(x, y)  
 -- get position of playfield on screen  
 print("stageBounds xMin: ".. TOTsF.playField.stageBounds.xMin .."; xMax: ".. TOTsF.playField.stageBounds.xMax)  
 print("stageBounds yMin: ".. TOTsF.playField.stageBounds.yMin .."; yMax: ".. TOTsF.playField.stageBounds.yMax)  
end  
--------------------------------------------------------------------------------------------------------------------------  
--------------------------------------------------------------------------------------------------------------------------  
  

[import]uid: 295 topic_id: 1376 reply_id: 301376[/import]

hi,

did you get any feedback on this?

regards
j [import]uid: 6645 topic_id: 1376 reply_id: 10573[/import]

I prefer modules like this:

[lua]–this is somethingsBuilder.lua file
module(…, package.seeall)

local gameSomethingsConfigurationTable={scene1={}, scene2={}}

function getSomething(info)
–preparation code (everything is local)
local something = – assign what you want
return something
end[/lua]

that can be called like this:

[lua]-- this is main.lua
builder = require(“somethingsBuilder”)[/lua]

[lua]–this is sceneX.lua
local something = builder.getSomething(info)[/lua]

[import]uid: 7356 topic_id: 1376 reply_id: 10588[/import]

see what you think of this. I’m “synthesizing” a getter/setter, although you could do it as a function if you only wanted a getter for instance

[import]uid: 6645 topic_id: 1376 reply_id: 10608[/import]

Seems fine to me!

I guess you return “this” at the end of new().
[import]uid: 7356 topic_id: 1376 reply_id: 10613[/import]

seems it’s a little bit slower than carlos/tim’s methods here
http://developer.anscamobile.com/forum/2010/11/09/display-objects-custom-component-question

am investigating [import]uid: 6645 topic_id: 1376 reply_id: 10621[/import]

@jmp909, which part seems slower? [import]uid: 26 topic_id: 1376 reply_id: 11467[/import]

hi walter,

see my post here
http://developer.anscamobile.com/forum/2010/11/09/display-objects-custom-component-question#comment-10615

my slower method is the one above in the same thread, where i’m not using a metatable.
http://developer.anscamobile.com/forum/2010/11/09/display-objects-custom-component-question#comment-10606

this latter one I think is similar to your movieclip.lua where an object is created as a function, that contains more functions within it.

I’m guessing it is slower because when I call new() with this method, then it creates all the functions as well inside that object so the object takes longer to create… Whereas with the metatable method these functions do not exist on the object on creation, but are performed using a lookup with the metatable index when the function is called?

is this correct?

thanks
J [import]uid: 6645 topic_id: 1376 reply_id: 11480[/import]