Skip to content

Game.Tools.GenerateAggregatesSystem

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

Type: class

Base: GameSystemBase

Summary:
GenerateAggregatesSystem is a game ECS system that turns CreationDefinition entities with AggregateElement buffers into live aggregate entities (instances of prefabs/aggregate archetypes). It collects chunks of new/updated definitions and chunks of deleted/temporary aggregates, then schedules a Burst-compiled job (CreateAggregatesJob) to: - gather previously deleted aggregate entities into a map keyed by prefab, - reuse deleted aggregates where possible (removing Deleted, marking Updated, setting Temp component), - or create new aggregate entities from an AggregateNetData archetype and attach PrefabRef/Temp, - copy AggregateElement buffers into the created/reused aggregate entity and mark referenced edges with Highlighted and BatchesUpdated. The system uses a ModificationBarrier1 command buffer to apply structural changes from the job and relies on component lookups and cached type handles for fast access. It requires CreationDefinition+AggregateElement+Updated query to run.


Fields

  • private ModificationBarrier1 m_ModificationBarrier
    Holds the modification barrier system used to create an EntityCommandBuffer for applying structural changes (create/set/add/remove components) from the scheduled job.

  • private EntityQuery m_DefinitionQuery
    Query selecting entities that define aggregates to (re)create: readonly CreationDefinition, readonly AggregateElement buffer, and readonly Updated marker. The system requires this query to run.

  • private EntityQuery m_DeletedQuery
    Query selecting aggregate entities that were marked Deleted and contain AggregateElement and Temp markers. These chunks are scanned to build a lookup of deleted aggregates for potential reuse.

  • private TypeHandle __TypeHandle
    A small cached struct that stores EntityTypeHandle, ComponentTypeHandle/BufferTypeHandle and ComponentLookup instances. It is assigned in OnCreateForCompiler to avoid repeatedly fetching type handles at runtime.

Properties

  • None. The system does not expose public properties.

Constructors

  • public GenerateAggregatesSystem()
    Default public constructor. No custom initialization beyond what OnCreate performs.

Methods

  • protected override void OnCreate()
    Initializes the system: obtains the ModificationBarrier1 via World.GetOrCreateSystemManaged, builds the two entity queries (definitions and deleted aggregates), and registers the definition query as required for the system to run (RequireForUpdate).

  • protected override void OnUpdate()
    Collects definition and deleted archetype chunk lists asynchronously, schedules the Burst IJob CreateAggregatesJob with combined dependencies, disposes the temporary chunk lists with the returned job handle, registers the job handle with the modification barrier, and updates base.Dependency.

  • protected override void OnCreateForCompiler()
    Compiler helper that assigns queries and type handles for the system (calls __AssignQueries and TypeHandle.__AssignHandles). Present to satisfy code generated patterns for systems.

  • private void __AssignQueries(ref SystemState state)
    (Compiler-assisted) placeholder for assigning EntityQueryBuilder queries; in this class it creates/Disposes a temporary builder. Kept for generated-code compatibility.

Nested types (summarized):

  • CreateAggregatesJob (private struct, [BurstCompile], implements IJob)
    Job executed on worker thread to:
  • Build a NativeParallelMultiHashMap of deleted aggregate entities keyed by prefab (FillDeletedAggregates).
  • For each CreationDefinition chunk, determine temp flags (Create/Delete/Select/Modify/Essential) based on CreationDefinition fields and possibly the original entity's PrefabRef.
  • If a deleted aggregate for the prefab exists, reuse it: remove Deleted, set Temp, add Updated. Otherwise, create a new entity using AggregateNetData.m_Archetype and set PrefabRef and Temp.
  • Copy the AggregateElement buffer into the target aggregate entity's buffer and add Highlighted and BatchesUpdated to each referenced edge. The job uses EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, BufferTypeHandle, ComponentLookup, ComponentLookup, and an EntityCommandBuffer passed from the modification barrier.

  • TypeHandle (private struct)
    Caches the EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle instances and ComponentLookup instances used by the CreateAggregatesJob. Provides __AssignHandles(ref SystemState) to fetch those handles from the SystemState.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization performed by this system:
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier1>();
    m_DefinitionQuery = GetEntityQuery(ComponentType.ReadOnly<CreationDefinition>(),
                                       ComponentType.ReadOnly<AggregateElement>(),
                                       ComponentType.ReadOnly<Updated>());
    m_DeletedQuery = GetEntityQuery(ComponentType.ReadOnly<AggregateElement>(),
                                    ComponentType.ReadOnly<Temp>(),
                                    ComponentType.ReadOnly<Deleted>());
    RequireForUpdate(m_DefinitionQuery);
}

Notes and implementation details: - The system schedules a Burst-compiled IJob (not an IJobEntity) and communicates structural changes via an EntityCommandBuffer from ModificationBarrier1. - Reuse of deleted aggregates is attempted using a NativeParallelMultiHashMap keyed by prefab Entity to avoid unnecessary allocations and maintain references from external code. - The CreateAggregatesJob explicitly marks edges (value.m_Edge) with Highlighted and BatchesUpdated components to ensure visuals/meshes are updated after aggregate creation. - TypeHandle and __AssignQueries are present to match Unity's generated/compiled patterns for better performance and deterministic handle usage.