Skip to content

Game.LifePathEventSystem

Assembly: Assembly-CSharp
Namespace: Game.Triggers

Type: class

Base: GameSystemBase

Summary:
Manages creation and cleanup of LifePath events for followed citizens. The system consumes LifePathEventCreationData enqueued by other systems or code and either spawns LifePathEvent entities (using an event archetype) or forwards "chirp" events to the CreateChirpSystem. It also maintains a "followed" set of citizens (limited by kMaxFollowed), tracks per-citizen LifePathEntry buffers, and cleans up events when a followed citizen is deleted or unfollowed. Internally it schedules two jobs: - CreateLifePathEventJob: dequeues creation requests, creates LifePathEvent entities or enqueues chirps, and appends LifePathEntry to the sender's buffer. - CleanupLifePathEntriesJob: adds Deleted components for event entities referenced from LifePathEntry buffers when a followed entity is deleted.


Fields

  • public static readonly int kMaxFollowed
    Maximum number of citizens that can be followed simultaneously (50).

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem used to read the current simulation/frame index.

  • private ModificationEndBarrier m_ModificationBarrier
    Barrier used to obtain an EntityCommandBuffer for making structural changes (create/delete components/entities).

  • private CreateChirpSystem m_CreateChirpSystem
    Reference to the CreateChirpSystem used to forward chirp-type life path events.

  • private EntityQuery m_FollowedQuery
    Query matching currently followed citizens (Followed, excluding Temp andDeleted).

  • private EntityQuery m_DeletedFollowedQuery
    Query matching followed citizens that are marked Deleted (used to clean up referenced events).

  • private EntityQuery m_TimeDataQuery
    Query to access the TimeData singleton.

  • private EntityArchetype m_EventArchetype
    Archetype used when creating new LifePathEvent entities.

  • private NativeQueue<LifePathEventCreationData> m_Queue
    Persistent native queue that other code/systems write LifePathEventCreationData into for this system to consume each update.

  • private JobHandle m_WriteDependencies
    Tracks job dependencies from writers that write to m_Queue; combined into scheduling so queue writes and reads are synchronized.

  • private TypeHandle __TypeHandle
    Internal container of cached type handles (buffer/component lookups) used to schedule jobs.

  • public bool m_DebugLifePathChirps { get; set; }
    Public property used to enable debug behavior where chirps are produced even if the sender has no LifePathEntry buffer (useful for debugging).

Properties

  • public bool m_DebugLifePathChirps { get; set; }
    When true, the system will produce chirps even if the sender does not have a LifePathEntry buffer. Default false. Useful for debugging chirp generation.

Constructors

  • public LifePathEventSystem()
    Default constructor (preserved). System is initialized in OnCreate; constructor itself is empty besides standard system initialization.

Methods

  • protected override void OnCreate() : System.Void
    Initializes internal references and queries:
  • Gets SimulationSystem, ModificationEndBarrier, CreateChirpSystem.
  • Creates entity queries for followed entities and time data.
  • Creates the event archetype for LifePathEvent.
  • Allocates the persistent NativeQueue used to receive LifePathEventCreationData.
  • Calls RequireForUpdate on the TimeData query and disables the system by default (Enabled = false).

  • protected override void OnGamePreload(Colossal.Serialization.Entities.Purpose purpose, GameMode mode) : System.Void
    Enables or disables the system based on the current GameMode. Keeps the base preload behavior.

  • protected override void OnDestroy() : System.Void
    Completes pending write dependencies and disposes the persistent NativeQueue, then calls base.OnDestroy().

  • public NativeQueue<LifePathEventCreationData> GetQueue(out JobHandle deps) : NativeQueue
    Returns the internal queue for producers to write LifePathEventCreationData into. Outputs the job handle representing current write dependencies (deps). Asserts that the system is Enabled when called.

  • public void AddQueueWriter(JobHandle handle) : System.Void
    Combines an external writer JobHandle into the system's m_WriteDependencies. Use this when a producer schedules a job that writes to the returned queue to ensure proper synchronization.

  • public bool FollowCitizen(Entity citizen) : System.Boolean
    Begins following a citizen entity if the current number of followed citizens is below kMaxFollowed. Adds a Followed component, adds a LifePathEntry buffer to the citizen, and marks the entity Updated. The Followed component stores a priority based on the current simulation frame and whether following started while the citizen was a child. Returns true on success, false when the follow limit has been reached.

  • public bool UnfollowCitizen(Entity citizen) : System.Boolean
    Stops following a citizen. Removes the Followed component, marks any LifePathEvent entities referenced by the citizen's LifePathEntry buffer with Deleted, removes the LifePathEntry buffer, and marks the citizen Updated. Returns true if the citizen was being followed, false otherwise.

  • protected override void OnUpdate() : System.Void
    Main scheduling logic run each frame:

  • Schedules CleanupLifePathEntriesJob (parallel IJobChunk) for followed-but-deleted citizens to mark referenced event entities as Deleted.
  • Prepares and schedules CreateLifePathEventJob to dequeue creation requests and create events or forward chirps. The Create job:
    • Reads LifePathEventData prefabs via ComponentLookup.
    • Uses BufferLookup to test for LifePathEntry buffers on senders.
    • Creates new LifePathEvent entities for non-chirp events and appends LifePathEntry to the sender buffer.
    • Enqueues chirps to CreateChirpSystem when the event prefab is marked as a chirp.
    • Adds Updated to sender whenever something is processed for it.
  • Manages JobHandles and registers producers with the modification barrier and CreateChirpSystem; updates m_WriteDependencies.

  • private void __AssignQueries(ref SystemState state) : System.Void
    Compiler helper that assigns (empty) queries on compiler path; used by OnCreateForCompiler.

  • protected override void OnCreateForCompiler() : System.Void
    Compiler helper that calls __AssignQueries and assigns cached TypeHandle handles.

  • Nested types (jobs):

  • CreateLifePathEventJob : IJob
    Dequeues LifePathEventCreationData and:
    • If the referenced LifePathEventData is a chirp, enqueues a ChirpCreationData into m_ChirpQueue.
    • Otherwise, if the sender has a LifePathEntry buffer, creates a LifePathEvent entity (using m_EventArchetype), sets its LifePathEvent component with the event prefab, target, and date (computed from TimeData and simulation frame), and appends a LifePathEntry to the sender's buffer. Adds Updated to the sender in all processed cases. The job uses the m_LifePathEventDatas ComponentLookup and m_LifePathEntries BufferLookup.
  • CleanupLifePathEntriesJob : IJobChunk
    Iterates LifePathEntry buffers in chunks and, for each referenced event entity, adds a Deleted component via a parallel EntityCommandBuffer. This is used when followed citizens become Deleted so their referenced events are cleaned up.

Usage Example

// Example: follow a citizen and enqueue a LifePathEvent creation from another system or authoring code.

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Enable system when appropriate (this system normally enables in OnGamePreload for game mode)
    base.Enabled = true;
}

// Somewhere in mod code that has a reference to the system:
var lifePathSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<Game.Triggers.LifePathEventSystem>();
Entity someCitizen = /* obtain a citizen entity */;
bool started = lifePathSystem.FollowCitizen(someCitizen);
if (started)
{
    // Queue a life-path event to be created next update
    JobHandle writerDeps;
    var queue = lifePathSystem.GetQueue(out writerDeps);

    // Example enqueue (make sure you construct valid entities/prefabs for these fields)
    var creation = new LifePathEventCreationData
    {
        m_EventPrefab = /* prefab entity index */,
        m_Sender = someCitizen,
        m_Target = Entity.Null
    };
    queue.Enqueue(creation);

    // If you enqueued from a job, call AddQueueWriter with that job's handle.
    lifePathSystem.AddQueueWriter(writerDeps);
}