Problem with passing data from a module to another

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.

Hi @J@V0,

I would do this :

toolbar.lua

local M = {} local globalData = require( "globalData" ) function M.loadBottomToolbar()      local  locMoveButton= widget.newButton(       {         -- ...       }     )     locMoveButton.x = 400     local modes = require("modes")     locMoveButton:addEventListener("tap", modes.moveMode)     globalData.moveButton = locMoveButton // Assign button for later use end return M

Also I would remove line 

globalData.moveButton = nil

from globalData.lua

Read more :

Have a nice day:)

ldurniat

This all seems a bit weird. I’m not sure what your goal with modes.lua and globalData.lua are, but you should be able to completely get rid of them (depending on what “more code” is).

Your main issue seems to lie with not actually returning the toolbar that you create AND not creating a local variable for it.

 

local toolbars = require("toolbar") local bottomToolbar = toolbars.loadBottomToolbar()

This way, once you create and return that bottomToolbar, you can actually control it locally in your main.lua file. In order to return that toolbar, you’d simply include “return locMoveButton” inside function M.loadBottomToolbar(). 

As for your toolbar.lua, calling widget.newButton() before requiring widgets will result in a crash. 

Interesting approach, I managed to fix the problem with the following code:

toolbar.lua

local M = {} local globalData = require( "globalData" ) function M.loadBottomToolbar() globalData.deleteButton = widget.newButton( { -- ... } ) globalData.deleteButton.x = 400 end return M

globalData.lua

-- Pseudo-global space local globalData = {} return globalData

main.lua

local globalData = require( "globalData" ) local toolbars = require("toolbar") toolbars.loadBottomToolbar() local modeModules = require("modes") globalData.moveButton:addEventListener("tap", modeModules.moveMode) local buildMenuModules = require("buildMenu") globalData.buildButton:addEventListener("tap", buildMenuModules.createBuildBar)

As you can see I don’t return anything from the function but instead change the value of the globalData variables, as @idurniat suggested. Do you see any drawback with my implementation?

it’s just a bit “convoluted” because of your three modules there’s not one who seems to be clearly in charge of things.

in your first usage, toolbars directly assigns its value to globalData.

in later uses, main is doing some work in globalData with values pulled from mode/build modules.

those are totally different “paths” through your code/data.

perhaps try removing all references to globalData from your modules, then put main in charge of “wiring”, fe:

-- shorthanded pseudocode style.. -- main.lua local globalData = require("globalData") globalData.thing1 = require("thing1").create() globalData.thing2 = require("thing2").create() globalData.thing3 = require("thing3").create() -- thing1.lua et al -- this module does NOT need globalData, just creates/returns a widget return { create = function(whatever) return widget.newWhatever(whatever) end }

Hi @J@V0,

I would do this :

toolbar.lua

local M = {} local globalData = require( "globalData" ) function M.loadBottomToolbar()      local  locMoveButton= widget.newButton(       {         -- ...       }     )     locMoveButton.x = 400     local modes = require("modes")     locMoveButton:addEventListener("tap", modes.moveMode)     globalData.moveButton = locMoveButton // Assign button for later use end return M

Also I would remove line 

globalData.moveButton = nil

from globalData.lua

Read more :

Have a nice day:)

ldurniat

This all seems a bit weird. I’m not sure what your goal with modes.lua and globalData.lua are, but you should be able to completely get rid of them (depending on what “more code” is).

Your main issue seems to lie with not actually returning the toolbar that you create AND not creating a local variable for it.

 

local toolbars = require("toolbar") local bottomToolbar = toolbars.loadBottomToolbar()

This way, once you create and return that bottomToolbar, you can actually control it locally in your main.lua file. In order to return that toolbar, you’d simply include “return locMoveButton” inside function M.loadBottomToolbar(). 

As for your toolbar.lua, calling widget.newButton() before requiring widgets will result in a crash. 

Interesting approach, I managed to fix the problem with the following code:

toolbar.lua

local M = {} local globalData = require( "globalData" ) function M.loadBottomToolbar() globalData.deleteButton = widget.newButton( { -- ... } ) globalData.deleteButton.x = 400 end return M

globalData.lua

-- Pseudo-global space local globalData = {} return globalData

main.lua

local globalData = require( "globalData" ) local toolbars = require("toolbar") toolbars.loadBottomToolbar() local modeModules = require("modes") globalData.moveButton:addEventListener("tap", modeModules.moveMode) local buildMenuModules = require("buildMenu") globalData.buildButton:addEventListener("tap", buildMenuModules.createBuildBar)

As you can see I don’t return anything from the function but instead change the value of the globalData variables, as @idurniat suggested. Do you see any drawback with my implementation?

it’s just a bit “convoluted” because of your three modules there’s not one who seems to be clearly in charge of things.

in your first usage, toolbars directly assigns its value to globalData.

in later uses, main is doing some work in globalData with values pulled from mode/build modules.

those are totally different “paths” through your code/data.

perhaps try removing all references to globalData from your modules, then put main in charge of “wiring”, fe:

-- shorthanded pseudocode style.. -- main.lua local globalData = require("globalData") globalData.thing1 = require("thing1").create() globalData.thing2 = require("thing2").create() globalData.thing3 = require("thing3").create() -- thing1.lua et al -- this module does NOT need globalData, just creates/returns a widget return { create = function(whatever) return widget.newWhatever(whatever) end }