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:
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.
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
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