Skip to content

Game.Triggers.CreateChirpSystem

Assembly: Assembly-CSharp (game)
Namespace: Game.Triggers

Type: class

Base: GameSystemBase

Summary:
CreateChirpSystem is a game system responsible for creating "chirp" entities (in-game social/network posts/events) in response to triggers. It collects recent chirps to avoid duplicate chirps from the same chirper prefab, consumes chirp-creation requests that are queued by other systems or jobs, and spawns chirp entities using a burst-compiled IJob (CreateChirpJob). The system uses Unity ECS patterns: entity queries, NativeQueue for cross-thread requests, NativeParallelHashMap to track recent chirps, and schedules Burst jobs for parallel processing. It also interacts with city statistics and various prefab/component lookups to configure chirp properties (likes, virality, participants, lifetime, etc.).


Fields

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem used to read the current simulation frame (frameIndex). Used when setting creation/inactive frames and scheduling.

  • private CityStatisticsSystem m_CityStatisticsSystem
    Reference to the CityStatisticsSystem used to obtain population/education statistics for like-count calculations.

  • private ModificationEndBarrier m_ModificationBarrier
    Barrier system used to create an EntityCommandBuffer for structural changes (creating chirp entities, adding buffers/components).

  • private JobHandle m_WriteDependencies
    Tracks job dependencies of writers that have enqueued chirp creation requests to the internal queue. Combined into scheduling to ensure correct ordering.

  • private EntityQuery m_PrefabQuery
    Query that selects chirp prefabs (ComponentType.ReadOnly) — used to size temporary data structures.

  • private EntityQuery m_ChirpQuery
    Query that selects existing chirp entities (Chirp + PrefabRef, excluding Temp/Deleted) — used to collect recent chirps.

  • private EntityQuery m_CitizenQuery
    Query that selects citizen entities used to pick random senders when needed.

  • private NativeQueue<ChirpCreationData> m_Queue
    Persistent NativeQueue storing chirp creation requests. Other systems/jobs write ChirpCreationData into this queue; CreateChirpSystem reads/dequeues and spawns chirps on update.

  • private TypeHandle __TypeHandle
    Internal aggregated set of EntityTypeHandle / ComponentTypeHandle / ComponentLookup / BufferLookup used by jobs. Assigned during OnCreateForCompiler / __AssignHandles.

Properties

  • (none exposed)

Constructors

  • public CreateChirpSystem()
    Default constructor. The system's managed initialization is performed in the protected OnCreate override.

Methods

  • protected override void OnCreate()
    Initializes references to required systems (SimulationSystem, CityStatisticsSystem, ModificationEndBarrier), creates entity queries (prefabs, chirps, citizens), initializes the persistent NativeQueue, and disables the system by default (base.Enabled = false). Called when system is created.

  • protected override void OnGamePreload(Colossal.Serialization.Entities.Purpose purpose, GameMode mode)
    Enables the system when in game mode (mode.IsGame()). Called during game preload.

  • protected override void OnDestroy()
    Completes outstanding write dependencies, disposes the persistent queue (m_Queue.Dispose()), and performs base cleanup. Ensures no memory leaks from the NativeQueue.

  • public NativeQueue<ChirpCreationData> GetQueue(out JobHandle deps)
    Returns the NativeQueue used for chirp creation requests and outputs the current JobHandle dependencies that writers should depend on. Asserts the system is enabled (Assert.IsTrue(base.Enabled)). Use this when scheduling jobs that will enqueue chirp requests so you can combine dependencies correctly.

  • public void AddQueueWriter(JobHandle handle)
    Adds a writer job's JobHandle to m_WriteDependencies (combined dependencies). Writers should call this after scheduling so the system knows about writer dependencies.

  • protected override void OnUpdate()
    Main update logic:

  • Builds a temporary NativeParallelHashMap of recent chirps (one entry per chirp prefab) sized by m_PrefabQuery.CalculateEntityCount().
  • If there are existing chirps, schedules CollectRecentChirpsJob (IJobChunk, Burst) to populate the recent chirps map with chirp-prefab -> chirp-entity entries for chirps created recently (within a configured frame window).
  • Builds a NativeList of random citizen chunks for random sender selection.
  • Prepares and schedules CreateChirpJob (IJob, Burst) which dequeues chirp creation requests from m_Queue and spawns chirp entities via an EntityCommandBuffer. CreateChirpJob uses many ComponentLookup/BufferLookup handles and city statistics to configure spawned chirps (likes, viral factor, continuous factor, inactive frame, target likes, participants).
  • Disposes temporary collections (recentChirps, randomCitizenChunks) with the scheduled dependency.
  • Updates m_WriteDependencies and registers the dependency with m_ModificationBarrier (AddJobHandleForProducer).

  • private void __AssignQueries(ref SystemState state)
    Internal method used by compiler-generated OnCreateForCompiler. Creates/assigns queries (presently empty in this compiled representation).

  • protected override void OnCreateForCompiler()
    Compiler helper that assigns handles and queries used by the generated job structs; calls __AssignQueries and __TypeHandle.__AssignHandles.

Nested / Job Types (summaries):

  • CollectRecentChirpsJob : IJobChunk (BurstCompile)
  • Purpose: iterates chirp entities (chunks) and records recent chirps into a NativeParallelHashMap (m_RecentChirps) keyed by chirp prefab to avoid creating duplicate "chirper" chirps within a timeframe.
  • Fields used: EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentLookup, NativeParallelHashMap.ParallelWriter, m_SimulationFrame.
  • Logic: If a chirp's creation frame is within a configured recent window (m_CreationFrame + 18000 >= m_SimulationFrame) and the chirp's prefab has ChirpData, try to add prefab->chirp entity mapping.

  • CreateChirpJob : IJob (BurstCompile)

  • Purpose: dequeues ChirpCreationData requests, determines which chirp prefab to spawn (direct trigger or chirper mapping), checks for duplicates via m_RecentChirps, selects sender (possibly random) and other participants, calculates like/viral/continuous factors (possibly overridden by RandomLikeCountData), and creates chirp entities using an EntityCommandBuffer.
  • Important behaviors:
    • Uses RandomSeed to pick chirp prefabs and random senders.
    • Uses CityStatisticsSystem values (educated/uneducated population) to estimate target likes.
    • Adds created chirp entity to recentChirps if it's a chirper-type chirp so duplicates are prevented that update tick.
    • Appends LifePathEntry to sender's lifepath buffer when the chirp is not a chirper-chirp and sender has a lifepath buffer.
    • Handles specialized selection logic for service/brand chirps, employees, household citizens, or completely random citizen selection (from random citizen chunks).
    • Uses multiple ComponentLookup/BufferLookup handles provided to the job (prefab->component data, citizen component lookups, household buffers, employee buffers, etc.).
  • Private helper methods inside the job:
    • GetChirpPrefab(triggerPrefab, ref Random, out bool isChirperChirp)
    • GetArchetype(prefab)
    • FindSender(sender, target, prefab, ref Random)
    • SelectRandomSender(DynamicBuffer, ref Random)
    • SelectRandomSender(DynamicBuffer, ref Random)
    • SelectRandomSender(ref Random) — selects a random adult citizen from the random citizen archetype chunks (tries randomized draws first, then exhaustive search).

Notes and cautions: - The system expects to be enabled only in game mode (OnGamePreload sets base.Enabled = mode.IsGame()). - GetQueue asserts that the system is enabled. Do not call GetQueue when the system is disabled. - Writers that enqueue into the NativeQueue must combine and register their job dependencies via AddQueueWriter so the system waits for writers before reading the queue. - The system disposes its persistent NativeQueue during OnDestroy; ensure writers complete before world shutdown. - Temporary native containers (NativeParallelHashMap, NativeList) are disposed with the scheduled dependency in OnUpdate.

Usage Example

// Example: enqueue a chirp creation request from another system/job
var createChirpSystem = World.DefaultGameObjectInjectionWorld
    .GetExistingSystemManaged<Game.Triggers.CreateChirpSystem>();

// Ensure the system is enabled (game mode) before writing
JobHandle writerDeps = default;
var queue = createChirpSystem.GetQueue(out writerDeps);

// If scheduling a job that will write to the queue, combine and add its handle:
// createChirpSystem.AddQueueWriter(myWriterJobHandle);

// For simple immediate enqueue on main thread:
queue.Enqueue(new ChirpCreationData {
    m_TriggerPrefab = someTriggerPrefabEntity,
    m_Sender = senderEntity,
    m_Target = targetEntity
});

// If you enqueued from a job, make sure to call AddQueueWriter with that job's handle:
// createChirpSystem.AddQueueWriter(jobHandleThatWroteToQueue);