Hello their! I am Oolks! I want to recreate the hierarchy of nodes like this:

In runtime, I decided to use the tree node but, it is unable to show me the childrens and the grand childrens (and so on) of an object. It works as:

extends Tree

var selected_item: Node

func _ready() -> void:
	create_tree_items(get_tree().get_root().get_node("index/Level"), self.create_item(self))

func create_tree_items(node: Node, item: TreeItem) -> void:
	for child_index in range(node.get_child_count()):
		var child = node.get_child(child_index)
        
		if child.get_name() == "":
			continue
        
		var child_item = self.create_item(item)
		child_item.set_text(0, child.get_name())
        
		create_tree_items_recursive(child, child_item)

func create_tree_items_recursive(node: Node, item: TreeItem) -> void:
	for child_index in range(node.get_child_count()):
		var child = node.get_child(child_index)
        
		if child.get_name() == "":
			continue
        
		var child_item = self.create_item(item)
		child_item.set_text(0, child.get_name())
        
		if child.get_child_count() > 0:
			create_tree_items_recursive(child, child_item)


func _on_Objects_child_entered_tree(node):
	pass # Replace with function body.

Whenever the _on_Objects_child_entered_tree is fired (signal) it creates a new tree item which has no parent meaning if its a children of an object, it wont show as a children of the object in the tree.

  • Guys, you are confusing the scene tree and the Tree node. Tree is a gui control node that displays any custom data in the tree format. Items are added using Tree::create_item(). This has nothing to do with Tree's children in the scene tree, which you can add via add_child() as you would with any other node.

    The problem in OP's code is probably in the first call to create_item() where self is passed as an argument. This is not valid since that method expects a reference to TreeItem object that will be used as a parent to the new item. When calling create_item() for the first time to create the root item, you should pass null or just nothing.

I don't see an add_child in there. Don't ask me where it would go, but I think you have to use add_child(child) to some object in the tree after it's created.

    fire7side I don't see an add_child in there.

    I haven't used a Tree node, but its documentation states that create_item() adds a child. There's no add_child() method in the documentation.

      Guys, you are confusing the scene tree and the Tree node. Tree is a gui control node that displays any custom data in the tree format. Items are added using Tree::create_item(). This has nothing to do with Tree's children in the scene tree, which you can add via add_child() as you would with any other node.

      The problem in OP's code is probably in the first call to create_item() where self is passed as an argument. This is not valid since that method expects a reference to TreeItem object that will be used as a parent to the new item. When calling create_item() for the first time to create the root item, you should pass null or just nothing.

        11 days later

        xyz Hey! So it seems that it works for objects node now:

        extends Tree
        
        var selected_item: Node
        
        func create_tree_items(node: Node, item: TreeItem) -> void:
        	if node is RigidBody or node is Node:
        		var current_item = self.create_item(item)
        		current_item.set_text(0, node.get_name())
        
        		for child_index in range(node.get_child_count()):
        			var child = node.get_child(child_index)
        			if child is RigidBody:
        				if child.get_name() == "":
        					continue
        				create_tree_items(child, current_item) 
        
        func Timeout():
        	clear()
        	create_tree_items(get_tree().get_root().get_node("index/Level"), null)
        	create_tree_items(get_tree().get_root().get_node("index/Controllers/ScriptController/Scripts"), null)

        But for scripts whenever a new node enters it, it doesn't create a TreeItem for that.