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]