Apply Fix 4: pool unit nodes instead of allocating fresh ones

Adds _pool_unit() / _acquire_from_pool() keyed by card_type.
_ready() creates an off-tree _pool_root node to hold retired bodies.
_pool_unit() frees MeshInstance3D children immediately then reparents
the StaticBody3D shell to _pool_root; overflows beyond _MAX_POOL_PER_TYPE
are freed() synchronously to cap memory. _acquire_from_pool() fully
resets transform, collision layers, groups and stale metadata before
reparenting back to units_root. rebuild_units() now calls _pool_unit()
instead of queue_free() for normal unit removal. add_unit_mesh() tries
the pool first; fresh allocs still get a new CollisionShape3D added once.
_exit_tree() frees _pool_root (and all pooled children) on scene teardown.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-05-10 09:50:36 -04:00
co-authored by Claude Happy
parent 3c3e479878
commit 4f8aba5116
@@ -35,6 +35,65 @@ var unit_owner_ids: Dictionary = {} # iid → accountId
var _active_tweens: Array[Tween] = [] # shader tweens tracked for kill-on-rebuild
const _MAX_POOL_PER_TYPE := 6 # overflow beyond this is freed immediately
var _unit_pool: Dictionary = {} # card_type (String) -> Array[StaticBody3D]
var _pool_root: Node3D = null
# ── Unit pool ───────────────────────────────────────────────────────────────────
func _ready() -> void:
_pool_root = Node3D.new()
_pool_root.name = "UnitPool"
_pool_root.visible = false
add_child(_pool_root)
## Retire a unit body to the pool instead of freeing it.
## MeshInstance3D children are freed immediately (releases mesh + material refs).
## If the per-type pool is full the body is freed immediately to cap memory.
func _pool_unit(body: StaticBody3D, card_type: String) -> void:
if not is_instance_valid(body):
return
for child: Node in body.get_children():
if child is MeshInstance3D:
child.free()
body.visible = false
body.reparent(_pool_root, false)
if not _unit_pool.has(card_type):
_unit_pool[card_type] = []
var pool: Array = _unit_pool[card_type]
if pool.size() >= _MAX_POOL_PER_TYPE:
body.free()
else:
pool.append(body)
## Pop a body from the per-type pool and fully reset it for reuse.
## Returns null if the pool is empty (caller should allocate fresh).
func _acquire_from_pool(card_type: String) -> StaticBody3D:
if not _unit_pool.has(card_type):
return null
var pool: Array = _unit_pool[card_type]
if pool.is_empty():
return null
var body: StaticBody3D = pool.pop_back()
if not is_instance_valid(body):
return null
body.position = Vector3.ZERO
body.rotation = Vector3.ZERO
body.scale = Vector3.ONE
body.collision_layer = 1
body.collision_mask = 1
for group: String in body.get_groups():
body.remove_from_group(group)
for key: String in body.get_meta_list():
if key not in ["unit_id", "owner_id", "card_type", "team_color", "above_tile"]:
body.remove_meta(key)
body.reparent(units_root, false)
body.visible = true
return body
# ── Incremental rebuild ───────────────────────────────────────────────────────
## Incremental diff: add/remove units without recreating unchanged ones.
@@ -129,7 +188,7 @@ func rebuild_units(state: Dictionary) -> void:
body.queue_free()
, CONNECT_ONE_SHOT)
else:
body.queue_free()
_pool_unit(body, card_type)
unit_bodies.erase(iid)
unit_meshes.erase(iid)
anim_players.erase(iid)
@@ -185,15 +244,30 @@ func add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false,
var exhausted: bool = unit.get("isExhausted", false)
var owner_id: String = _owner_id_for_unit(iid, Game.state)
var body: StaticBody3D = StaticBody3D.new()
var _pool_hit := false
var body: StaticBody3D = _acquire_from_pool(card_type)
var shape: CollisionShape3D
if body == null:
body = StaticBody3D.new()
shape = CollisionShape3D.new()
else:
_pool_hit = true
for child in body.get_children():
if child is CollisionShape3D:
shape = child
break
body.set_meta("unit_id", iid)
body.set_meta("owner_id", owner_id)
body.set_meta("card_type", card_type)
body.set_meta("team_color", team_color)
if not owner_id.is_empty():
unit_owner_ids[iid] = owner_id
if OS.is_debug_build():
var pool_sizes := {}
for ct: String in _unit_pool:
pool_sizes[ct] = _unit_pool[ct].size()
print("UnitMeshBuilder: add_unit iid=%s pool_hit=%s pools=%s" % [iid, _pool_hit, pool_sizes])
var shape: CollisionShape3D = CollisionShape3D.new()
var mi: MeshInstance3D = MeshInstance3D.new()
var mat: StandardMaterial3D = StandardMaterial3D.new()
mat.roughness = 0.65
@@ -234,11 +308,13 @@ func add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false,
if exhausted:
mat.albedo_color = mat.albedo_color.darkened(0.45)
body.add_child(shape)
if not _pool_hit:
body.add_child(shape)
body.add_child(mi)
body.set_meta("above_tile", above_tile)
body.position = wp + Vector3(0, tile_base + tile_h + above_tile, 0)
units_root.add_child(body)
if not _pool_hit:
units_root.add_child(body)
unit_meshes[iid] = mi
unit_bodies[iid] = body
if card_type != "Ruin":
@@ -683,6 +759,9 @@ func _exit_tree() -> void:
if is_instance_valid(t):
t.kill()
_active_tweens.clear()
if is_instance_valid(_pool_root):
_pool_root.free()
_unit_pool.clear()
func _collect_materialize_surfaces(node: Node, surfaces: Array[Dictionary]) -> void: