Skip to content

Game.DistrictModifierInitializeSystem

Assembly:
Game (inferred from namespace and project structure)

Namespace:
Game.Policies

Type:
class

Base:
GameSystemBase

Summary:
Initializes and updates district modifier buffers for newly created districts based on active policies. The system scans entities that have a District component and a buffer of Policy entries (and that are marked Created and not Temp), computes the district option mask and per-modifier values according to policy data (including slider-adjusted values), and writes those results into the District and DistrictModifier buffers. Work is dispatched as an IJobChunk (InitializeDistrictModifiersJob) to run in parallel across matching chunks.


Fields

  • private EntityQuery m_CreatedQuery
    Query used to select created district entities (ComponentType.ReadOnly, ComponentType.ReadWrite, ComponentType.Exclude). The system requires this query for update and schedules the job over it.

  • private DistrictModifierRefreshData m_DistrictModifierRefreshData
    Helper struct holding ComponentLookup/BufferLookup handles (PolicySliderData, DistrictOptionData, DistrictModifierData). This is initialized in OnCreate and updated each OnUpdate before scheduling the job. It contains the logic to compute option masks and to build the DistrictModifier buffer from policy data.

  • private TypeHandle __TypeHandle
    Container for per-frame TypeHandle instances (BufferTypeHandle read-only, ComponentTypeHandle read/write, BufferTypeHandle read/write). It has an __AssignHandles method used by the compiler-time OnCreateForCompiler routine to bind type handles to the SystemState.

Properties

  • None (this system exposes no public properties)

Constructors

  • public DistrictModifierInitializeSystem()
    Parameterless constructor (empty). The important initialization occurs in OnCreate where the DistrictModifierRefreshData is constructed and the entity query is created/required for update.

Methods

  • protected override void OnCreate()
    Initializes m_DistrictModifierRefreshData with this system (so it captures component/buffer lookups) and builds the m_CreatedQuery (Created + District, excluding Temp). Calls RequireForUpdate on the query to ensure the system runs only when matching entities exist.

  • protected override void OnUpdate()
    Updates the DistrictModifierRefreshData lookups, populates an InitializeDistrictModifiersJob struct with the current type handles and refresh data, and schedules it as a parallel JobChunk against m_CreatedQuery. The resulting job iterates all matching chunks and updates District and DistrictModifier buffers based on active policies.

  • protected override void OnCreateForCompiler()
    Compiler helper invoked for generated code paths; assigns queries and type handles via __AssignQueries and __TypeHandle.__AssignHandles. Present for DOTS/ECS code-generation compatibility.

  • private void __AssignQueries(ref SystemState state)
    Private helper used by compiler-generated code. Currently creates and disposes a temporary EntityQueryBuilder (no runtime effect in the provided source).

Nested/Related types and their important methods (summary):

  • InitializeDistrictModifiersJob (private struct, IJobChunk)
  • Execute(in ArchetypeChunk chunk, ...)
    Iterates entities in the chunk, reading District and Policy buffers and writing updated District and DistrictModifier buffers. For each entity that has policies, calls m_DistrictModifierRefreshData.RefreshDistrictOptions to compute the district's m_OptionMask and, if a DistrictModifier buffer exists, calls RefreshDistrictModifiers to populate modifiers.

  • DistrictModifierRefreshData (public struct)

  • Constructor DistrictModifierRefreshData(SystemBase system)
    Initializes ComponentLookup, ComponentLookup, and BufferLookup in read-only mode from the given system.
  • void Update(SystemBase system)
    Updates internal lookup handles (must be called each frame before using them in jobs).
  • void RefreshDistrictOptions(ref District district, DynamicBuffer policies)
    Resets and computes district.m_OptionMask by OR-ing option masks from DistrictOptionData for each active policy.
  • void RefreshDistrictModifiers(DynamicBuffer modifiers, DynamicBuffer policies)
    Clears the modifiers buffer, iterates active policies that have DistrictModifierData buffers, and for each DistrictModifierData item computes a delta (using PolicySliderData if present) and adds it to the modifiers by calling AddModifier.
  • private static void AddModifier(DynamicBuffer modifiers, DistrictModifierData modifierData, float delta)
    Ensures the modifiers buffer is large enough for the modifier type index, then applies the delta according to modifierData.m_Mode:

    • Relative: scales and accumulates as value.m_Delta.y = value.m_Delta.y * (1 + delta) + delta
    • Absolute: accumulates to value.m_Delta.x
    • InverseRelative: converts delta to inverse-relative using 1 / max(0.001, 1 + delta) - 1, then applies same formula as Relative
  • TypeHandle (private struct)

  • void __AssignHandles(ref SystemState state)
    Assigns BufferTypeHandle (read-only), ComponentTypeHandle (read/write), and BufferTypeHandle (read/write) from the provided SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // initialize the helper that holds Lookups for policy/option/modifier data
    m_DistrictModifierRefreshData = new DistrictModifierRefreshData(this);

    // create and require the query so the system runs only when needed
    m_CreatedQuery = GetEntityQuery(ComponentType.ReadOnly<Created>(),
                                    ComponentType.ReadWrite<District>(),
                                    ComponentType.Exclude<Temp>());
    RequireForUpdate(m_CreatedQuery);
}

[Preserve]
protected override void OnUpdate()
{
    // update lookup handles before scheduling the job
    m_DistrictModifierRefreshData.Update(this);

    var jobData = new InitializeDistrictModifiersJob
    {
        m_DistrictModifierRefreshData = m_DistrictModifierRefreshData,
        m_PolicyType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Policies_Policy_RO_BufferTypeHandle, ref base.CheckedStateRef),
        m_DistrictType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Areas_District_RW_ComponentTypeHandle, ref base.CheckedStateRef),
        m_DistrictModifierType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Areas_DistrictModifier_RW_BufferTypeHandle, ref base.CheckedStateRef)
    };

    // schedule the job to initialize modifiers for created districts
    base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_CreatedQuery, base.Dependency);
}

Notes and implementation details: - The system relies on several data components/buffers: Policy (buffer on district entities), PolicySliderData, DistrictOptionData, and buffers of DistrictModifierData attached to policy archetypes. Policies marked with PolicyFlags.Active are considered. - Slider-based policies compute a normalized fraction inside the policy slider range and interpolate modifier ranges accordingly (math.lerp). If a policy has no slider data, the modifier uses the minimum value from the modifierData.m_Range. - The AddModifier logic ensures modifiers buffer slots exist up to the highest modifier type index and applies different accumulation semantics depending on ModifierValueMode (Relative, Absolute, InverseRelative).