Skip to content

Game.Tools.ApplyAreasSystem

Assembly: Assembly-CSharp.dll
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
ApplyAreasSystem is an ECS system used by the game's area/tool pipeline to "apply" temporary area entities (Temp) to their original area entities. It resolves temporary-owner references, keeps SubArea buffers on owner entities in sync when ownership changes, and processes Temp entities according to their flags: creating applied components, updating originals (including copying Node buffers and LocalNodeCache), or marking originals/temp entities as Deleted. The system runs jobs (Burst compiled) and uses a ToolOutputBarrier to perform structural changes via an EntityCommandBuffer in a thread-safe manner.


Fields

  • private ToolOutputBarrier m_ToolOutputBarrier
    The barrier system used to create an EntityCommandBuffer. The command buffer is used by the scheduled jobs to perform structural changes (adding/removing components and buffers) safely from worker threads.

  • private EntityQuery m_TempQuery
    An EntityQuery selecting entities with Temp and Area components. The system requires this query for update (RequireForUpdate), so the system only runs when there are Temp area entities to process.

  • private ComponentTypeSet m_AppliedTypes
    A set of components that will be added to a Temp entity when it is "applied"/created. Initialized to include Applied, Created, and Updated component types so that newly applied area entities receive the proper markers.

  • private TypeHandle __TypeHandle
    Internal container storing the various Entity/Component/Buffer type handles and lookups used by the jobs. It is filled in OnCreateForCompiler to avoid repeated handle lookups inside jobs.

Properties

  • (This system exposes no public properties.)

Constructors

  • public ApplyAreasSystem()
    Default constructor. Marked with Preserve attribute in the compiled code. No custom initialization beyond what the base class performs; actual setup is performed in OnCreate.

Methods

  • protected override void OnCreate()
    Sets up the system: obtains (or creates) the ToolOutputBarrier, builds the m_TempQuery (reads Temp and Area), constructs the m_AppliedTypes (Applied, Created, Updated) and calls RequireForUpdate(m_TempQuery) so the system only updates when there are Temp area entities present.

  • protected override void OnUpdate()
    Main runtime method. It:

  • Prepares a PatchTempReferencesJob that reads Temp and Owner components and uses component/buffer lookups to patch owner references on Temp entities and update SubArea buffers when ownership changes.
  • Schedules a HandleTempEntitiesJob (parallel JobChunk) that inspects each Temp entity and:
    • If TempFlags.Delete is set: marks the original (if any) and the Temp entity as Deleted.
    • If Temp.m_Original != Entity.Null: updates the original entity (removes Hidden if present, adds Updated marker, and optionally copies Node and LocalNodeCache buffers from the Temp to the original).
    • If Temp.m_Original == Entity.Null: "applies" the Temp by removing Temp component and adding the m_AppliedTypes (Applied/Created/Updated) to the entity.
  • Uses ToolOutputBarrier.CreateCommandBuffer().AsParallelWriter() to let jobs emit structural changes and calls m_ToolOutputBarrier.AddJobHandleForProducer(jobHandle) to register the produced jobs with the barrier. The system updates base.Dependency with the scheduled job handle.

  • private void __AssignQueries(ref SystemState state)
    Internal helper called from OnCreateForCompiler. In the compiled code it builds a temporary EntityQueryBuilder and disposes it. It's part of the compiler-generated plumbing to satisfy certain ECS patterns.

  • protected override void OnCreateForCompiler()
    Compiler-time helper that calls __AssignQueries and assigns the TypeHandle's handles (via __TypeHandle.__AssignHandles) against the SystemState. This avoids repeated handle acquisition at runtime.

  • Nested job: PatchTempReferencesJob (private struct, BurstCompile, implements IJobChunk)
    Job responsibilities:

  • Iterates Temp area chunks, reads Temp and Owner components and the Owner entity id.
  • If a Temp entry references an original Entity.Null and is not marked for deletion, it tries to resolve the actual owner: if the owner entity itself has a Temp pointing to an original, and that Temp isn't a Replace, it rewrites the owner reference to follow the original.
  • When the owner changes, it removes the SubArea entry from the previous owner buffer and attempts to add the SubArea to the new owner's SubArea buffer (using CollectionUtils.RemoveValue / TryAddUniqueValue).
  • This keeps the owners' SubArea buffers consistent when ownerships are remapped by Temp data.

  • Nested job: HandleTempEntitiesJob (private struct, BurstCompile, implements IJobChunk)
    Job responsibilities:

  • Iterates Temp area entities and executes per-entity logic depending on the Temp state:
    • Delete: if Temp.m_Original != Entity.Null, add Deleted component to original, and add Deleted to the Temp entity.
    • Update (Temp.m_Original != Entity.Null): remove Hidden from the original if present, add Updated to the original; if the Temp chunk includes Node / LocalNodeCache buffers the job copies Node buffer contents to the original and handles LocalNodeCache buffer copy/add/remove accordingly; finally marks the Temp entity Deleted.
    • Create (Temp.m_Original == Entity.Null): removes Temp component from entity and adds the m_AppliedTypes (Applied, Created, Updated), effectively promoting the Temp to an applied area.
  • Uses EntityCommandBuffer.ParallelWriter to perform these operations in parallel and safely from worker threads.

Usage Example

// Example: mark an Area entity as a new Temp to be created/applied by ApplyAreasSystem
Entity areaEntity = ...; // existing entity representing an area tool selection
var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

// Create a Temp with no original to request creation/application
Temp temp = new Temp
{
    m_Original = Entity.Null,
    m_Flags = 0 // no Delete or Replace flags => this will be created/applied
};
entityManager.AddComponentData(areaEntity, temp);

// If you wanted to request deletion of an applied original:
temp.m_Original = originalAreaEntity;
temp.m_Flags = TempFlags.Delete;
entityManager.SetComponentData(areaEntity, temp);

// The ApplyAreasSystem automatically runs, processes Temp entities and commits changes

Notes / Tips for modders: - Temp, Owner, SubArea, Node, LocalNodeCache, Hidden, Applied, Created, Updated, Deleted, and TempFlags are part of the game's ECS area/tool data model — consult their definitions to understand fields and flag meanings. - ApplyAreasSystem schedules Burst-compiled jobs and performs structural changes through ToolOutputBarrier; do not attempt to perform conflicting structural changes on the same entities from other threads/systems without synchronizing via barriers. - To modify behavior, you can add/modify Temp components on area entities; ApplyAreasSystem will handle creating/updating/deleting originals and keep SubArea buffers in sync.