This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Is it possible to esport a node path to a singleton script so that it can be accessed by other nodes even if it changes its position in the tree?
Let's say I have a node which is is being accessed by other nodes via getnode. In getnode I'm prviding full paths from root. But, if that node needs to move for any reason (eg, I want to reparent it) the path will change and I will have to update the path in all scripts currently accessing the node. I'm looking for a way to make godot find the node wherever it happens to be in the tree. I was thinking, if I could somehow export its path to a global singleton and access it from there?

in Engine by (516 points)

2 Answers

0 votes

right, here's how i got it to work (let me know of there is an easier way):

Singleton script

onready var path_to_node


Sought Node script

func _ready()
Singleton.path_to_node = self.get_path()


Other nodes (which need to access sought node)' script

var sought_node = get_node(Singleton.path_to_node)
by (516 points)
+1 vote

Storing the path in a singleton will still loose reference once the node is moved unless it is updated. An easier way to do this is to store the node itself in the singleton and use get_path() if necessary. It is also possible to use find_node() like this

get_node("/root/scene node").find_node("name of node")
by (3,259 points)

wow, didnt know you could find nodes that easily in a hierarchy. I need to try this. Thanks!

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 Frequently asked questions and 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 [email protected] with your username.