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
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:
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:
Node
from the scene tree. Not a property of the current Node
.When you do this:
_init
.Thus, here:
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: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:"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:On the flip side – if you want to do it all from code – you could build it all from the root, I mean
Main
:Node
s 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
Node
s, 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 serializedNode
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
InstancePlaceholder
s - Use Background loading.
- Use
If you have better answer, please add a comment about this, thank you!