Godot Version
Godot 4
Question
How do I know if a node is present in a scene? I need a check to see if there is a landmark and then the character would be spawned at its position
Godot 4
How do I know if a node is present in a scene? I need a check to see if there is a landmark and then the character would be spawned at its position
Try this:
if has_node(node_name): print("The node is present in the scene")
Replace the node_name
with landmark`s name.
Doesn’t has_node require a NodePath?
If for whatever reason you can’t determine the node path, then you can try these.
Slow way to do it
Search through all the child nodes in the scene and check if it is the node you are looking for.
Fast way to do it
If the node is spawned in the scene, let it register itself to a var in the parent node of the scene. Then just check if the var is null or bnot.
Those are two ways I can think of on the spot.
You also can add the landmark node to a group called “landmark” and use get_tree().get_first_node_in_group("landmark")
, this will return the node if exists or null
if no node in this group are in the scene tree.