Game.Prefabs.AreaInitializeSystem
Assembly: Game
Namespace: Game.Prefabs
Type: class
Base: GameSystemBase
Summary:
Initializes and validates area-related prefab data when area prefabs are created/updated. This system:
- Populates AreaGeometryData and AreaColorData from AreaPrefabs (sets area type, flags, snap distance, LOD bias, colors, etc.)
- Fills SubObject buffers from AreaSubObjects definitions
- Initializes SpawnableObjectData placeholders and links placeholder prefabs
- Adds SubArea entries for MasterArea slave areas
- Schedules jobs to clean up placeholder references to deleted entities and to validate/subsample subarea nodes
The system uses EntityQueries to detect prefab entities and updated subarea buffers and schedules two Burst-compiled IJobChunk jobs (FixPlaceholdersJob and ValidateSubAreasJob) for safe, parallel chunk processing.
Fields
-
private PrefabSystem m_PrefabSystem
Handles access to prefab definitions and prefab-related helper methods. Retrieved from the world in OnCreate and used to get AreaPrefab and sub-components (AreaSubObjects, MasterArea, SpawnableArea, etc.). -
private EntityQuery m_PrefabQuery
Query matching prefab entities with PrefabData and AreaData and that are either Created or Deleted. Used to find area prefabs that need initialization. -
private EntityQuery m_SubAreaQuery
Query matching entities with Updated tag and read/write SubArea and SubAreaNode buffers. Used to validate subarea node lists after changes. -
private EntityQuery m_PlaceholderQuery
Query matching entities with AreaData and PlaceholderObjectElement buffers (excluding Deleted). Used when scheduling the FixPlaceholdersJob to clean up placeholder lists. -
private TypeHandle __TypeHandle
Container struct that stores EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle instances used for efficient chunk access. It has an internal method __AssignHandles to initialize those handles from a SystemState. -
(Nested)
private struct FixPlaceholdersJob : IJobChunk
Burst-compiled job that iterates chunks with PlaceholderObjectElement buffers and removes entries whose referenced entities have the Deleted component. -
(Nested)
private struct ValidateSubAreasJob : IJobChunk
Burst-compiled job that validates and compacts SubArea and SubAreaNode buffers: - Ensures node sampling respects a minimum node distance (from AreaGeometryData / prefab).
- Removes duplicate/too-close nodes and drops subareas that end up with fewer than 3 nodes.
-
Logs when nodes are removed for a prefab sub-area.
-
(Nested)
private struct TypeHandle
Holds read/write component and buffer type handles and component lookups used by the system and jobs. Its __AssignHandles method populates all required handles from the SystemState.
Properties
- None (no public properties are defined by this system).
Constructors
public AreaInitializeSystem()
Default constructor. The system obtains required systems and sets up queries in OnCreate.
Methods
protected override void OnCreate()
Sets up dependency systems and entity queries:- Gets PrefabSystem from the World.
- Constructs m_PrefabQuery to find area prefabs (PrefabData + AreaData) which are either Created or Deleted.
- Constructs m_SubAreaQuery to find updated SubArea/SubAreaNode buffers.
- Constructs m_PlaceholderQuery to find entities that own PlaceholderObjectElement buffers (excluding Deleted).
-
Calls RequireAnyForUpdate with m_PrefabQuery and m_SubAreaQuery so the system only updates when relevant data exists.
-
protected override void OnUpdate()
Main update: if m_PrefabQuery is non-empty schedules/executes InitializeAreaPrefabs; if m_SubAreaQuery non-empty schedules ValidateSubAreas. -
private void InitializeAreaPrefabs()
Processes area prefab chunks (synchronously on main thread in this method) and: - Determines the AreaType and GeometryFlags based on presence of LotData, DistrictData, MapTileData, SpaceData, SurfaceData, TerrainAreaData and map-feature-dependent flags.
- Sets AreaGeometryData fields: m_Type, m_Flags, m_SnapDistance (using AreaUtils.GetMinNodeDistance), and possibly m_LodBias (from RenderedArea).
- Copies prefab color fields into AreaColorData (fill/edge/selection colors).
- Populates SubObject buffers for each prefab from AreaSubObjects definitions (including edge placement).
- For SpawnableArea prefabs, registers the prefab entity as a placeholder (adds PlaceholderObjectElement entries to the placeholder prefab entities) and copies probability values from prefab component to SpawnableObjectData.
- Adds SubArea entries for MasterArea slave areas into the SubArea buffers with m_NodeRange = -1.
- If any spawned placeholders were created, schedules FixPlaceholdersJob (via JobChunkExtensions.ScheduleParallel) to remove occurrences referencing Deleted entities. The job uses the m_PlaceholderQuery and sets base.Dependency.
The method uses a temporary NativeArray
private void ValidateSubAreas()
Schedules ValidateSubAreasJob (Burst) using m_SubAreaQuery. The job:- Reads AreaGeometryData via ComponentLookup to compute min node distance per area prefab.
- Reads/writes SubArea and SubAreaNode buffers to compact node lists and remove too-close nodes.
- Removes SubArea entries that end up with less than 3 nodes, logging when nodes are removed.
-
Updates base.Dependency with the returned JobHandle.
-
private void __AssignQueries(ref SystemState state)
Called in OnCreateForCompiler (compiler/IL2CPP scaffolding). The method currently constructs and disposes an EntityQueryBuilder to satisfy code-gen paths; the real queries are set up in OnCreate. -
protected override void OnCreateForCompiler()
Compiler helper: calls __AssignQueries and initializes __TypeHandle handles via __AssignHandles on the stored SystemState. Present to satisfy generated-code expectations. -
(Nested)
TypeHandle.__AssignHandles(ref SystemState state)
Aggressively-inlined method that obtains all necessary Entity/Component/Buffer type handles and ComponentLookups from the passed SystemState. These handles are used when accessing chunk data in job scheduling paths. -
(Nested job methods) FixPlaceholdersJob.Execute / ValidateSubAreasJob.Execute
Implement the per-chunk logic described above. Both jobs provide an explicit IJobChunk.Execute wrapper calling their Execute implementation.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by the system:
// - Acquire the PrefabSystem from the world
// - Create queries for area prefabs and sub-area updates
// The real logic is in the system; you normally don't need to call this manually.
m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
}
Notes for modders: - The system runs on prefab entities (AreaPrefabs). If you add custom AreaPrefabs, ensure their components (AreaSubObjects, MasterArea, SpawnableArea, RenderedArea, etc.) are populated so the system will initialize runtime buffers and geometry flags correctly. - The two Burst IJobChunk jobs (FixPlaceholdersJob and ValidateSubAreasJob) operate on chunk buffers; changes to SubArea and PlaceholderObjectElement buffers should go through EntityManager or via authored prefab components so the system can pick up and validate them. - The system relies on AreaUtils.GetMinNodeDistance and prefab components to determine how to subsample/validate nodes — review those utilities if you need custom sampling behavior.