The usual way to make an enemy in Godot is a node: a CharacterBody2D with a sprite and a collision shape. That’s great for a dozen enemies and slow for a few thousand. Here’s the approach I used for a crowd system in GDScript, in case it helps anyone making a survivors-like, tower defense or bullet hell.
1. Data, not nodes
Every enemy is an index into packed arrays: position, velocity, HP, type, radius and so on. Moving the whole crowd is one loop over those arrays, with no per-node overhead and no scene tree work.
2. Removing without holes
When an enemy dies, the last one moves into its slot, so the arrays stay packed and loops never skip gaps. Indices change when that happens, so every enemy also gets a uid that never changes, plus a uid-to-index lookup. Use the index inside one frame and keep the uid for anything that follows an enemy over time (a homing shot, a boss).
3. A spatial grid rebuilt every frame with a counting sort
Count the enemies in each cell, turn the counts into start offsets (a running total), then drop each enemy’s index into one flat array. No dictionaries and no arrays of arrays, so rebuilding it every frame is cheap. Crowd spacing, area damage and nearest-enemy queries then only look at nearby cells.
for i in count: # 1. count per cell
cell_of[i] = cell_index(pos[i])
cell_count[cell_of[i]] += 1
var run := 0 # 2. counts -> start offsets
for c in cells:
cell_start[c] = run
run += cell_count[c]
fill.fill(0) # 3. place each enemy
for i in count:
var c := cell_of[i]
cell_items[cell_start[c] + fill[c]] = i
fill[c] += 1
4. One draw call per enemy type
Each type is drawn by a MultiMeshInstance2D. Per-instance custom data carries the animation frame, tint and a hit-flash amount, and a small shader reads it, so thousands of animated, flashing sprites are still one draw call per type.
Measured in Godot 4.3 (whole frame on the CPU, drawing excluded, on a 2.1 GHz server CPU), against the leanest node setup (CharacterBody2D + Sprite2D + CollisionShape2D):
| Enemies | Arrays + grid | One node per enemy |
|---|---|---|
| 1,000 | 3.2 ms | 6.9 ms |
| 2,000 | 6.5 ms | 25.3 ms |
| 4,000 | 12.5 ms | 91.3 ms |
I’ve packaged this up as Horde, a Godot 4 add-on with five behaviors, area queries, a benchmark scene and 63 tests: Horde - Thousands of 2D Enemies for Godot 4 by Spare Parts Studio
The free browser demo has the benchmark if you want to see the difference: Horde - Benchmark & Demo by Spare Parts Studio
Happy to answer questions about any of it.