Fix 5: guard against stale ModelCache callbacks on re-channeled spirits
When a spirit is returned to hand and re-channeled, the old in-flight ModelCache.load_model() callback could fire after the unit was pooled and re-created, clobbering the new placeholder and causing the 3D model to not appear. The browser logged "!is_inside_tree()" hundreds of times/sec as a result. Fix: add a per-unit load-generation counter (_unit_load_gen). Each call to add_unit_mesh() increments the counter and captures it in the closure. The callback checks on entry whether the generation still matches; if not, it treats itself as stale, frees the downloaded model_node, and returns without touching any scene state. 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:
@@ -32,6 +32,7 @@ var turn_glows: Dictionary = {} # iid → CPUParticles3D
|
||||
var turn_glow_rings: Dictionary = {} # iid → MeshInstance3D
|
||||
var materialize_until: Dictionary = {} # iid → Time.get_ticks_msec deadline
|
||||
var unit_owner_ids: Dictionary = {} # iid → accountId
|
||||
var _unit_load_gen: Dictionary = {} # iid → int (incremented each model-load start; stale callbacks bail out)
|
||||
|
||||
var _active_tweens: Array[Tween] = [] # shader tweens tracked for kill-on-rebuild
|
||||
|
||||
@@ -357,6 +358,10 @@ func add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false,
|
||||
needs_face_rot[iid] = needs_face_rotation
|
||||
|
||||
var body_ref: Variant = weakref(body)
|
||||
# Stale-callback guard: bump generation so any previous in-flight load
|
||||
# for this iid will bail out when it fires.
|
||||
_unit_load_gen[iid] = _unit_load_gen.get(iid, 0) + 1
|
||||
var _my_gen: int = _unit_load_gen[iid]
|
||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||
# Guard: body may have been freed by a state rebuild before load finished.
|
||||
var b: StaticBody3D = body_ref.get_ref() as StaticBody3D
|
||||
@@ -364,6 +369,11 @@ func add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false,
|
||||
if model_node != null:
|
||||
model_node.queue_free()
|
||||
return
|
||||
# Stale-callback guard: unit was removed/re-added since this load started.
|
||||
if _unit_load_gen.get(iid, 0) != _my_gen:
|
||||
if model_node != null:
|
||||
model_node.queue_free()
|
||||
return
|
||||
if model_node == null:
|
||||
# Download failed — schedule a background retry.
|
||||
schedule_model_retry(iid, model_url, 1, model_etag)
|
||||
|
||||
Reference in New Issue
Block a user