← Pixel Dreamer Studio

An enemy that dies once, however many hits land

Let the health decide that it died, once, before any queue_free.

queue_free() only removes a node at the end of the frame, so every pellet of a shotgun blast still hits an enemy that has already died, and each one runs the death code again. In our Godot 4.7 test 8 pellets of 4 damage on a 10 hp enemy ran its death 6 times and gave 300 XP instead of 50. Checking is_queued_for_deletion() fixed the shotgun, but with a death animation before the node is freed, the shots that landed while it played killed it 9 times. A health script that remembers it is dead gave one death and 50 XP in every case.

The code

Save it as dies_once.gd. In the enemy script add var health := preload("res://dies_once.gd").new(10.0), connect health.died.connect(_on_died) in _ready(), and give XP, drop loot and play the death effect in _on_died(). Bullets and explosions call enemy.health.hit(4.0). hit() returns true only for the hit that kills, so a weapon can count kills too.

extends RefCounted
## Health that dies exactly once, however many hits land in the same frame
## or while the death animation plays. Give each enemy one:
##   var health := DiesOnce.new(10.0)
##   health.died.connect(_on_died)          # XP, loot, death effect: once
##   # bullets, pellets, explosions:
##   enemy.health.hit(4.0)
## queue_free() does not remove a node until the end of the frame, and a death
## animation keeps it around longer, so the hits after the killing one still land.

signal died

var hp: float
var dead := false


func _init(max_hp: float) -> void:
	hp = max_hp


func hit(amount: float) -> bool:  # true only for the hit that kills
	if dead:
		return false  # already dying: no second death, no second reward
	hp -= amount
	if hp > 0.0:
		return false
	hp = 0.0
	dead = true
	died.emit()
	return true

What we measured

An enemy with 10 hp that gives 50 XP when it dies. A shotgun blast of 8 pellets of 4 damage in one frame, and then a death animation of 50 frames (0.8 s at 60 fps) with a shot landing every 6 frames. Two runs gave the same numbers.

Why it happens

queue_free() marks the node and frees it after the frame, so the node is still valid for the rest of that frame: every pellet that comes after the killing one finds it with 0 hp or less and runs the same death branch. A death animation stretches that window to the length of the animation. Checking hp <= 0 is not enough on its own, because it is true for every hit after the first one. The script keeps a dead flag, so only the first hit to cross zero can emit died.

What to change

What the player sees when it dies: Zombie Survival VFX for Godot 4 has Zombie Rise from the ground, Zombie Death in smoke or toxic, Infection Fog, Spore Cloud, Molotov, Road Flare and a campfire Safe Zone. Play Zombie Death from _on_died() and it plays once.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Zombie Survival VFX for Godot 4 has Zombie Rise from the ground, Zombie Death in smoke or toxic, Infection Fog, Spore Cloud, Molotov, Road Flare and a campfire Safe Zone.

More how-tos for Zombie Survival VFX for Godot 4: A fire or poison zone that keeps hurting while you stand in it

More how-tos: Game systems