I added my player as a singleton and now Godot cannot find its sprite node. The game crashes when I try to move and gives me a "get_node: Node not found: Sprite." error. Does anyone know what to do?
-
The singleton is a kinematicbody2D and I'm trying to access its child node, Sprite(animated_sprite), which whenever I type in kinematicbody2D's script something along the lines "$Sprite" it says that Sprite can't be found – Snowy Diamond Apr 10 '21 at 18:32
-
1Can you include the relevant part of your script and a screenshot of your hierarchy? – Aaron Record Apr 10 '21 at 23:06
1 Answers
If by "singleton" you mean AutoLoad, please pay attention that this concept was introduced to Godot as a workaround for storing scene-independent or inter-scene information. As documentation says:
When autoloading a script, a Node will be created and the script will be attached to it. This node will be added to the root viewport before any other scenes are loaded.
As far as I understood from your question, the Sprite
node is a child of a node that had a script attached to it with a line of code, similar to
self.get_node("Sprite")...
But when you AutoLoad
ed this script, you actually introduced a new node as a child of a root viewport, and this node does not inherit the tree structure of the "original" node that had Sprite
child. It has only the same script attached as the original node. Also referring by AutoLoad
name will refer to this new object (without Sprite
child) rather than your original one.
I think what you wanted to do is to provide an inter-scene way to refer to that particular node (that has its Sprite
child). I would recommend to look into Godot's Groups to do that in a clean way. Put your node to a specific group and then request it from that group:
theNode.add_to_group("MyPublicNodes")
# Get that node somewhere and get its `Sprite` child
get_tree().get_nodes_in_group("MyPublicNodes")[0].get_node("Sprite")

- 1,568
- 1
- 14
- 31