Getting error: Cannot get path of node as it is not in a scene tree

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m writing an editor plugin. I’m dynamically building a toolbar which involves iterating over the child nodes of a ‘menu’ node and creating a button control for each one of them. Since my custom button requires the path to the node that the button references, I’m calling get_path() on each of the nodes when it iterate over the children.

Unfortunately, this is resulting in the Cannot get path of node as it is not in a scene tree error being printed to console. The node with the children is being added as a child of the root plugin node. I’ve tried calling is_node_ready() and is_inside_tree() on the children when I build the buttons, but I’m still getting the error. I seem to be getting correct paths despite the error, since I can use them to find the original nodes.

What can I do to avoid this error?

		var menu_root = editor_plugin.config_scene.get_node("Views/View3D/Toolbar")
	
		for child in menu_root.get_children():
			if child is ToolbarButtonRef:
				var tool:CyclopsTool = child.tool

				if tool.is_inside_tree() && tool._show_in_toolbar() && tool._can_handle_object(active_block):
					var bn:ToolButton = preload("res://addons/cyclops_level_builder/gui/menu/tool_button.tscn").instantiate()
					bn.tool_path = tool.get_path()
					...

you didn’t add it to tree.

var bn:ToolButton = preload("res://addons/cyclops_level_builder/gui/menu/tool_button.tscn").instantiate()

you instantiated it, but I don’t see where is add_child() part

I omitted that part. I’m adding the button later. The error is coming from the tool.get_path() line.

After deleting my cache and rebuilding, the error seems to have gone away.

I really wish Godot would compile projects the way C++ does. There are just so many weird side effects from whatever it’s doing on the fly that makes the same project behave very differently depending on stuff that it ought not depend on.

1 Like

I recommend using the C# version if you want to compile. I believe you’ll get all the checking you’re looking for. Plus, you can convert your current project, as the C# version will run all the GDScript you’ve already written.