Game.Prefabs.BuildingInitializeSystem
Assembly:
Game (simulation/prefab systems)
Namespace:
Game.Prefabs
Type:
class
Base:
GameSystemBase
Summary:
Initializes building- and placeable-object prefabs when they are created. This ECS system configures geometry & lot sizes, terraform data, placement flags, service upkeep/upgrade relations and zone constraints, and schedules a parallel job that determines network/power/water/sewage connection requirements and generates procedural effects (collapse/fire SFX/VFX) for building prefabs. It runs during prefab creation and is responsible for a large portion of prefab preprocessing needed by the simulation (lot bounds, access flags, LOD, upkeep, service upgrade wiring, and effect buffers).
Fields
-
private static ILog log
Used for logging warnings and diagnostics during prefab initialization (e.g., geometry vs lot size warnings, monetary upkeep warnings). -
private EntityQuery m_PrefabQuery
EntityQuery that matches created Prefab entities that need initialization (buildings, extensions, service upgrades, spawnable buildings, etc.). Required for the system to run. -
private EntityQuery m_ConfigurationQuery
EntityQuery used to retrieve BuildingConfigurationData (singleton) used by connection/effect logic. -
private PrefabSystem m_PrefabSystem
Reference to the game's PrefabSystem used to fetch typed prefabs (BuildingPrefab, BuildingExtensionPrefab, etc.) during initialization. -
private TypeHandle __TypeHandle
Internal structure holding ComponentTypeHandle/BufferTypeHandle/ComponentLookup instances used to access chunk data efficiently. Assigned in OnCreateForCompiler. -
private EntityQuery __query_547773813_0
Internal query used to access EconomyParameterData (used when computing upkeep/maintenance costs). -
(Nested)
FindConnectionRequirementsJob
(Burst-compiled struct implementing IJobParallelFor)
A parallel job that inspects chunks of prefab entities to: - compute required network/water/power/sewage layers,
- set BuildingFlags (RequireRoad, HasWaterNode, HasSewageNode, HasLowVoltageNode, colorize lot, access flags, restricted traffic flags),
- examine sub-meshes and effects to add procedural collapse and fire SFX/VFX effects based on building geometry and configuration,
-
combine effects to generate looping/spot audio and VFX where needed. This job uses many ComponentTypeHandles, BufferLookups and a RandomSeed and operates over the archetype chunk array created in OnUpdate.
-
(Nested)
TypeHandle
(struct)
Holds the ComponentTypeHandle/BufferTypeHandle and ComponentLookup instances that GetComponent/GetBuffer logic uses; it exposes __AssignHandles to bind the handles to a SystemState.
Properties
- None (no public properties on the system itself).
Note: the nested TypeHandle and the job have many component handles/lookups used internally.
Constructors
public BuildingInitializeSystem()
Default (preserved) constructor. The system is constructed by the world; initialization of handles/queries happens in OnCreate/OnCreateForCompiler.
Methods
protected override void OnCreate()
Initial setup executed when the system is created:- Initializes the logger,
- obtains PrefabSystem from World,
- creates m_PrefabQuery (matches Created + PrefabData and any of building/extension/serviceupgrade/spawnable building components) and m_ConfigurationQuery (BuildingConfigurationData),
-
calls RequireForUpdate(m_PrefabQuery) so the system runs only when prefabs are present.
-
protected override void OnUpdate()
Main method executed when the system updates: - Creates an EntityCommandBuffer for deferred shared-component/other changes,
- Converts m_PrefabQuery to an array of archetype chunks and completes dependencies,
- Iterates chunks and for each chunk:
- if chunk is a Deleted (service upgrade deletion): removes upgrade references from associated buildings,
- otherwise reads/writes various component arrays (PrefabData, ObjectGeometryData, BuildingData, BuildingExtensionData, ConsumptionData, PlaceableObjectData, ServiceUpgradeData, BuildingPropertyData, ServiceUpkeep buffers, etc.),
- calls InitializeLotSize for buildings and performs object geometry/lot size adjustments for building extensions and placeables,
- adjusts placement flags (RoadSide/OwnerSide/NetObject/shoreline) and building flags (CanBeOnRoad),
- registers upgrades into BuildingUpgradeElement buffers,
- computes upkeep and adds ServiceUpkeep entries if needed, possibly logging warnings,
- Schedules the Burst-compiled FindConnectionRequirementsJob over the same chunk array to set connection flags and procedural effects, then waits for completion,
-
Plays back the EntityCommandBuffer and disposes resources.
-
private void InitializeLotSize(BuildingPrefab buildingPrefab, BuildingTerraformOverride terraformOverride, ref ObjectGeometryData objectGeometryData, ref BuildingTerraformData buildingTerraformData, ref BuildingData buildingData)
Compute and set buildingData.m_LotSize based on prefab lot width/depth or standing-geometry leg sizes; compute lot bounds and call InitializeTerraformData; update objectGeometryData (size, bounds, LOD layers); set BuildingFlags for access type (LeftAccess/RightAccess/BackAccess) based on prefab.m_AccessType; warn via log if geometry doesn't fit lot when asset warnings are enabled. -
public static void InitializeTerraformData(BuildingTerraformOverride terraformOverride, ref BuildingTerraformData buildingTerraformData, Bounds2 lotBounds, Bounds2 flatBounds)
Static helper used to derive BuildingTerraformData (flat region & smoothing) from given lot and flat bounds and optional override values. Applies level offsets, smoothing offsets and clamps computed flat extents into buildingTerraformData fields. -
private void __AssignQueries(ref SystemState state)
Internal method invoked by OnCreateForCompiler to build an internal query for EconomyParameterData (includes systems). -
protected override void OnCreateForCompiler()
Compiler-time helper that assigns internal queries and component handles by invoking __AssignQueries and TypeHandle.__AssignHandles. -
(Nested TypeHandle)
public void __AssignHandles(ref SystemState state)
Binds all ComponentTypeHandle/BufferTypeHandle/ComponentLookup fields in TypeHandle to an actual SystemState; called during system creation/compilation phase. -
(Nested FindConnectionRequirementsJob)
public void Execute(int index)
Job entry point that processes a single archetype chunk: sets building flags for roads/parking/track/power/water/sewage requirements based on ConsumptionData, WorkplaceData, special facility types (water pumping/station, transformer, parking, transport stations, parks), inspects SubNets to discover required net layers, marks nodes (HasLowVoltageNode/HasWaterNode/HasSewageNode), inspects SubMeshes + Effect buffers to add procedural collapse/fall VFX/SFX and fire effects (including combining close fire effects and adding loop/spot audio), and uses the BuildingConfigurationData and RandomSeed for procedural placement. -
(Nested FindConnectionRequirementsJob) helper methods:
private void AddFireSfxToBuilding(ref bool2 hasFireSfxEffects, DynamicBuffer<Effect> effects, float3 position, quaternion rotation, int parent)
Adds fire loop/spot SFX effects to the effects buffer if not already present.private void CheckPropFlags(ref BuildingFlags flags, DynamicBuffer<SubObject> subObjects, int maxDepth = 10)
Recursively inspects sub-objects (props) for spawn location data to determine properties such as HasInsideRoom and handles nested sub-object recursion (guarded by maxDepth).
Nested types
- FindConnectionRequirementsJob (BurstCompile, IJobParallelFor) — job used by OnUpdate to set flags and create procedural effects.
- TypeHandle (struct) — contains ComponentTypeHandles/BufferTypeHandles/ComponentLookups and __AssignHandles method.
Usage Example
// Typical usage is automatic: the system runs in the game's world when prefabs are created.
// You can, however, call the static terraform initializer from custom code if needed:
// Prepare data (terraformOverride can be null)
BuildingTerraformData terraformData = default;
Bounds2 lotBounds = new Bounds2(new float2(-4f, -4f), new float2(4f, 4f));
Bounds2 flatBounds = prefabObjectGeometry.m_Bounds.xz;
// Initialize terraform data (applies overrides and clamps flat extents)
BuildingInitializeSystem.InitializeTerraformData(terraformOverride, ref terraformData, lotBounds, flatBounds);
Notes: - This system is part of the simulation/prefab initialization pipeline. Most mods should not need to call the system directly; instead rely on the game's prefab creation flow. Modders can call InitializeTerraformData for custom prefab preprocessing if constructing BuildingTerraformData manually. - Many methods and data structures interact with internal ECS component types (buffers, lookups and chunk access). Use care when reading/writing those components from other systems — maintain dependency completion and proper access semantics.