Skip to content

Game.Tools.GenerateAreasSystem

Assembly: Assembly-CSharp
Namespace: Game.Tools

Type: class

Base: GameSystemBase

Summary:
System responsible for creating and updating "area" entities from CreationDefinition components. It collects newly defined areas (CreationDefinition + Node buffers) and deleted/temporary areas, reuses deleted area entities when possible, or creates new area entities based on AreaData archetypes. The system adjusts node heights using terrain and water surface data, handles sub-areas (slave areas), copies storage and pseudo-random seed data when needed, and marks temporary/updated flags appropriately. Work is performed in a Burst-compiled IJob (CreateAreasJob) and coordinated with TerrainSystem, WaterSystem and a ModificationBarrier1 command buffer for safe structural changes.


Fields

  • private struct OldAreaData
    Contains m_Prefab, m_Original and m_Owner and serves as a key when matching deleted area entities to new creation requests. Equality and hashcode are implemented to allow use in NativeParallelMultiHashMap lookups.

  • private struct CreateAreasJob
    Burst-compiled IJob that does the bulk of the work: collects deleted areas, iterates creation definitions, reuses deleted entities when appropriate or creates new entities, fills buffers (Node, LocalNodeCache), adjusts node heights (terrain/water), adds/removes components (Owner, Native, Temp, Deleted, Updated, Area, PrefabRef, Storage, PseudoRandomSeed), and creates/manages sub-areas.

  • private struct TypeHandle
    Holds cached EntityTypeHandle, ComponentTypeHandles, ComponentLookup and BufferLookup instances used to build the CreateAreasJob. It has an __AssignHandles method invoked during OnCreateForCompiler to populate handles for the current SystemState.

  • private TerrainSystem m_TerrainSystem
    Reference to the TerrainSystem, used to fetch terrain height data needed to adjust node elevations.

  • private WaterSystem m_WaterSystem
    Reference to the WaterSystem, used to fetch water surface data when area geometry requires water-surface alignment.

  • private ModificationBarrier1 m_ModificationBarrier
    Command buffer provider used to perform structural changes (creating entities, adding/removing components) from the job.

  • private EntityQuery m_DefinitionQuery
    Query to select entities with CreationDefinition and Node buffers (and Updated). Used as the source of area creation requests.

  • private EntityQuery m_DeletedQuery
    Query to select deleted/temporary Area entities that can be reused (Area + Temp + Deleted).

  • private TypeHandle __TypeHandle
    Instance of the nested TypeHandle struct storing the per-frame type/buffer/component lookups the job uses.

Properties

  • None (this system exposes no public properties)

Constructors

  • public GenerateAreasSystem()
    Default constructor. The system is a managed GameSystemBase derived class and has no special constructor logic beyond the default.

Methods

  • protected override void OnCreate()
    Initializes references to TerrainSystem, WaterSystem and ModificationBarrier1; creates entity queries for CreationDefinition (definitions to process) and for deleted areas; registers the definition query as required for updates (RequireForUpdate). This prepares the system to run only when creation definitions exist.

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

  • Asynchronously gathers archetype chunks for definition and deleted queries.
  • Sets up and schedules the CreateAreasJob, passing type handles, component and buffer lookups, terrain/water data, and a command buffer from the modification barrier.
  • Combines dependencies for safety and disposes temporary chunk lists once the job is scheduled.
  • Registers the job as a reader for terrain/water so those systems can synchronize.
  • Adds the job handle to the modification barrier as the producer handle and stores the job handle in base.Dependency.

  • private void __AssignQueries(ref SystemState state)
    A helper that (in the compiled code) would assign any required queries for the compiler path. Here it creates and disposes an EntityQueryBuilder; primarily part of the generated/compiled scaffolding.

  • protected override void OnCreateForCompiler()
    Called by the generated/compiled pipeline to assign queries and type handles for the SystemState used by the compiler/runtime. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • (Nested) CreateAreasJob.Execute()
    Entry point for the job: builds a NativeParallelMultiHashMap of deleted areas via FillDeletedAreas, then iterates definition chunks to process each CreationDefinition via CreateAreas, finally disposes temporary structures.

  • (Nested) CreateAreasJob.FillDeletedAreas(ArchetypeChunk, NativeParallelMultiHashMap)
    Reads entity/chunk data for deleted areas and populates a map keyed by OldAreaData so deleted entities can be reused for new creations when matching prefab/original/owner.

  • (Nested) CreateAreasJob.CreateAreas(ArchetypeChunk, NativeParallelMultiHashMap)
    Processes creation definitions: determines Temp and Area flags, attempts to reuse deleted entities or creates new ones using AreaData archetypes, copies or sets Owner, Native and Storage components; resizes and fills Node and LocalNodeCache buffers; adjusts node positions with terrain or water surface info; handles pseudo-random seed assignment; processes prefab-defined sub-areas by creating or reusing corresponding slave areas and setting Owner relationships.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
    m_WaterSystem = base.World.GetOrCreateSystemManaged<WaterSystem>();
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier1>();
    m_DefinitionQuery = GetEntityQuery(ComponentType.ReadOnly<CreationDefinition>(),
                                       ComponentType.ReadOnly<Node>(),
                                       ComponentType.ReadOnly<Updated>());
    m_DeletedQuery = GetEntityQuery(ComponentType.ReadOnly<Area>(),
                                    ComponentType.ReadOnly<Temp>(),
                                    ComponentType.ReadOnly<Deleted>());
    RequireForUpdate(m_DefinitionQuery);
}

Notes and tips: - The system relies heavily on ECS patterns: archetype chunk iteration, command buffers, buffer/component lookups and scheduling jobs. When modifying or extending behavior, ensure you respect thread-safety (read-only/lookups) and schedule dependencies properly. - To influence node elevation behavior, provide AreaGeometryData flags (OnWaterSurface / PseudoRandom) on area prefabs and ensure Original entities carry Storage or PseudoRandomSeed when appropriate. - Reuse logic: Deleted temporary areas may be reused to avoid allocations; a match is based on prefab, original, and owner (OldAreaData). Permanent flags prevent reuse and force creation of new entities.