How is Godot's hierarchy of nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

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

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

--LevelAssigner #This node changes the current "level"
----Level4 #Levels that aren't used is removed completely, freeing resources
----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.

MysteryGM | 2018-12-07 22:47

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:


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

JulioYagami | 2018-12-08 09:53

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

pospathos | 2018-12-08 12:38

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

p7f | 2018-12-08 12:40

:bust_in_silhouette: Reply From: eons

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.

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.

JulioYagami | 2018-12-08 19:42

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.

eons | 2018-12-08 20:19