Personally, I would not recommend caching the node you are about to delete as a way to determine how many enemies have been slain. Not only does it have issues with references being deleted when the node is deleted, but from a memory management perspective, it can make things confusing for Godot.
Instead, what I would recommend doing is either using a integer to track how many enemies of each type have been slain or appending an ID that represents the enemy type slain to an array.
For integers, the code is simple:
# Wherever you are storing the data for how many have been slain...
var blue_enemies_slain = 0
var red_enemies_slain = 0
#####################################
# then in the code for the enemy (different script):
var ID = "red"
func _process(delta):
if (health <= 0):
# add one to the count based on ID
if (ID == "red"):
# EnemyCounter is the node that has the script snippet above
EnemyCounter.red_enemies_slain += 1
print ("Red enemies slain: ", EnemyCounter.red_enemies_slain)
elif (ID == "blue"):
EnemyCounter.blue_enemies_slain += 1
print ("Blue enemies slain: ", EnemyCounter.blue_enemies_slain)
else:
print ("Unknown enemy type slain! ID is ", ID)
queue_free()
For the array, its similar but knowing how much of each type takes a "bit" more processing:
# Wherever you are storing the data for how many have been slain...
var enemies_slain = []
func get_enemies_slain(desired_id):
var enemy_count = 0
for ID in enemies_slain:
if (ID == desired_id):
enemy_count += 1
return enemy_count
#####################################
# then in the code for the enemy (different script):
var ID = "red"
func _process(delta):
if (health <= 0):
# Add to the array count
EnemyCounter.enemies_slain.append(ID)
# print the number of enemies slain in each ID:
print ("Red enemies slain: ", EnemyCounter.get_enemies_slain("red"))
print ("Blue enemies slain: ", EnemyCounter.get_enemies_slain("blue"))
print ("Enemies slain that have the same ID as this enemy: ", EnemyCounter.get_enemies_slain(ID))
queue_free()
Both methods have their ups and downs, so I'd go with whatever works for you.