When I require a file1, I can export the values, data or objects to file2, but file2 can't export to file1
(With "file" I mean a lua file, example, Scene1.lua, Data.lua)
How can I export from file2 to file1 and viceversa?
example code
Data.lua (file1)
--data.lua
local M = {}
local money = 500
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
M.moneyText = moneyText
M.moneyData = money
return M
file2
--file2.lua
local Data = require("data")
local moneyText2 = Data.moneyText
local moneyData = Data.moneyData
local function addSomeValue()
moneyData = moneyData + 1
end
timer.performWithDelay(1000, addSomeValue, 0)
local Data = require("data")
is more or less equivalent to
function chunkFromDataLua()
local M = {}
local money = 500
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
M.moneyText = moneyText
M.moneyData = money
return M
end
local Data = chunkFromDataLua()
Data
now refers to table M
local money = 500
M.moneyData = money
only creates a copy of money and stores that in M. So changing M.moneyData will not affect money. Hence changing Data.moneyData will also not affect money.
In addition to that
local moneyText = display.newText("Money: " .. money,
display.contentCenterX, display.contentCenterY, "calibri", 50)
creates a display object with a static text using the value of money
at that moment. So changing the value of money
after that won't change the content of your text display. You have to change Data.moneyText.text
to do that.