Attempt to call function '_dead' in base 'null instance' on a null instance.

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

I have a problem that I can´t resolve, I tried everything. I have a “big enemy” when he dead, he call the function “_dead” to kill all his “partners”. The thing is that when I kill a “partner” first, and then kill the “big enemy”, obviusly the "partner that I kill is null because is freed with queue_free when I kill him. I tried with

if  $(node of partner) != null:
partner_dead()
if is_instance_valid($(node of partner)):
partner_dead()
if $(node of aprtner).is_inside_tree():
partner_dead()
if has_node(node of partner)):
partner_dead()

and I tried many others that I can´t remember and I keep getting the error, so how can I check if a node exists?

:bust_in_silhouette: Reply From: albinaask

Greetings.

Generally it is relatively difficult to give help and advice when you don’t supply neither log-report nor relevant parts of the code, but I will do my best!

The first method you use appear to me as if it would work fine, then again It’s difficult to know when you don’t supply the function at hand, but if they were to be siblings of the big enemy one could write something along these lines:

func die():
	var siblings = get_parent().get_children()#get all siblings
	for sibling in siblings:#iterate over all siblings
		if sibling !=self&&sibling.has_method("die"):#aka if it is an enemy but not and thus can die, and it has not found itself
			sibling.queue_free()
	queue_free()#make sure to queue_free oneself last, don't know it is necessary per say to delete oneself last, but oh well

That would be the solution closest to the one you’ve got, another is to use Godot’s built-in group system, something like this:

#for all the enemies that you want to die at the same time
func _ready():
  add_to_group("enemy_group_1")

#only in your big enemy script
func die():
   var enemies = get_tree().get_nodes_in_group("enemy_group_1")
   for enemy in enemies:
      enemy.queue_free()

hi, thanks for taking the time. But i think you don´t understand me (or I don´t understand you) dosen´t matter any other code, the dead function only have self.queue_free(). So my ask is how I can check if a node was freed? because checking with has method, has node, is inside tree, etc dosen´t work because it still trying to acces to a dead funcion that dosen´t exists because is a freed node (is null), obiously beacause I killed before, so how i can check if a $(routeofnode) exists? and if exists call the function _dead .I dont understand why the first thing that I do:

if $enemy != null:
enemy._dead()

don´t work and give me the error that I put in the title, the same happen with has method,has node, etc

Pelli | 2020-06-23 23:35

:bust_in_silhouette: Reply From: Pelli

It is with has_node() but you have to give the path and I was putting $ to get the node, so that is because don´t worked to me, but is working now