How to Pass Parameters to a Module?

Hello,

I have two different scenes, one mainScene and one inventoryScene. Inside the mainScene I want to load a “buttons” module and that module creates all the buttons needed. But since I called this module from mainScene, i want the buttons to be added to the mainScene as well. But I can’t accomplish that since I can’t pass “sceneGroup” to this module. Is there any way to pass this variable to the buttons module?

Thanks in advance!

Hi @gurcanglc,

Are you using Corona’s built-in Composer scene management library? Or are you simply using Lua-side methods to manage your scenes?

Brent

This is kind of a deep subject because there are multiple way’s to accomplish it. Your buttons module could create the buttons, put them in a group and then the module could return that group. Maybe something like:

-- makebuttons.lua local M = {} function M.create()       local group = display.newGroup()       ... code to create buttons and insert them into "group"       ...       return group end return M

then when you want to get the buttons in scene A

local makeButtons = require("makebuttons") function scene:create( event )      local sceneGroup = self.view      local buttons = makeButtons.create()      sceneGroup:insert( buttons ) end

You would of course have to work that into  your own code.

Rob

How about something simple like this in your buttons module?

All you do is pass it a reference to the display group you want the buttons inserted into - be that your own group or the corona scene group.

function createButtons(displayGroup) --create buttons local button1 = widget.newButton({left = 100, top = 200, id = "button1", label = "Button1"})   displayGroup:insert(button1) local button2 = widget.newButton({left = 100, top = 300, id = "button2", label = "Button2"})   displayGroup:insert(button2) end

I’m using Corona’s built in composer library.

This is a great solution @Rob. It could work really good in my case.

This is also a great solution. I might use this one because it really looks elegant and easy.

Up until now I was using this line of code, I know it’s not really elegant and ugly and I wanted to phase it out.

assert(loadfile([[full.path.to.the.file]]))(sceneGroup);

then in the module

local arg={...}

and then whenever I wanted to use sceneGroup, I used arg[0].

I’ll use one of your solutions. Thank you so much.

Hi @gurcanglc,

Personally, and for the sake of simplicity and clarity, I really prefer using the Composer “setVariable()” and “getVariable()” functions. Set it once, get it anywhere else within a Composer-enabled module. Change them if needed, add to them, or even “nil” them out if you don’t need them anymore. So, so, so easy, I doubt I’d ever use another way (assuming I was using Composer). You can even, of course, store tables of multiple values or complex nested tables under one variable name, and then pick them apart in the receiving scene as needed.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

Best regards,

Brent

Knowing this will make the variable transitions between scenes very easy. But for this problem creating a function to add the scenes seems better and easier to understand in my opinion.

local buttons = require("buttons"); buttons.add\_buttons\_to\_scene(sceneGroup);

is clearer than adding sceneGroup to composer variables and then inside the buttons file using the sceneGroup then removing it. But nevertheless I’ll use getVariable and setVariable. Thanks a lot.

Hi @gurcanglc,

Are you using Corona’s built-in Composer scene management library? Or are you simply using Lua-side methods to manage your scenes?

Brent

This is kind of a deep subject because there are multiple way’s to accomplish it. Your buttons module could create the buttons, put them in a group and then the module could return that group. Maybe something like:

-- makebuttons.lua local M = {} function M.create()       local group = display.newGroup()       ... code to create buttons and insert them into "group"       ...       return group end return M

then when you want to get the buttons in scene A

local makeButtons = require("makebuttons") function scene:create( event )      local sceneGroup = self.view      local buttons = makeButtons.create()      sceneGroup:insert( buttons ) end

You would of course have to work that into  your own code.

Rob

How about something simple like this in your buttons module?

All you do is pass it a reference to the display group you want the buttons inserted into - be that your own group or the corona scene group.

function createButtons(displayGroup) --create buttons local button1 = widget.newButton({left = 100, top = 200, id = "button1", label = "Button1"})   displayGroup:insert(button1) local button2 = widget.newButton({left = 100, top = 300, id = "button2", label = "Button2"})   displayGroup:insert(button2) end

I’m using Corona’s built in composer library.

This is a great solution @Rob. It could work really good in my case.

This is also a great solution. I might use this one because it really looks elegant and easy.

Up until now I was using this line of code, I know it’s not really elegant and ugly and I wanted to phase it out.

assert(loadfile([[full.path.to.the.file]]))(sceneGroup);

then in the module

local arg={...}

and then whenever I wanted to use sceneGroup, I used arg[0].

I’ll use one of your solutions. Thank you so much.

Hi @gurcanglc,

Personally, and for the sake of simplicity and clarity, I really prefer using the Composer “setVariable()” and “getVariable()” functions. Set it once, get it anywhere else within a Composer-enabled module. Change them if needed, add to them, or even “nil” them out if you don’t need them anymore. So, so, so easy, I doubt I’d ever use another way (assuming I was using Composer). You can even, of course, store tables of multiple values or complex nested tables under one variable name, and then pick them apart in the receiving scene as needed.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

Best regards,

Brent

Knowing this will make the variable transitions between scenes very easy. But for this problem creating a function to add the scenes seems better and easier to understand in my opinion.

local buttons = require("buttons"); buttons.add\_buttons\_to\_scene(sceneGroup);

is clearer than adding sceneGroup to composer variables and then inside the buttons file using the sceneGroup then removing it. But nevertheless I’ll use getVariable and setVariable. Thanks a lot.