0 votes

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?

in Engine by (71 points)

1 Answer

+2 votes
Best answer

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()

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

by (10,329 points)
selected by

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.

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.

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

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.

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.