Reputation: 903
Here is a demo I modified to remove the module(..., package.seeall) statement. It works great, and I want to use the same idea in a Corona sdk project. I want to pass a value to an existing variable that I created in the demo. Thanks for any advice.
main.lua--------------------------------------------------
-- Load external library (should be in the same folder as main.lua)
local testlib = require("testlib")
testlib.testvar = 100 -- Trying to change the testvar value in external module
-- cache same function, if you call more than once
local hello = testlib.hello
-- now all future invocations are "fast"
hello()
-- This all works fine, but I need to change the value of testvar.
testlib.lua -----------------------------------------------------
local M = {}
local testvar = 0 -- I need to change the value of this variable as well as others later.
print("testvar=",testvar)
local function hello()
print ("Hello, module")
end
M.hello = hello
return M
Upvotes: 0
Views: 530
Reputation: 8497
In this case Your local testvar
is private variable for Your module (testlib.lua
).
You need to provide some setters/getters for that private variable.
Basic example would be adding this to Your testlib.lua
:
function setter(new_val)
test_var = new_val
end
function getter()
return test_var
end
M.set = setter
M.get = getter
Now, You can use testlib.set("some new value..")
and print(testlib.get())
in Your main.lua
to operate the value of testvar
variable.
Upvotes: 1