Object Pooling in Godot

Object Pooling is a game design pattern that breaks away from constantly creating (instantiate()) and destroying (queue_free()) objects. Instead, it pre-allocates a pool of nodes when the scene loads and recycles them as needed throughout gameplay.

Why Use Object Pooling?

In engines running dynamic memory management or garbage collection (GC), frequent allocation and deallocation of nodes creates memory fragmentation and triggers garbage collection sweeps, resulting in noticeable frame stutter during intensive gameplay.

In lower-level non-GC environments, pooling provides predictable memory layout and instant lookups. In game development, it is ideal for high-frequency entities like projectiles, damage numbers, audio players, and enemy waves.

Implementation 1: Single Scene Pooling (Bullets / Projectiles)

Below is a straightforward implementation for a single pooled scene type (e.g., Bullet.tscn).

extends Node

@export var bullet_scene: PackedScene
@export var pool_size: int = 20

var bullet_pool: Array[Node] = []

func _ready() -> void:
	for i in range(pool_size):
		var bullet: Node = bullet_scene.instantiate()
		bullet.hide()
		bullet_pool.append(bullet)
		add_child(bullet)

func get_bullet() -> Node:
	# Check for an inactive pooled bullet
	for bullet in bullet_pool:
		if not bullet.visible:
			bullet.show()
			return bullet

	# Guard: If pool size is exceeded, dynamically expand
	var new_bullet: Node = bullet_scene.instantiate()
	bullet_pool.append(new_bullet)
	add_child(new_bullet)
	return new_bullet

func reset_bullet(bullet: Node) -> void:
	bullet.position = Vector2(-9999, -9999)
	bullet.hide()

Implementation 2: Multi-Mob Spawner Pooling

When spawning multiple distinct enemy or item types from a single spawner, managing individual arrays manually becomes tedious. We can pool multiple PackedScene resource types using a Dictionary keyed by scene resource paths, allowing us to fetch and spawn dynamic, varied enemy types from a shared pool manager.

extends Node

@export var mob_scenes: Array[PackedScene] = []
@export var pool_size_per_mob: int = 10

# Dictionary mapping scene resource path -> Array of pooled nodes
var mob_pools: Dictionary = {}

func _ready() -> void:
	_initialize_pools()

func _initialize_pools() -> void:
	for scene in mob_scenes:
		if not scene:
			continue
			
		var scene_key: String = scene.resource_path
		mob_pools[scene_key] = []
		
		for i in range(pool_size_per_mob):
			var mob: Node = scene.instantiate()
			mob.hide()
			add_child(mob)
			mob_pools[scene_key].append(mob)

func get_mob(scene: PackedScene) -> Node:
	if not scene:
		return null
		
	var key: String = scene.resource_path
	
	# Fallback if pool wasn't registered upfront
	if not mob_pools.has(key):
		mob_pools[key] = []

	# Find available hidden mob in the specific scene pool
	for mob in mob_pools[key]:
		if not mob.visible:
			mob.show()
			return mob

	# Guard / Auto-expand pool if capacity is exceeded
	var new_mob: Node = scene.instantiate()
	add_child(new_mob)
	mob_pools[key].append(new_mob)
	return new_mob

func spawn_random_mob(spawn_position: Vector2) -> Node:
	if mob_scenes.is_empty():
		return null
		
	var random_scene: PackedScene = mob_scenes.pick_random()
	var mob: Node = get_mob(random_scene)
	
	if mob:
		mob.position = spawn_position
		# Reset any mob state here (e.g. mob.reset_health())
		
	return mob

func recycle_mob(mob: Node) -> void:
	mob.hide()
	mob.position = Vector2(-9999, -9999)

Key Features: