Skip to content

Game.Tools.RecentClearSystem

Assembly: Game (inferred)
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
RecentClearSystem is an ECS system that periodically removes the Recent component from entities whose Recent.m_ModificationFrame is older than a hard-coded threshold. It queries for entities that have Recent and are not Deleted or Temp, and schedules a parallel IJobChunk (ClearRecentJob) that issues RemoveComponent calls through an EndFrameBarrier EntityCommandBuffer. The system depends on SimulationSystem.frameIndex and only runs removal logic when the simulation frame index is past a specific cutoff (262,144). The system uses an update interval (GetUpdateInterval) of 65,536 to limit how often it is considered for updates by the scheduler.


Fields

  • private SimulationSystem m_SimulationSystem
    This holds a reference to the SimulationSystem (retrieved in OnCreate) so the system can read the current simulation frameIndex to decide whether to run the clearing job.

  • private EndFrameBarrier m_EndFrameBarrier
    A reference to the EndFrameBarrier system used to create an EntityCommandBuffer. The Job uses the ECB's ParallelWriter to remove the Recent component safely from worker threads.

  • private EntityQuery m_RecentQuery
    The EntityQuery used to find entities with the Recent component and without Deleted or Temp. The query is required for update (RequireForUpdate) and is used to schedule the job over matching chunks.

  • private TypeHandle __TypeHandle
    Internal per-system cached type handles (contains an EntityTypeHandle and a read-only ComponentTypeHandle) used to obtain the actual runtime handles via InternalCompilerInterface in OnUpdate/OnCreateForCompiler.

  • (nested) private struct TypeHandle
    Contains:

  • EntityTypeHandle __Unity_Entities_Entity_TypeHandle
  • ComponentTypeHandle<Recent> __Game_Tools_Recent_RO_ComponentTypeHandle
    and an __AssignHandles(ref SystemState) method to initialize those handles from the SystemState.

  • (nested) private struct ClearRecentJob
    IJobChunk job that performs the per-chunk iteration and removes the Recent component from entities whose m_ModificationFrame is older than the threshold. See Methods for details.

Properties

  • This class exposes no public properties.

Constructors

  • public RecentClearSystem()
    Default parameterless constructor (preserved). No special initialization beyond what GameSystemBase does; the system performs required setup in OnCreate.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 65536. The scheduler will use this to determine how frequently the system is considered for update. This large value means the system is not evaluated every frame.

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

  • Retrieves SimulationSystem and EndFrameBarrier from the World via GetOrCreateSystemManaged.
  • Creates the EntityQuery that matches ComponentType.ReadOnly() and excludes Deleted and Temp.
  • Calls RequireForUpdate(m_RecentQuery) so the system only runs if there are matching entities.

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

  • Checks if m_SimulationSystem.frameIndex >= 262,144; if not, it does nothing.
  • If the check passes, schedules a ClearRecentJob in parallel over m_RecentQuery:
    • The job receives the EntityTypeHandle and ComponentTypeHandle via InternalCompilerInterface and the system's cached TypeHandle.
    • m_SimulationFrame is set to the current simulation frame index.
    • m_CommandBuffer is created from m_EndFrameBarrier.CreateCommandBuffer().AsParallelWriter().
  • The returned JobHandle is added to the EndFrameBarrier with AddJobHandleForProducer so the ECB is kept alive until the job completes.
  • The system's base.Dependency is updated to the job handle.

  • (private) void __AssignQueries(ref SystemState state)
    Generated helper used at compile-time to populate/validate queries. The compiled method in this class creates a temporary EntityQueryBuilder and immediately disposes it — the effective query setup is performed in OnCreate above.

  • protected override void OnCreateForCompiler()
    Compiler helper that calls __AssignQueries and assigns the cached TypeHandle instances for use by the generated job scheduling code.

  • (nested) ClearRecentJob: IJobChunk implementation

  • Fields:
    • [ReadOnly] public EntityTypeHandle m_EntityType — used to GetNativeArray() from a chunk.
    • [ReadOnly] public ComponentTypeHandle<Recent> m_RecentType — used to GetNativeArray() from a chunk.
    • [ReadOnly] public uint m_SimulationFrame — current simulation frame passed in by the system.
    • public EntityCommandBuffer.ParallelWriter m_CommandBuffer — ECB parallel writer used to remove the Recent component from entities.
  • Behavior (Execute):
    • Computes a cutoff: num = m_SimulationFrame - 262,144.
    • Iterates all entities in the chunk, reading Recent via nativeArray2.
    • For each entity where recent.m_ModificationFrame <= num, issues m_CommandBuffer.RemoveComponent(unfilteredChunkIndex, entity).
    • The job runs in parallel across chunks (ScheduleParallel).
  • The job includes a bridge Execute method to satisfy the IJobChunk interface.

Notes about constants: - The code uses two magic constants: - 262,144 (1 << 18) — used as the age threshold in frames. Entities with Recent.m_ModificationFrame older than (currentFrame - 262,144) will have their Recent component removed. - 65,536 (1 << 16) — returned by GetUpdateInterval, controlling system scheduling frequency.

Usage Example

This system is normally created and managed by the ECS/World. To see its effect, create an entity with a Recent component and set its m_ModificationFrame to an old value; when the SimulationSystem.frameIndex has advanced past the threshold, RecentClearSystem will schedule a job to remove the Recent component via the EndFrameBarrier.

// Example: Create an entity with a Recent component that will be removed
var em = World.DefaultGameObjectInjectionWorld.EntityManager;
var archetype = em.CreateArchetype(typeof(Recent)); // plus any other components you want
var e = em.CreateEntity(archetype);

// Suppose you can access the current simulation frame (from SimulationSystem)
uint currentFrame = simulationSystem.frameIndex;

// Set the Recent modification frame to older than the cutoff (currentFrame - 262144 - 1)
em.SetComponentData(e, new Recent { m_ModificationFrame = currentFrame - 262145 });

// After SimulationSystem.frameIndex >= 262144 and the system runs, RecentClearSystem
// will remove the Recent component from this entity using the EndFrameBarrier ECB.

Additional notes: - The system relies on EndFrameBarrier to defer structural changes (RemoveComponent) until it is safe; the job writes to the ECB's ParallelWriter to avoid structural changes inside worker threads. - If you need to keep Recent components longer or shorter, you would adjust the magic constant (262,144) in the system (modding the code) or adjust how you set m_ModificationFrame when creating/updating Recent entries.