Skip to content

Game.ApplyWaterSourcesSystem

Assembly:
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
ApplyWaterSourcesSystem is a compiler-generated ECS system that processes temporary "Temp" entities representing edits to water sources and applies those changes to the original water source entities (Game.Simulation.WaterSourceData). It schedules a parallel IJobChunk (HandleTempEntitiesJob) which, per Temp entity, will either cancel, delete, update or create the corresponding underlying water source entity. The job uses a ToolOutputBarrier command buffer (as a parallel writer) to record component additions/removals/sets (Created, Updated, Deleted, BatchesUpdated, etc.) so changes are applied safely on the main thread. The system also sets up component type handles/lookups required by the job via an internal TypeHandle.


Fields

  • private ToolOutputBarrier m_ToolOutputBarrier
    Holds a reference to a ToolOutputBarrier system used to create a command buffer for the job to record structural changes. Assigned in OnCreate via World.GetOrCreateSystemManaged().

  • private EntityQuery m_TempQuery
    EntityQuery used to select Temp entities (with readonly Game.Simulation.WaterSourceData and excluding PrefabRef). The system calls RequireForUpdate(m_TempQuery) so it only runs when matching entities exist.

  • private TypeHandle __TypeHandle
    Internal container holding the cached EntityTypeHandle, ComponentTypeHandle and ComponentLookup instances required by the job. Populated via __AssignHandles in OnCreateForCompiler.

  • private struct HandleTempEntitiesJob (nested)
    IJobChunk implementation that contains the runtime logic for iterating Temp entities and applying changes to originals. Uses EntityTypeHandle, ComponentTypeHandle, and ComponentLookup (Hidden, Transform, WaterSourceData) plus a EntityCommandBuffer.ParallelWriter.

  • private struct TypeHandle (nested)
    Struct that stores type handles/lookups and provides __AssignHandles(ref SystemState) to initialize them from the system state.

Properties

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

Constructors

  • public ApplyWaterSourcesSystem()
    Default parameterless constructor. Marked [Preserve] in source. No custom initialization beyond base constructor.

Methods

  • protected override void OnCreate()
    Initializes the system: retrieves/creates the ToolOutputBarrier, constructs the m_TempQuery to select Temp entities with WaterSourceData (excluding PrefabRef), and calls RequireForUpdate(m_TempQuery) so the system only updates when matching entities exist.

  • protected override void OnUpdate()
    Creates and schedules the HandleTempEntitiesJob in parallel using JobChunkExtensions.ScheduleParallel, providing the required type handles/lookups (via InternalCompilerInterface.Get* calls) and a parallel EntityCommandBuffer from m_ToolOutputBarrier. The scheduled job handle is added to the ToolOutputBarrier via AddJobHandleForProducer and set as the system dependency.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper that currently creates/cleans up an EntityQueryBuilder(Allocator.Temp). The method exists to satisfy compiler-generated query initialization patterns and is called from OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization hook: calls __AssignQueries and assigns component/entity type handles using __TypeHandle.__AssignHandles(ref base.CheckedStateRef).

  • HandleTempEntitiesJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Core per-chunk job logic:

  • Iterates entities and corresponding Temp components in the chunk.
  • For each Temp:
    • If Temp.m_Flags has TempFlags.Cancel: calls Cancel(...) which, if the original entity has Hidden, removes Hidden and adds BatchesUpdated, then marks the Temp entity as Deleted.
    • Else if Temp.m_Flags has TempFlags.Delete: marks the original as Deleted (if it has WaterSourceData) and marks the Temp entity Deleted.
    • Else if the original entity has Game.Simulation.WaterSourceData: updates the original by copying components/buffers from the Temp entity to the original (using UpdateComponent overloads) and adds Updated to the original; removes Hidden if present and adds BatchesUpdated; finally marks the Temp entity Deleted.
    • Else: Create(...) — removes Temp component from the temp entity and adds Updated and Created to it (i.e., the temp becomes the created real entity).
  • Uses helper methods UpdateComponent(...) for both component data and buffer data to Add/Set/Remove components on the original entity depending on whether the Temp entity has that component/buffer and on whether updateValue is true.

  • HandleTempEntitiesJob.UpdateComponent(int chunkIndex, Entity entity, Entity original, ComponentLookup data, bool updateValue) where T : unmanaged, IComponentData
    Generic helper to sync a component value from the temp entity to the original. Adds, sets, or removes components on the original using the command buffer depending on presence and the updateValue flag.

  • HandleTempEntitiesJob.UpdateComponent(int chunkIndex, Entity entity, Entity original, BufferLookup data, bool updateValue) where T : unmanaged, IBufferElementData
    Generic helper to sync a buffer from the temp entity to the original. Adds, copies, or removes the buffer on the original using the command buffer depending on presence and the updateValue flag.

  • HandleTempEntitiesJob.Cancel/Delete/Update/Create (private helpers)
    Small helper methods that record the appropriate structural/component changes (Deleted, Updated, Created, BatchesUpdated, Hidden removal) on either the original entity or the temp entity using the parallel command buffer.


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();

    // Mirror of the system initialization used by ApplyWaterSourcesSystem
    m_ToolOutputBarrier = base.World.GetOrCreateSystemManaged<ToolOutputBarrier>();
    m_TempQuery = GetEntityQuery(
        ComponentType.ReadOnly<Temp>(),
        ComponentType.ReadOnly<Game.Simulation.WaterSourceData>(),
        ComponentType.Exclude<PrefabRef>());

    RequireForUpdate(m_TempQuery);
}

Notes / Implementation details: - The system is intended to run within Unity ECS/Jobs context (DOTS). It relies on Temp components that reference an original entity (Temp.m_Original) and flags (Temp.m_Flags) indicating Cancel/Delete/etc. - All structural changes are recorded through a ToolOutputBarrier command buffer (AsParallelWriter) so they are applied safely after the job completes. - The job uses ComponentLookup and BufferLookup (component lookup API) which requires proper initialization of handles via the internal TypeHandle struct and the system's CheckedStateRef in generated code.