Skip to content

Game.Tutorials.TutorialPolicyAdjustmentTriggerSystem

Assembly: Assembly-CSharp
Namespace: Game.Tutorials

Type: class

Base: TutorialTriggerSystemBase

Summary:
System that drives tutorial triggers related to policy adjustments. It monitors policy entities and policy-modify events to detect when a policy becomes active (or meets configured trigger conditions) and then marks tutorial triggers as pre-completed or completed and issues unlock events via the tutorial unlock archetype. The system uses EntityQueries and native arrays (Allocator.TempJob) and dispatches changes through an EntityCommandBuffer obtained from the barrier system.


Fields

  • private Unity.Entities.EntityQuery m_AdjustmentQuery
    Internal query used to read Modify event components (Event + Modify) that indicate policy changes.

  • private Unity.Entities.EntityQuery m_PolicyQuery
    Query that finds Policy entities (Policy component) excluding Temp and Deleted; used for first-time detection of already active policies when triggers become active.

  • private Unity.Entities.EntityArchetype m_UnlockEventArchetype
    Archetype created to spawn unlock Event entities (Event + Unlock) that are used with TutorialSystem.ManualUnlock to notify the tutorial system of unlocks.

Properties

  • This class does not expose public properties.

Constructors

  • public TutorialPolicyAdjustmentTriggerSystem()
    Default constructor. Marked with Preserve in the source. No custom construction logic beyond Unity Entities system initialization.

Methods

  • protected override void OnCreate() : System.Void
    Initializes the system:
  • Builds m_ActiveTriggerQuery via GetEntityQuery(ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.Exclude).
  • Initializes m_PolicyQuery (Policy, exclude Temp/Deleted).
  • Initializes m_AdjustmentQuery (Event + Modify).
  • Creates m_UnlockEventArchetype containing Event and Unlock components.
  • Calls RequireForUpdate(m_ActiveTriggerQuery) so the system runs only when active triggers exist. Notes: Runs once when the system is created; marked with Preserve in the original code.

  • protected override void OnUpdate() : System.Void
    Main update loop. Two major flows:

  • First-time check when triggers changed:
    • If base.triggersChanged and there are policy entities, it collects policy entities and active trigger data/entities into NativeArray (Allocator.TempJob).
    • For each active trigger it calls FirstTimeCheck; if true, it adds TriggerPreCompleted to the trigger entity and calls TutorialSystem.ManualUnlock to produce an unlock event.
    • Disposes all NativeArray allocations after use.
  • Adjustment detection:

    • If m_AdjustmentQuery is not empty, it collects active triggers, trigger entities, and Modify adjustments into NativeArray (Allocator.TempJob).
    • For each active trigger it calls Check(data, adjustments); if true, it adds TriggerCompleted to the trigger entity and calls TutorialSystem.ManualUnlock to produce an unlock event.
    • Disposes all NativeArray allocations after use. Notes: Uses an EntityCommandBuffer obtained from m_BarrierSystem to perform entity changes safely in the ECS update.
  • private bool Check(PolicyAdjustmentTriggerData data, NativeArray<Modify> adjustments) : System.Boolean
    Checks the Modify adjustment events to determine whether any matching adjustment fulfills the trigger:

  • Iterates adjustments and returns true when:
    • data.m_TargetFlags includes PolicyAdjustmentTriggerTargetFlags.District,
    • the adjusted entity has a District component,
    • the adjustment has the PolicyFlags.Active flag set,
    • the trigger data requires the Activated flag (data.m_Flags includes PolicyAdjustmentTriggerFlags.Activated).
  • Otherwise returns false.

  • private bool FirstTimeCheck(PolicyAdjustmentTriggerData data, NativeArray<Unity.Entities.Entity> policyEntities) : System.Boolean
    Checks existing policy entities (policies already present on entities) to detect if a trigger should be pre-completed immediately when it becomes active:

  • For each policy entity, reads the DynamicBuffer (isReadOnly: true).
  • If data targets Districts, the entity has a District component, AnyActive(buffer) is true, and the trigger requires Activated, returns true.
  • Otherwise returns false.

  • private bool AnyActive(Unity.Entities.DynamicBuffer<Policy> policies) : System.Boolean
    Scans a Policy buffer and returns true if any Policy entry has PolicyFlags.Active set.

Usage Example

// Create a PolicyAdjustment trigger entity that watches for policy activations in districts
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
var arch = em.CreateArchetype(typeof(PolicyAdjustmentTriggerData), typeof(TriggerActive));
Entity trigger = em.CreateEntity(arch);

// Configure the trigger to target districts and respond to Activations
em.SetComponentData(trigger, new PolicyAdjustmentTriggerData {
    m_TargetFlags = PolicyAdjustmentTriggerTargetFlags.District,
    m_Flags = PolicyAdjustmentTriggerFlags.Activated
});

// When a policy is activated on a district, a Modify event should be produced
// (example: create an Event + Modify entity with m_Entity = the district entity and m_Flags = PolicyFlags.Active).
// The system will detect the Modify and add TriggerCompleted to `trigger`, and call TutorialSystem.ManualUnlock.

Notes and implementation details: - The system allocates NativeArray with Allocator.TempJob in OnUpdate and disposes them within the same frame — ensure no leaks by following that pattern. - Changes to entities are enqueued through an EntityCommandBuffer from m_BarrierSystem to keep thread-safety and proper ordering. - Components referenced: PolicyAdjustmentTriggerData, TriggerActive, TriggerCompleted, TriggerPreCompleted, Policy, Temp, Deleted, Event, Modify, Unlock, District, PolicyFlags, PolicyAdjustmentTriggerTargetFlags, PolicyAdjustmentTriggerFlags. - The system relies on TutorialSystem.ManualUnlock to create tutorial unlock events using the m_UnlockEventArchetype.