0 votes

I need to know this to better work with the get_node function and write the proper path strings to use with $. I want to know something like this:

  • Scene Tree
  • Main Scene
  • Node1
  • Node2
in Engine by (655 points)

I notice Godot works a lot like the old game engines, and I have started mimicking there tree designs.
So it looks something like this for me:

Main #This node is responsible for Global values, like score

--MainMenu #Keeps all my menus
----Options
----FileBrowser
----Inventory

--LevelAssigner #This node changes the current "level"
----Level4 #Levels that aren't used is removed completely, freeing resources
------Light
------Floor
------Enemy
------Pickup
----Player #Player is actually next to level, to keep it persistent when level changes.

If you keep the tree flat, it becomes difficult to find things. The nodes like MainMenu and LevelAssigner acts as groups. If I want a menu I know where to check.

That was not quite what I wanted to know. I want to know how I can get a node from the scene tree, for example in this code:

get_tree().get_root()

For example, how can I get the "Inventory" node from the scene tree?

Do you think this get_tree().root.get_node("NondeName") ? Your question is not clear enough.

$MainMenu/Inventory is not working for you? I'm not sure if i understand the problem

1 Answer

+1 vote
Best answer

The tree is not a node, is a MainLoop object (which takes care of the game's lifecycle), it contains the root viewport and do all the game loop stuff.
All the nodes can get a reference to the tree with get_tree().

The tree root is a Viewport node and it can hold many nodes, usually the current scene and autoloaded scripts and scenes.
You can access the root with $"/root" or get_tree().root

The current scene can be found in get_tree().root.current_scene.
Autoloaded scenes and scripts are children of the root and you will find these there like any node $/root/autoloaded_name.

"Navigation" can be absolute or relative from the position where the script/node is.

All this and more is in the docs, and looking at the running tree (remote scenetree while running) may help you a bit to understand the full structure.

by (7,860 points)
selected by

So for example, if I want to get the root node of my main scene can I use get_tree().root.current_scene? And as for its nodes can I use get_tree().root.current_scene.get_node("MyNode")? For indeed this was my main doubt, because I have a project where I need to get some knots from his grandchildren, nephews and other cases.

Absolute paths are needed only if you do not know the full path, and is not a common situation.

For a node/script to access a grandchild, you can use relative paths, like $child_node/grandchild_node ($ is the same to get_node).

To get a sibling, you can do, from child_node1 get_parent().get_node(child_node2) or $"../child_node2" (relative, starts in node 1, goes up, gets node 2).
The .. refers to the parent, the notation is the same used on text consoles (cmd, bash) when changing directories or reading/opening files.
You can even do get_node or $ with "../.." to get a grandparent instead of get_parent().get_parent().


For a single scene you can do export(NodePath) var foo_path and use the inspector to pick a node from the tree, so you do not have to worry about the path, then in the script get the node reference with get_node(foo_path).


It is always advised to avoid accessing other than down the tree from a particular node and the parent at most when going up, because that may imply a problem in the design that later will be reflected in entangled scenes and code that will be difficult to maintain and fix.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.