diff --git a/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd b/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd index 28d7854..357effd 100644 --- a/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd +++ b/Client/origin-client/scenes/game/components/units/unit_mesh_builder.gd @@ -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)