Excellent! Happy to hear that you managed to solve it!
Like I promised, here is a small and easy example to dynamic spawned buttons using the gui.clone_tree() function for all who got some problems (or for the manual):
- Creating a GUI with a template button (if you dont already have one). In this example, just a box with a child text-node.
To keep it easy, we call the box simply ābuttonā, the text inside the box ābutton_textā:
- Create a new script (if not already there) and connect to the GUI:
- If not done, create a GameObject in main.collection to make it visible:
- Now to the main-thing: the gui_script.
- To do: cloning the template-button, changing text of each button, display button and manage if clicked.
Here is the script (gui_script, attached to the GUI with our template button):
function init(self)
msg.post(".", "acquire_input_focus")
-- table who stores our new gui-nodes / buttons
self.buttons = {}
-- cloning buttons and store in self.buttons
for i=1, 5 do
self.buttons[i] = gui.clone_tree(gui.get_node("button"))
-- acces to the cloned nodes - changing text of each button
gui.set_text(self.buttons[i][hash("button_text")], "Button # " .. i)
end
-- check if everything is fine
pprint(self.buttons)
-- placing the buttons in viewarea
for i,v in ipairs(self.buttons) do
-- placing to the horizontal center. y we multiple our counter with 35 (little more than button height to have a little space between, we use 35px height for that)
gui.set_position(self.buttons[i][hash("button")], vmath.vector3(640, 500 - i*35, 0))
end
end
function on_input(self, action_id, action)
if action_id == hash("click") and action.released then
-- CHECK the dynamic buttons
for i, v in ipairs(self.buttons) do
if gui.pick_node(self.buttons[i][hash("button")], action.x, action.y) then
print("CLICK ON BUTTON # " .. i)
end
end
end
end
Thats all to know
These two snippets of code will not mix well. Lua tables functioning as arrays (ie sequentially ordered indices) expect the first element to be on index 1, not 0. The length operator (#self.buttons
) and ipairs()
rely on this. Iām betting that your iterator will ignore the first button.
EDIT: Thank you for sharing a concrete example!
Thanks @britzl. Changed the first FOR-LOOP from 1 to 5
(Its never a good idea to code in different languages at the same time. other arrays/lists start with 0 )
//EDIT:
no problem - I like to help and give other ānewbiesā in defold or lua like me a little help to start up
Tell me about it! Iāve been coding a bit of Java these last couple of days and Iāve accidentally used {} in my Lua conditionals and function scopesā¦
Yesā¦ Its like ā¦
- ending all lines with a ;
- forgetting the āthenā after IF-clause or writing a bracket {
- using THIS instead of SELF
- using NEXT instead of END at the end of a FOR-loop
ā¦ etc etc
I am ofting coding my tools in Purebasic (like the original game I am now āportingā to defold). Its near to lua in many cases. There is a little bit JAVA and C# (unity) also
But I still miss the SELECT CASE statementsā¦
At the beginning (you know!) I got troubles with the variable-access. there is no public/private/this etc in lua.
But now I got it