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.
- The same node taken out and added back by hand: health 30, then 0, then 0. _ready() ran once, @onready once, _enter_tree() three times.
- The script, with no reset() on the enemy, so it calls request_ready(): health 30 on all three lives. _ready() and @onready ran three times.
- The script, with a reset() that sets health back: health 30 on all three lives. _ready() ran once and reset() twice.
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
- Put everything a fresh enemy needs in
reset(): health, state, target, timers, and the animation back to idle. - Call
pool.clear()when the level ends, so the enemies still waiting are freed. - Pooling hit sparks instead of enemies: a pool of emitters. Effects cut off when the enemy is freed: particles that disappear with the node. Where to bring them back: off screen.
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
- 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
- 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
- An enemy that dies once, however many hits land
- 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