Return-value and nodeID using gui.clone_tree() (SOLVED)

Excellent! Happy to hear that you managed to solve it!

1 Like

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):

  1. 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ā€:

  1. Create a new script (if not already there) and connect to the GUI:

  1. If not done, create a GameObject in main.collection to make it visible:

  1. 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 :slight_smile:

3 Likes

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 :slight_smile:

(Its never a good idea to code in different languages at the same time. other arrays/lists start with 0 :wink: )

//EDIT:
no problem - I like to help and give other ā€œnewbiesā€ in defold or lua like me a little help to start up

1 Like

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ā€¦

:smiley:

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 :slight_smile:

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 :slight_smile:

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 :wink:

3 Likes