Skip to content

Game.Tools.ToolClearSystem

Assembly:
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
ToolClearSystem is an ECS system used by the game's tool layer to clear "temporary" entities (entities with the Temp component). It schedules a Burst-compiled IJobChunk (ClearEntitiesJob) that iterates over chunks that contain Temp components and issues entity commands (via a ToolOutputBarrier command buffer) to mark entities as Deleted, update batches, and remove visual flags such as Hidden and Highlighted for original/aggregate entities. The system also handles sub-objects (entities with Owner components) differently so that owner/child relationships and essential flags are respected when clearing.


Fields

  • private ToolOutputBarrier m_ToolOutputBarrier
    This barrier system is used to create an EntityCommandBuffer. The system writes commands into a parallel writer returned by m_ToolOutputBarrier.CreateCommandBuffer(). These commands are later played back by the barrier to modify entities (add/remove components, etc.) safely from jobs.

  • private EntityQuery m_ClearQuery
    An EntityQuery that targets entities containing the Temp component. The query is required for this system to update (RequireForUpdate(m_ClearQuery)), so the system only runs when there are Temp-tagged entities.

  • private TypeHandle __TypeHandle
    Holds cached component/Entity type handles (EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, etc.). These are assigned via __AssignHandles and used to obtain the correct handles for job scheduling.

Properties

  • None (no public properties exposed by this system).

Constructors

  • public ToolClearSystem()
    Default constructor. The system is marked with [Preserve] on lifecycle methods and constructor to ensure it is available to the runtime and not stripped by build tooling.

Methods

  • protected override void OnCreate()
    Initializes the system: obtains (or creates) the ToolOutputBarrier from the world, builds the m_ClearQuery to require Temp, and calls RequireForUpdate(m_ClearQuery) so the system only runs when there are Temp entities. Called once when the system is created.

  • protected override void OnUpdate()
    Main runtime logic. Constructs and schedules the ClearEntitiesJob (Burst-compiled) using JobChunkExtensions.ScheduleParallel with the m_ClearQuery. It populates the job's type handles and component lookups from __TypeHandle via InternalCompilerInterface.GetHandle/GetLookup calls and attaches the parallel command buffer writer from m_ToolOutputBarrier. After scheduling the job, it calls m_ToolOutputBarrier.AddJobHandleForProducer(jobHandle) so the barrier knows to wait for job completion before playing back commands, and it sets base.Dependency = jobHandle.

  • protected override void OnCreateForCompiler()
    Compiler helper called by generated code paths: assigns queries and component handles for the compiler path via __AssignQueries and __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal method used to assign/initialize any queries required by the system for compiler-time setup. In this compiled file it creates and immediately disposes an EntityQueryBuilder; the actual runtime query is set up in OnCreate.

  • private struct TypeHandle
    Holds read-only handles/lookup fields used by the job: EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, ComponentLookup. It provides __AssignHandles(ref SystemState state) which initializes these handles through the SystemState API.

  • private struct ClearEntitiesJob : IJobChunk (Burst-compiled)
    Core worker job that runs per-chunk. Main responsibilities:

  • Read entity arrays and component arrays (Temp, Owner, aggregate buffers).
  • Detect entities that should be cleared and mark them with the Deleted component via the parallel EntityCommandBuffer.
  • For sub-objects (chunks that are "handled as a sub-object", determined by the IsHandledAsSubObject method which checks for Lane, Object minus (Vehicle/Creature/Building/ServiceUpgrade)), it collects both the original owner entities and the sub-objects to delete and treats essential Temp flags specially (temp.m_Flags & TempFlags.Essential).
  • For originals that are hidden (Hidden component present), it queues BatchesUpdated addition and removes Hidden.
  • For aggregate buffers (AggregateElement), it queues BatchesUpdated addition and removes Highlighted from the aggregate entities.
  • Uses stackalloc-backed StackList to collect entities per-chunk and emits batched commands through the parallel command buffer.
  • Implements both the strongly-typed Execute(...) and the explicit IJobChunk.Execute(...) for job execution.
  • Important notes: - The job uses m_HiddenData and m_TempData lookups to inspect component presence on other entities (e.g., temp.m_Original, owner.m_Owner). - The job is marked with [BurstCompile] for performance.

    Usage Example

    [Preserve]
    protected override void OnCreate()
    {
        base.OnCreate();
        // Typical initialization performed by the system: get barrier and require the Temp query
        m_ToolOutputBarrier = base.World.GetOrCreateSystemManaged<ToolOutputBarrier>();
        m_ClearQuery = GetEntityQuery(ComponentType.ReadOnly<Temp>());
        RequireForUpdate(m_ClearQuery);
    }
    

    Notes and tips for modders: - If you need to modify behavior, consider replacing or augmenting ClearEntitiesJob logic to change what is considered "clearable" or how original/aggregate entities are handled. - When issuing additional commands from the job, use the provided m_CommandBuffer (parallel writer) and respect the per-chunk unfilteredChunkIndex when calling AddComponent/RemoveComponent overloads that accept an array. - Be careful when changing component lookups or type handles — they must remain consistent with the job's readonly/read-write intent and be properly initialized from the SystemState before scheduling.