Skip to content

Game.ObsoleteChirpSystem

Assembly:
Assembly-CSharp.dll (typical Unity game assembly; the system lives in the game's runtime assembly)

Namespace:
Game.Simulation

Type:
public class

Base:
GameSystemBase

Summary:
ObsoleteChirpSystem enforces a maximum number of active "Chirp" trigger entities. When the total number of Chirp entities exceeds the configured LimitSettingData.m_MaxChirpsLimit, the system schedules a job (ObsoleteChirpJob) to sort Chirp entities by their creation frame and mark the oldest entries with the Deleted component. The work uses an EndFrameBarrier command buffer so removal happens safely at end-of-frame, and the job is scheduled as a temp-job using Unity's IJob with Burst compilation for performance.


Fields

  • private EndFrameBarrier m_EndFrameBarrier
    Used to obtain a command buffer (m_EndFrameBarrier.CreateCommandBuffer()) that the job uses to add Deleted components on producer thread. The barrier is also used to register the job handle with AddJobHandleForProducer so the barrier can wait for the scheduled job before playing back commands.

  • private EntityQuery m_ChirpQuery
    EntityQuery that matches Chirp entities: ComponentType.ReadOnly(), excluding Deleted, Game.Triggers.LifePathEvent, and Temp. This query is used to fetch the entity array the job operates on.

  • private EntityQuery m_LimitSettingQuery
    EntityQuery to access the LimitSettingData singleton which contains m_MaxChirpsLimit (the configured maximum allowed chirps).

  • private TypeHandle __TypeHandle
    Container for component lookup handles used by the job. __AssignHandles fills the ComponentLookup used by job and comparer.

(Inner/nested types existence) - ObsoleteChirpJob (nested struct) — job that sorts entities and enqueues Deleted component additions. - ChirpComparer (nested struct) — comparer used to sort Entities by Chirp.m_CreationFrame. - TypeHandle (nested struct) — handles creation for component lookups.

Properties

  • None (this system exposes no public properties)

Constructors

  • public ObsoleteChirpSystem()
    Default constructor. The class uses [Preserve] on lifecycle methods; constructor is empty and the system is initialized in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 65536 in this implementation. This value controls the update interval for the system (how often the system is scheduled relative to the specified SystemUpdatePhase). In this system it effectively makes the update infrequent (game-specific scheduling behavior).

  • [Preserve] protected override void OnCreate()
    Initializes the system:

  • Acquires EndFrameBarrier via base.World.GetOrCreateSystemManaged().
  • Creates m_ChirpQuery to select Chirp components while excluding Deleted, LifePathEvent, and Temp.
  • Creates m_LimitSettingQuery to read the LimitSettingData singleton.
  • Calls RequireForUpdate for both queries so the system only runs when relevant entities/singleton exist.

  • [Preserve] protected override void OnUpdate()
    Main logic:

  • Reads the current chirp count via m_ChirpQuery.CalculateEntityCount().
  • If count exceeds limit (from m_LimitSettingQuery.GetSingleton().m_MaxChirpsLimit), prepares and schedules an ObsoleteChirpJob:
    • Converts m_ChirpQuery to an entity array (Allocator.TempJob) and assigns to job.m_Entities.
    • Provides a ComponentLookup via InternalCompilerInterface.GetComponentLookup using the stored TypeHandle.
    • Copies LimitSettingData singleton into job.m_LimitSettingData.
    • Creates a command buffer from m_EndFrameBarrier and assigns to job.m_CommandBuffer.
  • Schedules the job (IJobExtensions.Schedule) and stores the returned dependency into base.Dependency.
  • Registers the job handle with the EndFrameBarrier via m_EndFrameBarrier.AddJobHandleForProducer(base.Dependency) so the barrier ensures command buffer playback only happens after the job completes.

  • private void __AssignQueries(ref SystemState state)
    Called from OnCreateForCompiler. In this compiled stub it creates and immediately disposes an EntityQueryBuilder with Allocator.Temp — this method is generated/used by the compiler pipeline for query assignment and is part of the compiled-system plumbing.

  • protected override void OnCreateForCompiler()
    Compiler-time helper that calls __AssignQueries and __TypeHandle.__AssignHandles to populate internal handles and queries used by the runtime OnCreate/OnUpdate.

(Inner types — important methods)

  • ObsoleteChirpJob.Execute()
    Burst-compiled job entry point. It:
  • Sorts the job.m_Entities array using ChirpComparer which compares Chirp.m_CreationFrame.
  • Iterates over the earliest entities beyond the allowed m_MaxChirpsLimit and calls m_CommandBuffer.AddComponent(entity) to mark them deleted.

  • ChirpComparer.Compare(Entity x, Entity y)
    Compares two entities by reading their Chirp component's m_CreationFrame (via the ComponentLookup) and returning comparison result. Used to sort chirps by age (oldest first).

  • TypeHandle.__AssignHandles(ref SystemState state)
    Initializes the internal ComponentLookup via state.GetComponentLookup() so that the job/comparer can access component data in a thread-safe manner.

Usage Example

// Ensure a LimitSettingData singleton exists (example - create/modify once at startup)
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
if (!em.HasSingleton<LimitSettingData>())
{
    var e = em.CreateEntity(typeof(LimitSettingData));
    em.SetComponentData(e, new LimitSettingData { m_MaxChirpsLimit = 200 }); // set desired max
}

// The ObsoleteChirpSystem runs automatically (it's a GameSystemBase derived system).
// When the number of Game.Triggers.Chirp entities exceeds the configured limit,
// the system will schedule a job that marks the oldest chirps with the Deleted component
// via an EndFrameBarrier command buffer, ensuring safe end-of-frame removal.

Notes and implementation details: - The system uses Allocator.TempJob for the entity array; the ObsoleteChirpJob is annotated to DeallocateOnJobCompletion. - Sorting is done on the entity array on the worker thread (Burst-compiled) using a custom ChirpComparer that looks up Chirp component data. - Deletion is performed by adding a Deleted component through a command buffer created from EndFrameBarrier, and the barrier receives the job handle to ensure playback waits for the job.