Skip to content

Game.DestroyAbandonedSystem

Assembly:
Game (Assembly-CSharp / runtime assembly; inferred)

Namespace:
Game.Simulation

Type:
class

Base:
GameSystemBase

Summary:
System that detects abandoned buildings and, when their abandonment time plus configured delay is reached, queues damage and destroy events and updates building icons. Implemented using a Burst-compiled IJobChunk (DestroyAbandonedJob) scheduled in parallel. Reads BuildingConfigurationData (singleton) to get the abandoned-destroy delay, reads Abandoned components on Building entities, and writes event entities (Damage, Destroy) using pre-created archetypes. Uses EndFrameBarrier to create a parallel EntityCommandBuffer, and IconCommandSystem to update icons. The system has an elevated update interval and only runs when there are matching abandoned buildings.


Fields

  • private SimulationSystem m_SimulationSystem
    Reference to the global SimulationSystem used to read the current simulation frame (frameIndex). Used to determine if an abandoned building should be destroyed.

  • private IconCommandSystem m_IconCommandSystem
    Reference to the IconCommandSystem used to create an IconCommandBuffer for adding/removing notifications/icons on buildings.

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier used to create an EntityCommandBuffer (AsParallelWriter) for creating event entities (Damage, Destroy) at end of frame.

  • private EntityQuery m_AbandonedQuery
    EntityQuery matching Building + Abandoned components and excluding Destroyed. The system requires this query for update.

  • private EntityArchetype m_DamageEventArchetype
    Pre-created archetype for Damage event entities (components: Event, Damage). Used to create damage event entities for abandoned buildings.

  • private EntityArchetype m_DestroyEventArchetype
    Pre-created archetype for Destroy event entities (components: Event, Destroy). Used to create destroy event entities for abandoned buildings.

  • private EntityQuery m_BuildingSettingsQuery
    EntityQuery used to fetch the BuildingConfigurationData singleton (contains m_AbandonedDestroyDelay and notification prefab references).

  • private TypeHandle __TypeHandle
    Compiler-generated container for entity/component type handles used by the job. Assigned on compiler-time path (OnCreateForCompiler).

Properties

  • None.

Constructors

  • public DestroyAbandonedSystem()
    Default constructor. Marked [Preserve]. No runtime initialization is done here; real setup happens in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 4096. The system overrides the update interval so it runs on a sparse cadence (value from the implementation).

  • protected override void OnCreate()
    Creates and caches references to required systems (SimulationSystem, IconCommandSystem, EndFrameBarrier), creates the abandoned-entities query, builds archetypes for Damage and Destroy events, creates query to read BuildingConfigurationData singleton, and calls RequireForUpdate(m_AbandonedQuery) so the system only updates when matching entities exist. Marked [Preserve].

  • protected override void OnUpdate()
    Main runtime logic: builds a DestroyAbandonedJob instance, filling:

  • EntityTypeHandle and ComponentTypeHandle (via InternalCompilerInterface and __TypeHandle)
  • m_BuildingConfigurationData from the building settings singleton
  • event archetypes
  • current simulation frame from m_SimulationSystem.frameIndex
  • command buffers: m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter() and m_IconCommandSystem.CreateCommandBuffer() Then schedules the job with ScheduleParallel over m_AbandonedQuery, stores the returned Dependency, and registers the dependency with the EndFrameBarrier and IconCommandSystem.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper called from OnCreateForCompiler. It prepares (here: disposes a new EntityQueryBuilder) — used by codegen/setup.

  • protected override void OnCreateForCompiler()
    Compiler path that assigns queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles).

Nested types / job:

  • private struct DestroyAbandonedJob : IJobChunk (marked [BurstCompile])
  • Read-only inputs:
    • EntityTypeHandle m_EntityType
    • ComponentTypeHandle m_AbandonedType
    • EntityArchetype m_DamageEventArchetype
    • EntityArchetype m_DestroyEventArchetype
    • BuildingConfigurationData m_BuildingConfigurationData
    • uint m_SimulationFrame
  • Writers:
    • EntityCommandBuffer.ParallelWriter m_CommandBuffer
    • IconCommandBuffer m_IconCommandBuffer
  • Execute: iterates chunk entities and for each Abandoned component checks: if (abandoned.m_AbandonmentTime + m_BuildingConfigurationData.m_AbandonedDestroyDelay <= m_SimulationFrame) If condition true:

    • Create Damage event entity with Damage(entity, float3(1,0,0))
    • Create Destroy event entity with Destroy(entity, Entity.Null)
    • Use IconCommandBuffer to remove Problem and FatalProblem icons for the building
    • Use IconCommandBuffer.Add to add the configured m_AbandonedCollapsedNotification with IconPriority.FatalProblem The job is executed in parallel over the abandoned entities.
  • private struct TypeHandle
    Compiler-generated holder of:

    • EntityTypeHandle __Unity_Entities_Entity_TypeHandle (readonly)
    • ComponentTypeHandle __Game_Buildings_Abandoned_RO_ComponentTypeHandle (readonly) with __AssignHandles(ref SystemState state) to fetch handles.

Notes / behavior summary: - The system reacts only to entities that are Building + Abandoned and not Destroyed. - The destroy decision uses the simulation frame counter and a configured delay from BuildingConfigurationData.m_AbandonedDestroyDelay. - When triggered, the system queues both a Damage event and a Destroy event via the EndFrameBarrier command buffer (parallel writer), and updates notifications via IconCommandSystem. - The job is Burst-compiled and scheduled in parallel for performance. - Update interval is set high (4096) so the runtime scheduler will run this system infrequently; actual frequency depends on phase and scheduler.

Usage Example

// Example: the system's OnUpdate schedules the burst job. This is the core scheduling flow used by the system.
[Preserve]
protected override void OnUpdate()
{
    var jobData = new DestroyAbandonedJob {
        m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
        m_AbandonedType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Buildings_Abandoned_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_BuildingConfigurationData = m_BuildingSettingsQuery.GetSingleton<BuildingConfigurationData>(),
        m_DamageEventArchetype = m_DamageEventArchetype,
        m_DestroyEventArchetype = m_DestroyEventArchetype,
        m_SimulationFrame = m_SimulationSystem.frameIndex,
        m_CommandBuffer = m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter(),
        m_IconCommandBuffer = m_IconCommandSystem.CreateCommandBuffer()
    };

    Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_AbandonedQuery, Dependency);
    m_EndFrameBarrier.AddJobHandleForProducer(Dependency);
    m_IconCommandSystem.AddCommandBufferWriter(Dependency);
}

If you want a quick test: create a Building entity, add an Abandoned component with m_AbandonmentTime set to an earlier frame, set BuildingConfigurationData.m_AbandonedDestroyDelay to 0 or a small value, ensure the system is present in the World, and advance the simulation frame so that m_SimulationSystem.frameIndex >= m_AbandonmentTime + delay. The system will then queue the Damage and Destroy events and update icons.