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

Hey there.

I am cloning a node-tree (box with text and another sprite-box).
I’ll try to dynamiclly clone it to have couple of buttons based on a table/map.

simply works if I print out the returnvalue of the clone_tree, but where to find the unique-node-id of each element?
the return-table is like this:

DEBUG:SCRIPT: 
{
  hash: [buildmenu/text_buildstructure_base_title] = Headquater@(-35, -3, 0),
  hash: [buildmenu/icon_buildstructure_base] = box@(-40, 0, 0),
  hash: [buildmenu/text_buildstructure_base_description] = 100 Iron@(-35, -13, 0),
  hash: [buildmenu/button_buildstructure_base] = box@(550, 360, 0),
}

DEBUG:SCRIPT: 
{
  hash: [buildmenu/text_buildstructure_base_title] = Headquater@(-35, -3, 0),
  hash: [buildmenu/icon_buildstructure_base] = box@(-40, 0, 0),
  hash: [buildmenu/text_buildstructure_base_description] = 100 Iron@(-35, -13, 0),
  hash: [buildmenu/button_buildstructure_base] = box@(550, 360, 0),
}

DEBUG:SCRIPT: 
{
  hash: [buildmenu/text_buildstructure_base_title] = Headquater@(-35, -3, 0),
  hash: [buildmenu/icon_buildstructure_base] = box@(-40, 0, 0),
  hash: [buildmenu/text_buildstructure_base_description] = 100 Iron@(-35, -13, 0),
  hash: [buildmenu/button_buildstructure_base] = box@(550, 360, 0),
}
  1. looks like all cloned nodes have the same names etc? is that possible ?
  2. how to access (where is the unique node-id) for changing text of childs and for positioning?

clone return table, with your nodes. table[hash(“buildmenu…”)] will return node

1 Like

Hey.

Not sure if I understand what you mean. how to access the ID?

if I clone 100 of these node-trees, are they enumerated? Or do they all have the same hashs??

Thats how the table is filled:

	for v, i in ipairs(GLOBAL.structureType) do
		local nodeID = gui.clone_tree(gui.get_node("buildmenu/button_buildstructure_base"))
		pprint(nodeID)
		table.insert(buildStructureMenuItems, {nodeID = nodeID})
	end

you mean access to the IDs like this?

		for v,i in ipairs(buildStructureMenuItems) do
			menuY = menuY + space
			gui.set_position(buildStructureMenuItems[v][hash("buildmenu/button_buildstructure_base")], menuX, menuY, 0)			
		end

[EDIT] not working :slight_smile:

Clone nodes not have id. gui.clone_tree return table where key is hash of id, and key the new node. If i understand your code you should try buildStructureMenuItems[v][“nodeID”][hash("…")

1 Like

If you need to set an id on a node, use gui.set_id(): http://www.defold.com/ref/gui/#gui.set_id:node-id

1 Like

Thanks @sicher.

have to get access to every gui-element of the node first. I’ll check the tip of @d954mas (thanks to him too).
I was sure I have to set a new ID and already saw the SET_ID already, but without access to the new elements its impossible :slight_smile:

Typically all GUI nodes of a scene are referenced only by the scenes gui script. If you create dynamic nodes you can easily store the references you get back from gui.new_*() or gui.clone_*() in self for future use. There is seldom need for a node id if you already have the reference.

Ah ok.
but if I print out the table with the multiple cloned node (same) the Hashs/reference is all the same name?

/EDIT:

Oh. I should change the IDs BEFORE I stupidly store them into a table :wink: … will check that!

Hm… I’m stuck a bit (maybe it was alittle bit too much sun today…)

Thats the line with the cloning…

local nodeID = gui.clone_tree(gui.get_node("buildmenu/button_buildstructure_base"))

…with this output / content /table of nodeID:

DEBUG:SCRIPT: 
{
  hash: [buildmenu/text_buildstructure_base_description] = 100 Iron@(-35, -13, 0),
  hash: [buildmenu/button_buildstructure_base] = box@(-346.30075073242, 682.10809326172, 0),
  hash: [buildmenu/text_buildstructure_base_title] = Headquater@(-35, -3, 0),
  hash: [buildmenu/icon_buildstructure_base] = box@(-40, 0, 0),
}

Thats the line (right after the cloning) where I try to change the IDs of the 3 GUI Elements:

		gui.set_id(nodeID["buildmenu/icon_buildstructure_base"], "icon_buildstructure_" .. GLOBAL.structureType[v].name)
		gui.set_id(nodeID["buildmenu/text_buildstructure_base_description"], "buildmenu/text_buildstructure_" .. GLOBAL.structureType[v].name .."_description" )
		gui.set_id(nodeID["button_buildstructure_base"], "button_buildstructure_" .. GLOBAL.structureType[v].name)
		gui.set_id(nodeID["text_buildstructure_base_title"], "text_buildstructure_" .. GLOBAL.structureType[v].name .."_title" )

running will return the following error:

ERROR:SCRIPT: main/ingameGUI/ingame.gui_script:12: bad argument #1 to 'set_id' (userdata expected, got nil)

May someone has a tip / hint or a solution?

@sicher An example in the manuals would be very helpful :slight_smile:

The debug print of the references look the same but they are unique references to each specific node.

I’ll see how to clarify this.

should be

gui.set_id(nodeID[hash("buildmenu/icon_buildstructure_base")], ...)

But again, there is no need to set an id. You have them in a table that you can store in self for later use:

self.nodes = nodeID

and then you can access the nodes easily:

gui.set_color(self.nodes[hash("buildmenu/text_buildstructure_base_title")], ...)
1 Like

Thanks, the “knot in my brain” is now open :smiley: Got it. Sure without IDs (after my brain connected the right synapsis again).
Sometimes I feed stuck in a black hole waiting for sunlight :smiley:

Maybe a create a short example so we can mark this topic as solved or as example for the docs

3 Likes

I try to change a sprite in my node-tree.
I tried the gui.set_texture() already and gui.play_flipbook() without success.

gui.play_flipbook(nodeID["buildmenu/icon_buildstructure_base"], "units/Structure_Base")

The value I try to set is the same like in the editor, returns error

ERROR:SCRIPT: main/ingameGUI/ingame.gui_script:13: Texture units/Structure_Base is not specified in scene

The texture “units” is already set in the editor of cause.

And Structure_Base is an image in the units atlas? Is the atlas saved?

Hey @britzl.

Sure.

I’ve tried lots of different ways now:

with and without the “units/” …

I’ve tried the gui.set_texture() function also as the gui.play_flipbook() - returns “ERROR:SCRIPT: main/ingameGUI/ingame.gui_script:13: Animation units/animation_collector_collecting invalid for node (no animation set)” as I tried a defined animation.

Need more screens or code?

Thats the actual code:

	for v, i in ipairs(GLOBAL.structureType) do
		local nodeID = gui.clone_tree(gui.get_node("buildmenu/button_buildstructure_base"))
		-- CHANGE TEXT
		gui.set_text(nodeID["buildmenu/text_buildstructure_base_title"], GLOBAL.structureType[v].name)
		gui.set_text(nodeID["buildmenu/text_buildstructure_base_description"], GLOBAL.structureType[v].buildresource .. " ".. GLOBAL.structureType[v].buildcost)
		
		-- CHANGE SPRITE
		gui.set_texture(nodeID["buildmenu/icon_buildstructure_base"], "Unit_Collector") -- .. GLOBAL.structureType[v].sprite)
		--gui.play_flipbook(nodeID["buildmenu/icon_buildstructure_base"], "units/animation_collector_collecting")-- .. GLOBAL.structureType[v].sprite)
		
		
		--[[	
						DEBUG:SCRIPT: 
						{
						  hash: [buildmenu/button_buildstructure_base] = box@(-346.30075073242, 682.10809326172, 0),
						  hash: [buildmenu/text_buildstructure_base_title] = Headquater@(-35, -3, 0),
						  hash: [buildmenu/icon_buildstructure_base] = box@(-40, 0, 0),
						  hash: [buildmenu/text_buildstructure_base_description] = 100 Iron@(-35, -13, 0),
						}				
		]]
		table.insert(buildStructureMenuItems, nodeID)
	end

gui.set_texture() is only used if you have multiple textures added to your gui and you need to assign an image from a texture other than the one currently assigned to the node. If the image is in the same texture then it’s gui.play_flipbook() that you’re supposed to use. And as per the API example you should only use the image/animation name from the atlas, ie “Structure_Base” and not “units/Structure_Base”.

I did / tried. Did not work :frowning:

The animation 'Unit_Collector' could not be found.

But the “Unit_Collector” is in that atlas like you can see screen below.

Unit_Collector or Structure_Base… all are in that atlas…

// EDIT:

Works!
Dont know why it didnt work before. Saved everything etc. and ran. hm…
Doesnt matter. works now perfect.

Thanks (again and again) for you help @britzl

1 Like