← Pixel Dreamer Studio

An enemy that stops at the dead player and finds the respawned one

Drop the target when it dies or is freed, and look for a new one in the player group.

An enemy that keeps the player in a variable and checks target != null goes on attacking a player that is dead but still on screen, and when that player is freed and a new one spawns, it holds nothing and never looks for the new one. In our Godot 4.7 test it hit the dead player twice during a 1 second death animation, then never attacked the respawned player. Ask for the target every physics frame, drop it when it is dead, freed or queued for deletion, and search the player group again: the new player was picked up on the first physics frame after it spawned.

The code

Save it as target_keeper.gd and give each enemy one: var keep = preload("res://target_keeper.gd").new(). Every physics frame, var target = keep.get_target(self), and attack or chase only when it is not null. Put your player in the player group, and give it a dead property that is true while its death animation plays.

extends RefCounted
## An enemy's target that is dropped the moment it dies, is freed or is about
## to be freed, and found again in a group when a new one appears, such as a
## player who respawns as a new node. Ask get_target(self) every physics frame
## instead of keeping the node in a variable of your own.

var group := &"player"  # the group the targets are in
var max_distance := 50.0  # only pick targets this close
var node: Node3D = null  # the current target, or null


func get_target(from: Node3D) -> Node3D:
	if not usable(node):
		node = null
		var best := max_distance
		for n in from.get_tree().get_nodes_in_group(group):
			if n is Node3D and usable(n):
				var d: float = from.global_position.distance_to(n.global_position)
				if d <= best:
					best = d
					node = n
	return node


func usable(n) -> bool:  # alive, in the tree, not freed or queued for deletion
	return is_instance_valid(n) and n.is_inside_tree() and not n.is_queued_for_deletion() and n.get("dead") != true

What we measured

A player that is alive for 1 second, then dead for 1 second while its body stays on screen, then freed. One second later a new player spawns. Three enemies attack every 0.5 seconds whenever they have a target. Two runs gave the same numbers.

Why it happens

A variable holds the node it was given and nothing else. It does not know the player died, because dying is your game's idea, not Godot's, and it does not know a new player exists, because the new player is a different node. Once the old node is freed, Godot 4.7 makes the variable compare equal to null, so the check stops the attacks without an error, but the enemy just stands there. There is also a gap: between queue_free() and the end of the frame the node still passes both checks, so code that runs in that frame can hit a player that is about to disappear.

What to change

The moment the player comes back: Holy & Light VFX for Godot 4 has a Resurrection Pillar, Heal Circle, Halo and Light Wings, and a showcase scene built around a resurrection. Play the pillar where the new player spawns, and the enemies find them the next physics frame.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Holy & Light VFX for Godot 4 has a Resurrection Pillar, heal circles, halos and smite beams, and a showcase scene built around a resurrection.

More how-tos for Holy & Light VFX for Godot 4: A glowing circle on the ground · A force field shield with a hit ripple

More how-tos: Game systems