Game.Triggers.TriggerSystem
Assembly: Assembly-CSharp (game)
Namespace: Game.Triggers
Type: class
Base: GameSystemBase, IDefaultSerializable, ISerializable
Summary:
TriggerSystem collects trigger actions (queued by other systems and jobs), evaluates them against trigger prefabs and conditions, and enqueues concrete creations such as chirps, life‑path events, radio tags and tutorial activations. It runs a Burst-compiled IJob (TriggerActionJob) over a NativeArray of TriggerAction items, respects per‑prefab frequency limits, checks trigger conditions, and dispatches creation requests to the appropriate producer systems. The system also persists a map of last-created frames per trigger prefab for interval limiting and periodically triggers telemetry for city stats.
Fields
-
private SimulationSystem m_SimulationSystem
System reference providing the current simulation frame index. -
private TriggerPrefabSystem m_TriggerPrefabSystem
System providing trigger prefab lookup / metadata used to map trigger actions to concrete prefabs. -
private ModificationEndBarrier m_ModificationBarrier
Modification barrier used to register producer job handles when the job enqueues work that will modify entity state later. -
private List<NativeQueue<TriggerAction>> m_Queues
List of temporary NativeQueueinstances that external writers create via CreateActionBuffer(). These are drained each update. -
private JobHandle m_Dependencies
Accumulated job dependencies from external writers that the system must wait on before consuming their queues. -
private CreateChirpSystem m_CreateChirpSystem
Producer system for chirp creation; TriggerSystem enqueues chirp creation requests here. -
private LifePathEventSystem m_LifePathEventSystem
Producer system for life-path events; TriggerSystem enqueues life-path event creation requests here. -
private RadioTagSystem m_RadioTagSystem
Producer system for radio tag events; TriggerSystem enqueues normal and emergency radio tags here. -
private TutorialEventActivationSystem m_TutorialEventActivationSystem
Producer system for tutorial activations; TriggerSystem enqueues tutorial activation requests here. -
private DateTime m_LastTimedEventTime
Timestamp used to track when the last timed telemetry (city stats) event fired. -
private TimeSpan m_TimedEventInterval
Interval between telemetry/timed events (default is 15 minutes). -
private EntityQuery m_EDWSBuildingQuery
EntityQuery used internally (constructed on OnCreate) — here it queries Early Disaster Warning System buildings (used for early-disaster related triggers). -
private NativeParallelHashMap<Entity, uint> m_TriggerFrames
Persistent map of trigger prefab entity -> last simulation frame when it was created. Used to enforce frame-interval limits per prefab. -
private TypeHandle __TypeHandle
Compiler-generated holder of ComponentLookup/BufferLookup handles used when scheduling the Burst job.
Properties
- None (no public properties defined on this type)
Constructors
public TriggerSystem()
Default public constructor. System initialization logic is performed in OnCreate.
Methods
-
protected override void OnCreate()
Initializes system references (SimulationSystem, TriggerPrefabSystem, producer systems), allocates m_Queues and m_TriggerFrames, sets defaults for timed events and queries, and disables the system by default. Called by the framework when the system is created. -
protected override void OnGamePreload(Colossal.Serialization.Entities.Purpose purpose, GameMode mode)
Enables the system only when the game mode is a normal game (mode.IsGame()). -
protected override void OnStopRunning()
Completes outstanding dependencies, disposes any leftover queues and clears the list. Called when system stops running. -
public NativeQueue<TriggerAction> CreateActionBuffer()
Creates and returns a new NativeQueue(Allocator.TempJob) for producers to enqueue TriggerAction items. The queue is tracked in m_Queues and will be consumed on the next update. Asserts that the system is enabled. -
public void AddActionBufferWriter(JobHandle handle)
Registers an external job handle (writer) dependency so TriggerSystem will wait for writers to finish before consuming queues. -
protected override void OnUpdate()
Main update: completes dependencies, collects all queued TriggerAction items into a NativeArray, sets up and schedules the Burst TriggerActionJob (providing many read-only component lookups and producer queues), registers the job handle with producer systems and the modification barrier, and periodically triggers telemetry (Telemetry.CityStats) using m_TimedEventInterval. Also disposes temporary queues and clears m_Queues. -
protected override void OnDestroy()
Disposes the persistent m_TriggerFrames map. -
public void SetDefaults(Context context)
Resets runtime defaults; currently clears m_TriggerFrames. -
public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
Serializes m_TriggerFrames (keys and last-frame values) to the provided writer. Writes count and then each pair. -
public void Deserialize<TReader>(TReader reader) where TReader : IReader
Deserializes m_TriggerFrames from reader. Clears current map and reads count entries; skips Entity.Null entries. -
private void __AssignQueries(ref SystemState state)
Compiler-generated query assignment stub (no runtime query assignments required here). -
protected override void OnCreateForCompiler()
Compiler helper invoked at build time to assign queries and type handles used by the Burst job.
Nested type: TriggerActionJob (Burst-compiled IJob)
TriggerActionJob.Execute()
Iterates over the provided m_Actions array, for each TriggerAction:- Determines target type (building, citizen, road, policy, service building, etc.) by checking component presence on the target entities.
- Uses m_TriggerPrefabData to find matching prefabs for the trigger type / target type / trigger prefab id.
- For each prefab candidate, checks per-prefab interval limit (CheckInterval) and trigger conditions buffer (CheckConditions).
- If passed, calls CreateEntity to enqueue creation into the correct producer queue (chirp, life-path event, radio tag (emergency vs normal), tutorial activation), and records the last-frame into m_TriggerFrames if the prefab has TriggerLimitData.
-
For road traffic-accident triggers, may compute distance to camera using CullingInfo and m_CameraPosition and set triggerAction.m_Value accordingly.
-
TriggerActionJob.CheckInterval(Entity prefab) : bool
Returns whether enough frames have passed since last creation of this prefab according to TriggerLimitData. Reads m_TriggerFrames and returns true if no limit or frame interval satisfied. -
TriggerActionJob.CheckConditions(Entity prefab, TriggerAction action) : bool
If prefab has a TriggerConditionData buffer, iterates conditions (Equals, GreaterThan, LessThan) and validates action.m_Value against each. Returns false if any condition fails. -
TriggerActionJob.CreateEntity(ref Random random, Entity prefab, TriggerAction triggerAction)
Enqueues the actual creation request: chirps go to m_ChirpQueue, life-path events to m_LifePathEventQueue, radio events to m_RadioTagQueue or m_EmergencyRadioTagQueue depending on RadioEventData, and tutorial activation events to m_TutorialTriggerQueue. Also updates m_TriggerFrames when the prefab has TriggerLimitData.
TypeHandle (nested struct)
public void __AssignHandles(ref SystemState state)
Initializes a set of ComponentLookup/BufferLookup fields used by the TriggerActionJob. This is a compiler-generated helper tying the job's lookups to the world state.
Usage Example
// Example: enqueueing a trigger action from some system or job context
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Get the TriggerSystem (game world)
var triggerSystem = World.GetOrCreateSystemManaged<Game.Triggers.TriggerSystem>();
// Create a temporary action queue (NativeQueue<TriggerAction>) that this producer can write to.
var queue = triggerSystem.CreateActionBuffer();
// Build a TriggerAction (fields shown as example — actual available fields are used by game code).
// You must fill valid Entity references and trigger identifiers from your mod context.
queue.Enqueue(new TriggerAction
{
m_TriggerType = /* e.g. TriggerType.Event */,
m_TriggerPrefab = /* prefab Entity or identifier */,
m_PrimaryTarget = /* sender entity */,
m_SecondaryTarget = /* target entity */,
m_Value = 0f
});
// If you wrote to the queue from a job, call triggerSystem.AddActionBufferWriter(jobHandle)
// to register the writer dependency so TriggerSystem waits before reading the queue.
}
Notes: - TriggerSystem relies heavily on the Unity Jobs + Burst + Entities world. When enqueuing from a job, register the job handle with AddActionBufferWriter so the system waits for producers to finish. - TriggerAction, TriggerPrefabData, TriggerConditionData, RadioEventData and other component types referenced here are defined elsewhere in the game's codebase; consult their docs for field semantics when creating TriggerAction items. - The system persists last-trigger frames in m_TriggerFrames and serializes them via Serialize/Deserialize to maintain interval limits across saves.