Help with clone_tree

#1

I am using a GUI interface to generate a little map of each level in my game for the level select screen. I have each level stored as a table (levels are grid shaped), and each entry in the table is a different type of tile. Each tile is represented on-screen by a GUI node. Some of the GUI nodes have parent-child relationships, which allow me to use clipping. I have to move these cloned GUI nodes around and I cannot, for the life of me, figure out how to work with the cloned trees.

here’s what I have for the normal nodes (not trees)

	local tile = gui.clone(gui.get_node("topleft"))
	gui.set_position(tile, pos)
	gui.set_parent(tile, self.levelselectbuttons[levelno], true)
	table.insert(self.tiles[levelno], tile)
	gui.set_layer(tile, "tiles")

but I have spent hours and hours trying to figure out how to do the same for gui_clone.tree(). The table that I get back is difficult to access as it doesn’t have numerical keys, and I am not sure if parent-child relations are being maintained.

…I basically need to work with the cloned tree as if it were a single node, but I am not sure how to access the table created (without numerical keys). Can anyone help?

0 Likes

#2

Hi 88.josh,

Here is gui with a root and some child nodes.

In the guiscript you clone root node:

function init(self)
	local gui_root=gui.get_node("root_node")
	local cloned_gui=gui.clone_tree(gui_root)
	local cloned_root=cloned_gui[hash("root_node")]
	gui.set_position(cloned_root, vmath.vector3(300,300,0))
end

the resulting table is the mapping between original nodes and the cloned ones (https://defold.com/ref/gui/#gui.clone_tree:node)

Then you can get the cloned root node from the table and play with it.

The hierarchy is been respected in the clone.

Mobile game.zip (277.6 KB)

Hope this helps :slight_smile:

5 Likes