Hi,
I’m trying to break my code into modules but I am strugglig with passing the data from a file to another.
I read the Data Module, scopedocumentation and this thread but still having issues.
My code is divided in the following files:
main.lua
-- some code local toolbars = require("toolbar") toolbars.loadBottomToolbar() -- more code...
toolbar.lua
local M = {} local globalData = require( "globalData" ) local locMoveButton= globalData.moveButton function M.loadBottomToolbar() locMoveButton= widget.newButton( { -- ... } ) locMoveButton.x = 400 local modes = require("modes") locMoveButton:addEventListener("tap", modes.moveMode) end return M
modes.lua
local M = {} local globalData = require( "globalData" ) local locMoveButton= globalData.moveButton function M.exitMode() locMoveButton.alpha = 1 -- more code end return M
globalData.lua
-- Pseudo-global space local globalData = {} globalData.moveButton = nil return globalData
In this example locMoveButton is working fine in toolbar but once it is called again in modes.lua it has ben reset to nil by globalData.lua and thus is not usable when setting locMoveButton.alpha = 1
If I remove the local from local locMoveButton= globalData.moveButton then it becomes a global variable, which is exactly what I trying to avoir with the modules…
Am I missing something? I tried to return something from the diverse functions but it hasn’t really helped.