Hi,
If you want to check if there is actually node under some path you can probably just use get_node()
and check its return value like:
func node_exists(nodePath) -> bool:
var node = get_node(nodePath)
return node != null
Of course that is only simple example if you are going to use node if it is returned probably better way will be using get_node()
directly more like:
func do_something(nodePath):
var node = get_node(nodePath)
if(!node):
return
#Do something with returned node
That way you avoid getting node twice (first time for checking if path is ok and second one to actually use node). Don't know how costly is get_node()
, but that is way I prefer.
On the other hand if you have already some node and you need to get its path you can do that by:
var nodePath = someNode.get_path()
Maybe there is some dedicated method to check node under some path, but I used above way and it worked for me (one issue is get_node()
will print error when node is not found, but it doesn't break functionality if you handle that properly)
Edit: Just checked docs and it seems get_node_or_null()
is even better candidate for that. It won't print error if node is not found.