A way to check and see if a node exists or not

Godot Version

4.3

Question

Hello there, title says it all. Is there a way to make sure a variable that once was a node exists or not? does if !Nodepath == null work? because i have a hurt function and sometimes it tries to access a node that’s already gone and when it doesn’t find it the entire game crashes, can i stop it from doing that?
Here is the code:


And the error:
Screenshot From 2024-11-13 20-06-46
And the variables:
Screenshot From 2024-11-13 20-09-29

has_node("path_to_node")

I think you should have written it like this

if camera != null and TargetNode != null 

There is also this method you can take advantage of:
Node get_node_or_null(path: NodePath) const

Fetches a node by NodePath. Similar to get_node, but does not generate an error if path does not point to a valid node.

Can i use it in a if statement or do i have to use it like a get_node() function?

because i assigned the nodes by exporting the variables

Try
if get_node_or_null(Nodepath) != null:

Try this first

Replace with your line 18 in the screenshot,
Probably the simplest

Tried it, same result.

1 Like

I did if get_node_or_null(cam1) != null and get_node_or_null(TargetNode) != null:
And got this error: Invalid type in function 'get_node_or_null' in base 'Node (health_controller.gd)'. Cannot convert argument 1 from Object to NodePath.

cam1 is camera

You should give path to the node not the reference

Path to the scene or path to the files?

Path of the node like ../nodeA etc

Could you provide the code where queue_free is happening?

Ok, so i did it for the camera but the TargetNode is an exported variable, like it sometimes can it be the player and other times it’s the enemy. like @export var TargetNode = CharacterBody2D.new() so it doesn’t have a fixed path.

What you could do is you in _ready
You could store the path of the node in a variable
Its probably called get_path() or something like that.
So you could write

targetPath = TargetNode.get_path()

And use that later.

it’s a signal:
if current_health < 1: TargetNode.get_node("BasicCharacterBodyController").gravity_active = false TargetNode.get_node("BasicCharacterBodyController").movement_active = false TargetNode.get_node("CollisionShape2D").queue_free() var hitzone = TargetNode.get_node("hitzone/CollisionShape2D_hitbox") if !hitzone == null: hitzone.queue_free() TargetNode.velocity.x = 0 TargetNode.velocity.y = 0 TargetAnimationControllerNode.queue_free() TargetAnimationNode.pause() #get_node("AnimationPlayer").play("death") if TargetNode.name=='Player': TargetNode.get_tree().reload_current_scene() else: emit_signal("Dead")
first it checks for the health and then it does the basic things like removing the collision shape then it sends a signal, this way i can have different death animations. queue_free() happens in a signal

Make sure it queue frees only once, kinda hard to read without formatting

Oh, Sorry… i forgot to format it.

So i did targetPath = TargetNode.get_path() (in the ready function)
for targetnode:

func _ready() -> void:
	targetPath = TargetNode.get_path()
	targetpath1 = get_node_or_null(targetPath)

and for the camera i did:

@onready var cam = get_tree().current_scene
@onready var cam1 = cam.get_node_or_null("AdvancedCamera2D")

and it’s still giving me the same erorr: Attempt to call function 'is_in_group' in base 'previously freed' on a null instance.

it’s like completely ignoring this check

		if cam1 != null and targetpath1 != null: 
1 Like