• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: Godot: Cannot get path of node as it is not in a scene tree

Resolved: Godot: Cannot get path of node as it is not in a scene tree

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a problem with Godot (I’m learning it). I have a project with 3 scenes, in each there are nodes with attached scripts, shown bellow.
Main.gd
Maquina.gd
Baldosa.gd
To my understanding: Main starts instantiating Maquina correctly, since it’s done when Main is in the Scene tree, but when Maquina instantiates Baldosa it isn’t on the tree, so _ready is not called and on _init, Sprite doesn’t exist yet.
I can’t figure out how to put the node in the tree before trying to access its properties. Any help, please. 🙁
UPDATE
I missed to mention that Baldosa.tscn already have an Sprite node:
enter image description here

Answer:

The evident problem


You say this:

I can’t figure out how to put the node in the tree before trying to access its properties. Any help, please. 🙁


But the problem is not accessing the properties. The problem is accessing the scene tree. What I’m saying is that this line:
Is trying to get a child Node from the scene tree. Not a property of the current Node.
When you do this:
Godot will allocate the object, initialize the variables of the object (to their default value), and run _init.
Thus, here:
Yet, that won’t work on its own without changing Maquina too:

The hidden problem: Where is Sprite?


When _ready executes the Node is already in the scene tree. But, as it turns out, it does not have this Sprite child.
I don’t see any code that adds this Sprite child anywhere in your question.
Here I need to remind you that a script is not a scene. The name Baldosa refers to a script, as you see here:
And when you initialize it, like this:
Godot will create an object of the build-in type the script ultimately inherits from (the script can say to extend a build-in type, another script, or not say at all… In which case it extends Reference… In the case at hand you get a Node2D), and attach the script to it.
That process does not involve figuring out in which scene you use the script (there could be one, multiple, or none), nor instantiating any scene.
Thus baldosa will not have children. So there would not be a Sprite child, and thus this still fails:
Replace "res://path/to/scene/baldosa.tscn" with the path to the scene you want to use (which has the Baldosa script on the root, and a child Sprite). If you don’t have it, then go ahead and create said scene.
And with that the code on Baldosa should work.
Just in case, let me tell you: Godot does not have a prefab/scene distinction. They are all scenes. You can instantiate scenes inside of scenes. There are no prefabs, this is not Unity. If you are thinking “I want a prefab here”, you want a scene.
I went over the process Godot uses to instantiate a scene elsewhere. Including when _init, _enter_tree and _ready execute. You can look there for more details.

If you REALLY don’t want to use a scene…


And just to be thorough, if you really don’t want to use a scene, you can always have the Baldosa script create the child from code:
I notice, this is similar to what you have been doing. So you probably just set up the scene the way you want and not have no code at all.
On the flip side – if you want to do it all from code – you could build it all from the root, I mean Main:
Similarly you could add Nodes on demand from code.
What is the ultimate goal here? I can think of a few reasons why you might want to do things from code:
  • Good old curiosity of how to do it. Hopefully this answer satisfies that.
  • You want to instance these Nodes, but you only when or know how many in runtime. For this use case, you would have code that instantiates a scene, as shown previously. Consider a scene a serialized Node with properties and children.
  • You are trying to reduce the load time of your scene. For this use case, aside from using code to instantiate scenes, you could:
    • Use InstancePlaceholders
    • Use Background loading.

If you have better answer, please add a comment about this, thank you!

class gdscript godot scene
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Sort dataframe columns value by close it to another column

27/03/2023

Resolved: PostgreSQL resample 1 minute OHLCV candle data into 5 minute OHLCV candle data

27/03/2023

Resolved: How do I navigate a table without any easily accessible distinctions?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.