Game.Buildings.InitializeSystem
Assembly: Game
Namespace: Game.Buildings
Type: class
Base: GameSystemBase
Summary: Initializes newly created building and coverage entities. This system schedules two parallel IJobChunk jobs: - InitializeCoverageTypeJob: reads PrefabRef and Prefab CoverageData to set the CoverageServiceType shared component on coverage-elements. - InitializeBuildingsJob: inspects building prefabs and instance state to add or remove runtime components such as ElectricityConsumer, WaterConsumer, GarbageProducer, MailProducer and CrimeProducer, remove destroyed-building components, and enqueue road-edge updates for electricity/water graphs.
The system uses a ModificationBarrier2 EntityCommandBuffer for structural changes and coordinates with ElectricityRoadConnectionGraphSystem and WaterPipeRoadConnectionGraphSystem to update connection graphs via queues. It runs only when its entity queries match.
Fields
-
private ModificationBarrier2 m_ModificationBarrier
Used to create a parallel EntityCommandBuffer for safe structural changes from jobs. The barrier receives the job handle so structural changes are applied correctly. -
private ElectricityRoadConnectionGraphSystem m_ElectricityRoadConnectionGraphSystem
Reference to the electricity road connection graph system. Used to obtain a queue writer to enqueue road-edge updates when electricity-consumer components are added to buildings. -
private WaterPipeRoadConnectionGraphSystem m_WaterPipeRoadConnectionGraphSystem
Reference to the water pipe road connection graph system. Used to obtain a queue writer to enqueue road-edge updates when water-consumer components are added. -
private EntityQuery m_CoverageQuery
EntityQuery that selects coverage elements requiring initialization (reads CoverageServiceType, CoverageElement, PrefabRef and Created; excludes Placeholder). If not empty, InitializeCoverageTypeJob is scheduled. -
private EntityQuery m_BuildingQuery
EntityQuery that selects building entities to initialize (reads Building, PrefabRef, Updated and excludes ServiceUpgrade and Placeholder). If not empty, InitializeBuildingsJob is scheduled. -
private ComponentTypeSet m_DestroyedBuildingComponents
ComponentTypeSet containing components that should be removed from destroyed buildings (ElectricityConsumer, WaterConsumer, GarbageProducer, MailProducer). Passed to jobs when a building is destroyed. -
private TypeHandle __TypeHandle
Generated struct that caches EntityTypeHandle, ComponentTypeHandle and ComponentLookup handles used by the jobs. Populated in OnCreateForCompiler via __AssignHandles. -
private struct InitializeCoverageTypeJob
(nested)
IJobChunk that: - Reads Entity and PrefabRef arrays for chunk
- Looks up CoverageData from the prefab
-
Uses an EntityCommandBuffer.ParallelWriter to set a shared component CoverageServiceType on each instance based on prefab CoverageData
-
private struct InitializeBuildingsJob
(nested)
IJobChunk that: - Reads Entity, Building and PrefabRef arrays and component existence flags (PoliceStation, PostFacility, Destroyed, Created, Abandoned)
- Uses ComponentLookup to inspect prefab/component archetypes for ElectricityConsumer, WaterConsumer and GarbageProducer (via TypeIndex)
- Adds/removes runtime components (CrimeProducer, MailProducer, ElectricityConsumer, WaterConsumer, GarbageProducer)
- Enqueues road-edge entities for electricity/water graph updates via NativeQueue.ParallelWriter when consumers are added
-
Removes components listed in m_DestroyedBuildingComponents when building is Destroyed
-
private struct TypeHandle
(nested)
Generated container of ComponentTypeHandle and ComponentLookup handles with method __AssignHandles to bind them to a SystemState.
Properties
- None (no public properties on this system)
Constructors
public InitializeSystem()
[Preserve] default constructor. The system relies on OnCreate to initialize internal references and queries.
Methods
protected override void OnCreate()
Initializes the system:- Obtains ModificationBarrier2 and the two connection-graph systems from the world.
- Creates entity queries m_CoverageQuery and m_BuildingQuery used to decide when to schedule jobs.
-
Initializes the ComponentTypeSet m_DestroyedBuildingComponents containing components to remove when buildings are destroyed.
-
protected override void OnUpdate()
Main update method: - If m_CoverageQuery is not empty, schedules InitializeCoverageTypeJob (parallel) to set CoverageServiceType on coverage elements, attaches the job handle to the modification barrier.
-
If m_BuildingQuery is not empty, schedules InitializeBuildingsJob (parallel) that:
- Adds/removes building runtime components and enqueues road-edge updates.
- Combines dependencies with the queue-producing systems to ensure correct ordering.
- Attaches the job handle to the modification barrier and informs the electricity/water graph systems that the queue will be written.
-
protected override void OnCreateForCompiler()
Compiler-time helper that calls __AssignQueries and assigns handles from the generated TypeHandle to the SystemState. (Used in generated code paths to ensure handles are ready for jobs.) -
private void __AssignQueries(ref SystemState state)
Generated/placeholder method: currently creates and disposes an EntityQueryBuilder (may be a stub used by code generation). Marked MethodImplOptions.AggressiveInlining for performance inlined use by compiler-generated flow. -
Nested job methods:
void InitializeCoverageTypeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Processes a chunk to read PrefabRef and set CoverageServiceType via command buffer based on prefab CoverageData.void InitializeBuildingsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Processes a chunk to add/remove building-related components and enqueue road-edge updates when applicable.- Each nested job also has explicit interface implementations for IJobChunk.Execute that forward to the typed Execute method.
Usage Example
// Create a building entity with a PrefabRef and Created so InitializeSystem will process it
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var entityManager = world.EntityManager;
Entity prefabEntity = /* obtain prefab entity for this building type */;
Entity building = entityManager.CreateEntity(
typeof(Game.Buildings.Building),
typeof(Game.Prefabs.PrefabRef),
typeof(Game.Common.Created)
);
// Set the PrefabRef so InitializeSystem can read prefab metadata
entityManager.SetComponentData(building, new Game.Prefabs.PrefabRef { m_Prefab = prefabEntity });
// After the ECS update loop runs, InitializeSystem will:
// - Add ElectricityConsumer/WaterConsumer/GarbageProducer/MailProducer/CrimeProducer
// based on the prefab's ConsumptionData/BuildingData/ObjectData and current created/abandoned/destroyed flags.
// - Enqueue any necessary road-edge updates for electricity/water connection graphs.