← Pixel Dreamer Studio

A pooled enemy that comes back as fresh as a new one

_ready() only runs the first time a node enters the tree. Reset the node yourself, or ask Godot to run _ready() again.

If you reuse enemies by taking them out of the tree and adding them back, _ready() does not run again, so whatever it set up stays the way it was when the enemy died. In our Godot 4.7 test an enemy that set health = max_health in _ready() had 30 health on its first life and 0 on its second and third. Call request_ready() before adding it back, which made _ready() and @onready run on every life, or give it a reset() the pool calls.

The code

Save it as node_pool.gd, make one per enemy type with var pool = preload("res://node_pool.gd").new(preload("res://enemy.tscn")), spawn with var e = pool.take(self) and, instead of queue_free(), call pool.give_back(e) when it dies. If the enemy has a reset() method, the pool calls it each time the enemy comes back. If not, the pool calls request_ready().

extends RefCounted
## Reuses enemies, pickups or effects instead of freeing and loading them again.
## Godot runs _ready() only the first time a node enters the tree, so a node
## that comes back from the pool keeps everything it had when it left.
## take() calls reset() on a reused node, or request_ready() if it has none,
## so it comes back as fresh as a new one.

var scene: PackedScene
var _waiting: Array[Node] = []


func _init(packed: PackedScene) -> void:
	scene = packed


func take(parent: Node) -> Node:
	var n: Node
	if _waiting.is_empty():
		n = scene.instantiate()
		parent.add_child(n)
		return n
	n = _waiting.pop_back()
	if not n.has_method("reset"):
		n.request_ready()  # _ready() and @onready run again when it is added
	parent.add_child(n)
	if n.has_method("reset"):
		n.reset()
	return n


func give_back(n: Node) -> void:  # instead of queue_free()
	if n.get_parent() != null:
		n.get_parent().remove_child(n)
	_waiting.append(n)


func clear() -> void:  # free everything still waiting, when the level ends
	for n in _waiting:
		n.free()
	_waiting.clear()

What we measured

An enemy with 30 max health that sets its health in _ready(). Each life it was added to the scene, its health was read, it was killed (health 0) and taken out again, three times. Two runs gave the same numbers.

Why it happens

Godot calls _ready() once per node, the first time it and its children are in the tree, and marks it as done. Taking the node out and putting it back runs _enter_tree() every time, but not _ready(), and @onready variables are set at the same moment as _ready(). request_ready() clears that mark for the next time the node is added. A reset() is often the better choice, because _ready() may also connect signals or create child nodes, which you do not want twice.

What to change

What the player sees when they come back: Spawn, Death & Respawn VFX for Godot 4 has Enemy Spawn in Fire, Frost and Shadow, Summon Circle, Grave Rise, Player Respawn, Ally Revive and crystal and ash deaths. Its effects can be reused too: PDVFX.play(fx) plays a one-shot again instead of spawning a new one.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Spawn, Death & Respawn VFX for Godot 4 has Enemy Spawn in Fire, Frost and Shadow, Player Respawn and Ally Revive, and one-shots you can play again with PDVFX.play(fx).

More how-tos for Spawn, Death & Respawn VFX for Godot 4: Dissolve shader for 3D models · Death particles disappear when the enemy is freed

More how-tos: Game systems