How to add button widget from module into main scene?

Hi,

I have a main game scene and multiple modules that make up the scene. If I want to create a new button widget in one of the modules, how do I insert it into the game scene? Right now I have game.lua:

local someModule = require("someModule"); ... unction scene:create(event) local sceneGroup = self.view; -- Insert button object from someModule sceneGroup:insert(someModule.someButton); end ...

someModule.lua:

local composer = require("composer"); -- Load scene local widget = require("widget"); -- Used to make buttons local M = {} local function onDoSomething(event) if (event.phase == "began") then end if (event.phase == "ended") then end end M.someButton = widget.newButton({ ... }) return M

Am I doing it right? Is there a better/right way to add a button into a scene from a module?

Thanks!

That is probably overkill…  Just create the button directly in scene:create()?

Ya maybe but then my game.lua file will be one very big file. I’m trying to break it down into modules, isn’t that what we’re supposed to do with big long files?

let’s set aside whether this might or might not be overkill for breaking things down…

i’d not have “someModule” directly create “someButton” on load. (which is what it’s doing now)  rather, i’d have someModule have a method that creates the button when requested, returning a unique instance of the newly-created widget.

first, you won’t have these widgets polluting the display just because you required the module, they won’t exist until you explicitly call the helper method.

second, you could then rename “someModule” to “ButtonSupport” (or somesuch) and potentially put several such helpers in there.  (if you want, depending on how much break-down you’re after)  

instead of writing your own (which may be a worthwhile learning exercise) you could do what I did (when I started) and use something like http://www.pixelenvision.com/wp-content/uploads/ui.lua.2.4.zip

you can then create buttons with a single line of code in scene:create()

once your comfortable with that you can then extend it (and modernise it) to animate, transition, etc.

Code organization is something that you constantly improve upon. It’s a good idea to think about your Lua files and what their purposes are for.

As an example, the game I’ve been tinkering with in my spare time, I have taken the approach where I break code into scenes, classes, and libraries. 

Scenes have code that manages putting everything on screen. If the scene needs a UI with a button, it will call the code to create the button. That, of course, may just be a direct call to widget.newButton or it could be a library call that has a module designed to make a certain type of button (in my case, it shows helper icons for using controllers). The game scene also calls class files to create the player and enemies. Those modules contain all the code specific to the enemy, how to create it, how it moves, how it fights, etc. The creation code returns the display object with all the management methods.  The library files contain kind of single purpose functions that get used everywhere.

Rob

Thanks for your response :slight_smile: I just simplified my example to pin-point the problem I was having. My module isn’t really called someModule and it does other things among creating a button widget too.

That is probably overkill…  Just create the button directly in scene:create()?

Ya maybe but then my game.lua file will be one very big file. I’m trying to break it down into modules, isn’t that what we’re supposed to do with big long files?

let’s set aside whether this might or might not be overkill for breaking things down…

i’d not have “someModule” directly create “someButton” on load. (which is what it’s doing now)  rather, i’d have someModule have a method that creates the button when requested, returning a unique instance of the newly-created widget.

first, you won’t have these widgets polluting the display just because you required the module, they won’t exist until you explicitly call the helper method.

second, you could then rename “someModule” to “ButtonSupport” (or somesuch) and potentially put several such helpers in there.  (if you want, depending on how much break-down you’re after)  

instead of writing your own (which may be a worthwhile learning exercise) you could do what I did (when I started) and use something like http://www.pixelenvision.com/wp-content/uploads/ui.lua.2.4.zip

you can then create buttons with a single line of code in scene:create()

once your comfortable with that you can then extend it (and modernise it) to animate, transition, etc.

Code organization is something that you constantly improve upon. It’s a good idea to think about your Lua files and what their purposes are for.

As an example, the game I’ve been tinkering with in my spare time, I have taken the approach where I break code into scenes, classes, and libraries. 

Scenes have code that manages putting everything on screen. If the scene needs a UI with a button, it will call the code to create the button. That, of course, may just be a direct call to widget.newButton or it could be a library call that has a module designed to make a certain type of button (in my case, it shows helper icons for using controllers). The game scene also calls class files to create the player and enemies. Those modules contain all the code specific to the enemy, how to create it, how it moves, how it fights, etc. The creation code returns the display object with all the management methods.  The library files contain kind of single purpose functions that get used everywhere.

Rob

Thanks for your response :slight_smile: I just simplified my example to pin-point the problem I was having. My module isn’t really called someModule and it does other things among creating a button widget too.