Skip to content

Game.Policies.BuildingModifierInitializeSystem

Assembly:
Game

Namespace:
Game.Policies

Type:
Class

Base:
GameSystemBase

Summary:
BuildingModifierInitializeSystem is an ECS system that initializes and refreshes building option masks and building modifier buffers for newly created buildings. It queries entities that have a Created tag and a Building component (and exclude Temp), reads any attached Policy buffers, and uses BuildingModifierRefreshData to compute the building's option mask and per-type BuildingModifier values. Work is scheduled as a parallel IJobChunk (InitializeBuildingModifiersJob) so the logic runs efficiently over chunks.


Fields

  • private EntityQuery m_CreatedQuery
    EntityQuery configured to match entities with Created (read-only) and Building (read/write) while excluding Temp. The system requires this query for update (only runs when matching entities exist).

  • private BuildingModifierRefreshData m_BuildingModifierRefreshData
    Helper struct instance that encapsulates ComponentLookup/BufferLookup for policy slider data, building option data and building modifier data. Used on update to refresh its lookups and then applied in the job to compute modifiers/options.

  • private TypeHandle __TypeHandle
    Container of ComponentTypeHandle and BufferTypeHandle fields used to cache type handles for efficient access in jobs. The __AssignHandles method fills these handles from a SystemState.

Properties

  • (none)

Constructors

  • public BuildingModifierInitializeSystem()
    Default constructor. The system is attributed with Preserve for AOT scenarios. Initialization of runtime state happens in OnCreate.

Methods

  • protected override void OnCreate()
    Creates and configures the system: constructs BuildingModifierRefreshData, creates the m_CreatedQuery (Created + Building, exclude Temp) and calls RequireForUpdate(m_CreatedQuery) to ensure the system only updates when matching entities exist.

  • protected override void OnUpdate()
    Updates BuildingModifierRefreshData lookups, constructs an InitializeBuildingModifiersJob with appropriate component/buffer handles, and schedules it as a parallel JobChunk against m_CreatedQuery. The job will iterate created buildings and refresh their option mask and building modifier buffers based on active policies and policy slider adjustments.

  • private void __AssignQueries(ref SystemState state)
    Internal helper used by compiler-generated OnCreateForCompiler; currently constructs a temporary EntityQueryBuilder (no external effect in the decompiled source) — part of the compiler plumbing.

  • protected override void OnCreateForCompiler()
    Compiler-time helper invoked to assign queries and type handles (calls __AssignQueries and __TypeHandle.__AssignHandles). Present for generated code compatibility.

Nested types and their important methods (summarized):

  • InitializeBuildingModifiersJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    IJobChunk implementation that:
  • Retrieves Building components and buffers for Policy and BuildingModifier from the chunk.
  • For each entity in the chunk, if it has any policies, calls RefreshBuildingOptions to compute the building.m_OptionMask, writes it back, and calls RefreshBuildingModifiers to build the BuildingModifier buffer entries for that building.

  • BuildingModifierRefreshData.BuildingModifierRefreshData(SystemBase system)
    Constructor that obtains read-only ComponentLookup, ComponentLookup, and BufferLookup from the provided system.

  • BuildingModifierRefreshData.Update(SystemBase system)
    Updates the internal ComponentLookup/BufferLookup instances (must be called each frame before using them in jobs).

  • BuildingModifierRefreshData.RefreshBuildingOptions(ref Building building, DynamicBuffer<Policy> policies)
    Clears building.m_OptionMask and accumulates option masks from active policies that have BuildingOptionData. Writes the combined option mask into the building.

  • BuildingModifierRefreshData.RefreshBuildingModifiers(DynamicBuffer<BuildingModifier> modifiers, DynamicBuffer<Policy> policies)
    Clears the modifiers buffer and for each active policy that has BuildingModifierData, iterates its BuildingModifierData entries. For each modifier data:

  • If a PolicySliderData exists, computes an interpolated delta based on the policy's adjustment and the modifier's range.
  • Otherwise uses the modifierData.m_Range.min as the delta.
  • Calls AddModifier to fold the delta into the per-type entry in the BuildingModifier buffer.

  • private static void BuildingModifierRefreshData.AddModifier(DynamicBuffer<BuildingModifier> modifiers, BuildingModifierData modifierData, float delta)
    Ensures the modifiers buffer is large enough for the modifier type index, then applies the delta to the stored BuildingModifier value depending on ModifierValueMode:

  • Relative: multiplies existing relative part and accumulates appropriately.
  • Absolute: adds directly to the absolute delta.x.
  • InverseRelative: computes an inverse-relative transform and applies similarly to Relative.

  • TypeHandle.__AssignHandles(ref SystemState state)
    Assigns BufferTypeHandle, ComponentTypeHandle, and BufferTypeHandle by querying the provided SystemState. This is used to populate handles used when scheduling the job.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // BuildingModifierInitializeSystem sets up its query and refresh data in its own OnCreate.
}

// The system's OnUpdate demonstrates scheduling the chunk job that refreshes options/modifiers:
[Preserve]
protected override void OnUpdate()
{
    m_BuildingModifierRefreshData.Update(this);
    var jobData = new InitializeBuildingModifiersJob
    {
        m_BuildingModifierRefreshData = m_BuildingModifierRefreshData,
        m_PolicyType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Policies_Policy_RO_BufferTypeHandle, ref base.CheckedStateRef),
        m_BuildingType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Buildings_Building_RW_ComponentTypeHandle, ref base.CheckedStateRef),
        m_BuildingModifierType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Buildings_BuildingModifier_RW_BufferTypeHandle, ref base.CheckedStateRef)
    };
    base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_CreatedQuery, base.Dependency);
}

Notes: - This system is designed to run only for newly created buildings (the Created tag) and will compute building options and per-type modifiers based on currently active policies and any slider adjustments. - BuildingModifierRefreshData must be updated each frame before use so its ComponentLookup/BufferLookup are valid for the current system state.