How can I reliably get the Scene Tree, when not inside the tree?

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

Here’s the scenario:

I have scene that should run at certain times. To that effect, I create the scene with .instance() and call a run() function that I created on it.

What I want to happen is to have that scene add itself to the currently active scene. In order to do this, I have to somehow get the currently active scene.

get_tree returns null, because according to the docs, I haven’t added it to a tree, so it can’t get the root of the tree. When a node is part of the Scene Tree, the SceneTree singleton can be obtained by calling Node.get_tree().”

how can I have this node add itself to the tree?

:bust_in_silhouette: Reply From: njamster

how can I have this node add itself to the tree?

You can’t! Nothing strange about it either, you cannot invite yourself to an exclusive club, you have to get invited by someone who already is a member of that club:

var scene = load("<PathToScene">).instance()
get_tree().get_current_scene().add_child(scene)
scene.run()

(You may call run() before calling add_child - depending on what it does)

yes… This is the conclusion I came to myself. So I added a work around. I made a global Singleton of the root tree, and have my class use that. It’s a kludge, but it is working. I’ll have to come up with a different design idea eventually.

Allen Kennedy Jr. | 2020-05-28 16:19

May I ask why it’s important to you that an instance adds itself to the current scene instead of getting added by some other instance? Just curious.

njamster | 2020-05-28 17:53

It’s an onscreen effect that needs to live on the HUD layer which is in its own canvas. But it is generated by a scene in the main game tree on a different canvas layer. Translating from the game global coordinates to the HUD canvas coordinates when there is a camera involved using zoom is literally impossible. There is no way to do it. So, if I could get the effect to be a child of the HUD, then the position coordinates become simple.

That is to say… finding the HUD from the root game node is hard enough because I don’t know how far down it is. Plus, should the game character really ‘know’ about the HUD? I don’t think so, from a OOP standpoint. So it makes more sense for the effect to know where and how to place itself with no other dependency

Allen Kennedy Jr. | 2020-05-28 17:58

I think I would turn the HUD-layer into a Singleton and add the effect as it’s child in its _ready-method. I still don’t see why that would require the effect to add itself.

finding the HUD from the root game node is hard enough because I don’t know how far down it is

You could always use find_node("<NodeName>") for that.

should the game character really ‘know’ about the HUD?

Well, as long as it’s in the scene tree, the player will “know” about it. However, I agree that it’s good practice to keep the game and its HUD as separated and self-contained as possible. But again, that doesn’t require nodes adding themselves to the tree.

njamster | 2020-05-29 09:53