is there such thing as the "Main" tree

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

Hello,

I’m creating a “saving function” for saving my game state.
This function would need to use a tree in order to get the group of nodes to save. (Scene_tree.get_nodes_in_group("group_name"))

Thing is I made the Save function static to call it anywhere in my code. but I would need a tree to get all the node that need to be saved.

My question is. Is there such thing as the main tree just like the main scene?
I’m fully aware that you can have completely different tree. But I thought that there might be a “main tree” since we choose the “main scene” inside the config. And I thought that there was an easy way to access that tree since it would be the main one.

Thank you,

:bust_in_silhouette: Reply From: Wakatta

Sorry to burst your thought bubble but there is only one tree

So the Scenetree is constructed as follows

Root > Main > Branch > Leaves
            > Branch > Leaves
            > Branch > Leaves > More leaves

And when you add Autoloads

Root > Main > Branch > Leaves
     > Autoload > Branch > Leaves

In these examples the Branches can be nodes or scene instances and the Leaves thier children, while main and autoloads can be considered stems/trunks.

To access the main node which i suspect is your intention use get_tree().get_current_scene() or if not then use get_tree().get_root() which will be the node at the very base of the Scenetree and all nodes can be accessed from it like a filesystem structure.

E.g.

get_node("/Main/Branch/Leaf")

Your code Scene_tree.get_nodes_in_group("group_name") is already the best posible way you can impliment your save system. Ensure your nodes have a save function then call it using get_tree().call_group("group_name", "save_function") from any node

Wakatta | 2021-11-13 00:20