Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with passing data from a module to another
Started by J@V0 Nov 04 2018 07:18 AM

4 replies to this topic
data module scope variables

Best Answer ldurniat , 04 November 2018 - 08:57 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Observer

  • 14 posts
  • Corona SDK

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, scope documentation 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.



[TOPIC: post.html]
#2

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 365 posts
  • Corona SDK

  Best Answer

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


[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 293 posts
  • Corona SDK

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. 



[TOPIC: post.html]
#4

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Observer

  • 14 posts
  • Corona SDK

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(). 

 

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?



[TOPIC: post.html]
#5

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,236 posts
  • Enterprise

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
}


  • J@V0 likes this


[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: data module, scope, variables