How to check if a parent node has a specific node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leandro

I’m working on a shmup. Many enemies need to know the position of the player ship, so I use this:

target = get_node(“…/PlayerShip”).position

Problem is that sometimes my player ship will be removed from the game, when it explodes so enemy scenes won’t find it and crash the game. How to check first if the player ship exists in the parent scene? Is there something like this in Godot?

if node_exists ("../PlayerShip"):
       target = get_node("../PlayerShip").position

Thanks in advance! :slight_smile:

:bust_in_silhouette: Reply From: Thomas Karcher
if has_node("../PlayerShip"): 
    target = get_node("../PlayerShip").position

Alternatively, the ‘is_instance_valid(instance)’ method can be used to check if a previously existing object has been freed: