Variables in modularized programming

I want to know if anyone has a method for passing variables between modules.

I know how to call functions between modules, but lets say I have a variable (like a score) that is being constantly updated and I want to be able to use that variable for some purpose in the code of another module. Is this possible?

Thanks!

[import]uid: 102017 topic_id: 19528 reply_id: 319528[/import]

Yes. Check out Ice. Ice helps to store and retrieve data (mainly used for scores) in different modules.

Link: http://developer.anscamobile.com/code/ice
[import]uid: 23689 topic_id: 19528 reply_id: 75377[/import]

You can also use a module to share data between other modules. I did a little test to verify some things I’d read here on the forum.

=== main.lua ===
[lua]print(“main: requiring scores\n”)
local scores = require(“scores”)
print("main: scores.number = " … scores.getNumber());
print(“main: setting scores.number to 1”)
scores.setNumber(1)
print(“main: scores.number = " … scores.getNumber());
print(“main: requiring module2”)
local module2 = require(“module2”)
print(”\nmain: scores.number = " … scores.getNumber());[/lua]

=== module2.lua ===
[lua]print("\nmodule2: requiring scores")
local scores = require(“scores”)
print("module2: scores.number = " … scores.getNumber());
print(“module2: setting scores.number to 2”)
scores.setNumber(2)
print("module2: scores.number = " … scores.getNumber());[/lua]

=== scores.lua ===
[lua]local publicFuntions = {}

local number = 0

local function getNumber()
return number
end
publicFuntions.getNumber = getNumber

local function setNumber(val)
number = val
end
publicFuntions.setNumber = setNumber

return publicFuntions[/lua]

The output is:
main: requiring scores

main: scores.number = 0
main: setting scores.number to 1
main: scores.number = 1
main: requiring module2

module2: requiring scores
module2: scores.number = 1
module2: setting scores.number to 2
module2: scores.number = 2

main: scores.number = 2
What this demonstrated to me was what had been stated here, although I wasn’t sure about the data sharing. Once main.lua had required scores.lua, any additional modules which require scores.lua get the same copy, and any variable which gets updated in scores.lua is visible to all modules which require it as the same variable and value. This is just a simple way to share data, and I don’t intend to compare it with Ice, but I was glad to find that it worked as a “singleton”.
[import]uid: 22076 topic_id: 19528 reply_id: 75383[/import]