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.
- No check, death code then queue_free(): all 8 pellets landed, the death ran 6 times, 300 XP. With the death animation: 9 deaths, 450 XP.
- Skipping hits once
is_queued_for_deletion()is true: 1 death and 50 XP from the shotgun. With the death animation it does not help, because queue_free() has not been called yet: 9 deaths, 450 XP. - The script: 1 death and 50 XP for the shotgun and for the death animation, with all 8 pellets and the 8 later shots still landing on the body.
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
- Bullets that should pass through the body during the animation: also turn off its collision with set_deferred("disabled", true) on the CollisionShape3D in _on_died(). Life steal that keeps healing from dead enemies: life steal and overkill.
- Enemies that come back from a pool already dead: reset a pooled enemy. An enemy still attacking a dead player: a target that was freed.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene