Skip to content

Game.Triggers.EarlyGameOutsideConnectionTriggerSystem

Assembly: Assembly-CSharp
Namespace: Game.Triggers

Type: class

Base: GameSystemBase

Summary:
EarlyGameOutsideConnectionTriggerSystem is a game/system-level helper that detects early-game buildings that do not have an outside connection available and enqueues a NoOutsideConnection trigger action once the game's resource availability system has applied the OutsideConnection resource and a short delay has elapsed. It runs periodically (update interval 64) and schedules a burst-compiled job (TriggerJob) to scan relevant Building components and produce a TriggerAction via the TriggerSystem action buffer. It avoids repeated triggering by setting an internal m_Triggered flag and resets behavior appropriately when a new game is loaded.


Fields

  • private const uint UPDATE_INTERVAL = 64u
    Constant used to indicate the system update interval (returned by GetUpdateInterval).

  • private static readonly float kDelaySeconds = 10f
    Delay in seconds before the system will evaluate and trigger after the applied resource becomes OutsideConnection (converted to frames by multiply by 60 when comparing to SimulationSystem.frameIndex).

  • private EntityQuery m_BuildingQuery
    EntityQuery that selects Building entities for scanning. It is created to include Building and BuildingCondition and exclude Deleted and Temp.

  • private TriggerSystem m_TriggerSystem
    Reference to the TriggerSystem used to create action buffers where TriggerAction items are enqueued.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem used to get the current frameIndex and other simulation timing data.

  • private ResourceAvailabilitySystem m_ResourceAvailabilitySystem
    Reference to the ResourceAvailabilitySystem used to check which resource is currently applied (the system waits for AvailableResource.OutsideConnection).

  • private bool m_Started
    Internal flag set when the start timer has been started (true after a building is observed and the timer is set).

  • private double m_StartTime
    Frame index recorded when the timer was started (taken from SimulationSystem.frameIndex). Used to compare elapsed frames to the configured delay.

  • private bool m_Triggered
    Flag that indicates whether the trigger has already been executed; prevents duplicate triggering.

  • private TypeHandle __TypeHandle
    Holds buffer lookups (ResourceAvailability) used by the job. Populated via __AssignHandles in OnCreateForCompiler.

  • private struct TriggerJob (nested)
    Burst-compiled IJob that performs the actual scanning of buildings on a worker thread. It:

  • Reads a NativeArray.
  • Uses a BufferLookup to check availability buffers for road edges.
  • Enqueues a TriggerAction (NoOutsideConnection) to a NativeQueue created by TriggerSystem when it finds a building whose outside connection availability is <= 0f.
  • Breaks after the first matching building to avoid enqueueing multiple duplicate actions.

  • private struct TypeHandle (nested)
    Contains the read-only BufferLookup and an __AssignHandles method that binds the lookup from the SystemState.

Properties

  • (No public properties defined on this type.)

Constructors

  • public EarlyGameOutsideConnectionTriggerSystem()
    Default constructor (preserve attribute present on class-level methods but constructor is empty in source). Initialization of queries and handles is done in OnCreate / OnCreateForCompiler rather than constructor.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 64 (UPDATE_INTERVAL). Used by the system scheduler to determine how often this system should run.

  • protected override void OnCreate()
    Initializes the system:

  • Builds m_BuildingQuery to select Building entities (with BuildingCondition and excluding Deleted and Temp).
  • Obtains references to TriggerSystem, SimulationSystem, and ResourceAvailabilitySystem from the world (via GetOrCreateSystemManaged).
  • Note: buffer lookups are assigned via OnCreateForCompiler / __AssignHandles.

  • protected override void OnUpdate()
    Main runtime logic executed on the configured interval:

  • If there are buildings (m_BuildingQuery not empty) and the trigger hasn't fired (m_Triggered == false):
    • If the timer hasn't started (m_Started == false), store current frameIndex to m_StartTime and set m_Started = true.
    • If ResourceAvailabilitySystem.appliedResource == AvailableResource.OutsideConnection and enough frames have passed (frameIndex - m_StartTime > kDelaySeconds * 60f), create and schedule the TriggerJob:
    • Fill TriggerJob.m_Buildings by calling m_BuildingQuery.ToComponentDataArray(Allocator.TempJob).
    • Acquire the BufferLookup using InternalCompilerInterface.GetBufferLookup and the __TypeHandle.
    • Acquire an action buffer NativeQueue from m_TriggerSystem.CreateActionBuffer().
    • Schedule the IJob (Burst compiled) and attach it to base.Dependency, then inform TriggerSystem of the writer dependency via AddActionBufferWriter.
    • Set m_Triggered = true to prevent re-triggering.
  • If the building query is empty and the timer had started but the trigger has not fired, reset m_Started = false (stops the pending timer).

  • protected override void OnGameLoaded(Context serializationContext)
    Handles behavior when a game is loaded:

  • For a new game (Purpose.NewGame) it resets m_Started, m_StartTime, and m_Triggered so the system can start fresh.
  • For loaded games (not NewGame) it sets m_Triggered = true to avoid firing the early-game trigger after load.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Compiler-time helper that currently creates and disposes an EntityQueryBuilder temporarily. Present to satisfy generated compiler patterns; queries are actually initialized in OnCreate.

  • protected override void OnCreateForCompiler()
    Compiler helper that calls __AssignQueries(ref base.CheckedStateRef) and binds buffer lookups by calling __TypeHandle.__AssignHandles(ref base.CheckedStateRef). This ensures the TypeHandle's BufferLookup is wired for use via InternalCompilerInterface.GetBufferLookup.

  • private struct TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the BufferLookup from the given SystemState using state.GetBufferLookup(isReadOnly: true). Used by OnCreateForCompiler.

  • private struct TriggerJob.Execute()
    Implementation of the job's Execute method:

  • Iterate all buildings in m_Buildings.
  • For each building, if building.m_RoadEdge != Entity.Null and the availability buffer exists for that road edge and NetUtils.GetAvailability(...) for AvailableResource.OutsideConnection at building.m_CurvePosition <= 0f then:
    • Enqueue new TriggerAction(TriggerType.NoOutsideConnection, Entity.Null, 0f) into m_ActionBuffer.
    • Break (stop scanning after first match).

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Create query to match Building + BuildingCondition, exclude Deleted and Temp:
    m_BuildingQuery = GetEntityQuery(
        ComponentType.ReadOnly<Building>(),
        ComponentType.ReadOnly<BuildingCondition>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>()
    );

    // Obtain system references:
    m_TriggerSystem = base.World.GetOrCreateSystemManaged<TriggerSystem>();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    m_ResourceAvailabilitySystem = base.World.GetOrCreateSystemManaged<ResourceAvailabilitySystem>();
}

Notes and modding tips: - The system relies on ResourceAvailabilitySystem.appliedResource being set to AvailableResource.OutsideConnection before it will attempt to detect missing outside connections. - The time comparison uses frame indices and multiplies seconds by 60 (assumes 60 frames per second in this codebase) — when adjusting delays, account for frames vs. seconds. - The TriggerJob transfers data via NativeArray and BufferLookup and creates an action via TriggerSystem.CreateActionBuffer(). When interacting with TriggerSystem, ensure proper dependency management (AddActionBufferWriter) if you schedule additional jobs that produce triggers.