Skip to content

Game.Buildings.BuildingStateEfficiencySystem

Assembly: Assembly-CSharp
Namespace: Game.Buildings

Type: class

Base: GameSystemBase

Summary:
BuildingStateEfficiencySystem updates per-building Efficiency buffer factors based on building state flags. It runs a Burst-compiled IJobChunk (BuildingStateEfficiencyJob) over entities matching an EntityQuery (Updated + Building + Efficiency, excluding Deleted and Temp). For each building it sets three efficiency factors: - Disabled — if the building has the BuildingOption.Inactive option. - Abandoned — if the entity or chunk has the Abandoned tag. - Destroyed — if the entity or chunk has the Destroyed tag.

The job writes directly to the dynamic Efficiency buffer using a BufferTypeHandle and uses ComponentTypeHandle reads for Building, Abandoned and Destroyed. The system schedules the job in parallel via JobChunkExtensions.ScheduleParallel and updates its Dependency accordingly.

Fields

  • private EntityQuery m_BuildingQuery
    Used to select building entities that the system processes. Constructed in OnCreate to require entities with Updated, Building and an Efficiency buffer and to exclude Deleted and Temp. The system calls RequireForUpdate(m_BuildingQuery) so it only runs when matching entities exist.

  • private TypeHandle __TypeHandle
    Compiler-generated helper struct instance that stores ComponentTypeHandle and BufferTypeHandle fields used by the job. __TypeHandle.__AssignHandles(ref state) is called (from OnCreateForCompiler) to initialize the handles for the current SystemState.

Properties

  • None.

Constructors

  • public BuildingStateEfficiencySystem()
    Default public constructor. Marked with [Preserve] on lifecycle methods; the system is instantiated by the ECS world/engine. No special runtime initialization beyond the base constructor.

Methods

  • protected override void OnCreate()
    Creates and stores the EntityQuery used by the system: GetEntityQuery(ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.ReadWrite(), ComponentType.Exclude(), ComponentType.Exclude());
    Calls RequireForUpdate(m_BuildingQuery) so the system only runs when there are matching entities.

  • protected override void OnUpdate()
    Prepares and schedules BuildingStateEfficiencyJob:

  • Obtains ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle and BufferTypeHandle via InternalCompilerInterface.Get* wrappers (using stored __TypeHandle fields and the checked SystemState reference).
  • Constructs the job struct and schedules it with JobChunkExtensions.ScheduleParallel(jobData, m_BuildingQuery, base.Dependency).
  • Assigns the returned JobHandle to base.Dependency.

  • protected override void OnCreateForCompiler()
    Compiler-generated initialization used by DOTS codegen. Calls __AssignQueries and __TypeHandle.__AssignHandles to ensure the generated handles/queries are assigned when the system is compiled into the player.

  • private void __AssignQueries(ref SystemState state)
    Compiler stub that can initialize any generated EntityQuery builders. In this class it currently performs no query population (contains a no-op EntityQueryBuilder call).

  • (Nested) BuildingStateEfficiencyJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    IJobChunk worker; Burst-compiled:

  • Reads Building components via ComponentTypeHandle.
  • Reads presence of Abandoned and Destroyed tags at the chunk level via chunk.Has(ref ...) (these tag checks are done once per chunk).
  • Obtains BufferAccessor for the Efficiency dynamic buffer.
  • For each entity in the chunk:
    • Checks BuildingUtils.CheckOption(nativeArray[i], BuildingOption.Inactive) to determine Disabled state.
    • Calls BuildingUtils.SetEfficiencyFactor(bufferAccessor[i], EfficiencyFactor.Disabled, value) where value is 0f if inactive, else 1f.
    • Calls BuildingUtils.SetEfficiencyFactor(bufferAccessor[i], EfficiencyFactor.Abandoned, value) where value is 0f if chunk has Abandoned tag else 1f.
    • Calls BuildingUtils.SetEfficiencyFactor(bufferAccessor[i], EfficiencyFactor.Destroyed, value) where value is 0f if chunk has Destroyed tag else 1f.
  • The job implements IJobChunk.Execute and forwards to the strongly-typed Execute method.

  • (Nested) TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the component and buffer type handles from the provided SystemState:

  • state.GetComponentTypeHandle(isReadOnly: true)
  • state.GetComponentTypeHandle(isReadOnly: true)
  • state.GetComponentTypeHandle(isReadOnly: true)
  • state.GetBufferTypeHandle()

Usage Example

// The system is registered by the ECS world; you don't normally call it directly.
// Example showing how the system logic sets efficiency factors (conceptual):

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Query created in the system: Updated + Building + Efficiency, exclude Deleted/Temp
    // RequireForUpdate ensures this system only runs when matching entities exist.
}

[Preserve]
protected override void OnUpdate()
{
    // This is effectively what the system schedules:
    var job = new BuildingStateEfficiencyJob {
        m_BuildingType = /* component handle created from SystemState */,
        m_AbandonedType = /* component handle */,
        m_DestroyedType = /* component handle */,
        m_EfficiencyType = /* buffer handle */
    };
    Dependency = JobChunkExtensions.ScheduleParallel(job, m_BuildingQuery, Dependency);
}

// Internally, for each building the job does (conceptual):
// bool inactive = BuildingUtils.CheckOption(building, BuildingOption.Inactive);
// BuildingUtils.SetEfficiencyFactor(effBuffer, EfficiencyFactor.Disabled, inactive ? 0f : 1f);
// BuildingUtils.SetEfficiencyFactor(effBuffer, EfficiencyFactor.Abandoned, chunkHasAbandoned ? 0f : 1f);
// BuildingUtils.SetEfficiencyFactor(effBuffer, EfficiencyFactor.Destroyed, chunkHasDestroyed ? 0f : 1f);

Notes: - Efficiency is a dynamic buffer type; the system requires that buffer to be present on building entities. - The job is Burst-compiled and scheduled to run in parallel over chunks — it is optimized for performance and avoids per-entity structural changes. - BuildingUtils contains helper methods referenced here (CheckOption and SetEfficiencyFactor) — ensure those remain compatible if modifying efficiency semantics.