Skip to content

Game.Simulation.BuildingConstructionSystem

Assembly:
Game (assembly name inferred)

Namespace:
Game.Simulation

Type:
class BuildingConstructionSystem

Base:
GameSystemBase

Summary:
Processes buildings that are marked UnderConstruction. Runs as a periodic system (interval = 64 simulation frames) and schedules a Burst-compiled parallel IJobChunk (BuildingConstructionJob) that: - Advances construction progress per UnderConstruction component (randomized speeds). - Handles crane "points of interest" updates for sub-objects while the building is under construction. - When construction completes, swaps the building's prefab (PrefabRef), sets up/cleans up associated buffers (mesh colors, sub-areas, sub-nets) and schedules creation of area and net entities (CreationDefinition / NetCourse). - Uses an EndFrameBarrier EntityCommandBuffer.ParallelWriter to apply structural changes safely from the job. - Uses TerrainSystem's building upgrade writer to record previous prefab -> new prefab mapping for upgrades.


Fields

  • private const int UPDATE_INTERVAL_BITS
    Bits used to represent the update interval. Value in source: 6.

  • public const uint UPDATE_INTERVAL
    Update interval in frames. Value in source: 64.

  • private SimulationSystem m_SimulationSystem
    Reference to the SimulationSystem used to read the current simulation frame index.

  • private TerrainSystem m_TerrainSystem
    Reference to the TerrainSystem used to obtain the building upgrade writer (previous prefab map) and to set dependencies.

  • private ZoneSpawnSystem m_ZoneSpawnSystem
    Reference used to read debug spawn settings (debugFastSpawn).

  • private CityConfigurationSystem m_CityConfigurationSystem
    Reference used to read city configuration (e.g., left-hand traffic).

  • private EndFrameBarrier m_EndFrameBarrier
    Barrier system used to create a parallel command buffer for safely mutating entities from the job.

  • private EntityQuery m_BuildingQuery
    Query selecting entities to update (ReadOnly, ReadOnly, Exclude).

  • private TypeHandle __TypeHandle
    Holds ComponentTypeHandle/ComponentLookup/BufferLookup instances used by the BuildingConstructionJob. Populated in OnCreateForCompiler.


Properties

  • (none public on this system)

Constructors

  • public BuildingConstructionSystem()
    Default constructor (preserve attribute present on other lifecycle methods in the source).

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns the update interval for the system in frames. Implementation returns 64.

  • [Preserve] protected override void OnCreate()
    Initializes system references (SimulationSystem, TerrainSystem, ZoneSpawnSystem, CityConfigurationSystem, EndFrameBarrier) and builds the EntityQuery for buildings under construction. Calls RequireForUpdate(m_BuildingQuery) so the system only runs when there are matching entities.

  • [Preserve] protected override void OnUpdate()
    Creates and schedules the BuildingConstructionJob using JobChunkExtensions.ScheduleParallel with the system's TypeHandle lookups, current RandomSeed, EndFrameBarrier command buffer (parallel writer), and TerrainSystem building-upgrade writer. Adds the job handle to the EndFrameBarrier and sets TerrainSystem dependency. The job is scheduled to run for m_BuildingQuery with the system's current dependency.

  • protected override void OnCreateForCompiler()
    Helper used by compilation/time-of-generation to assign queries and component handles. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Internal helper used by OnCreateForCompiler to prepare any queries required during compilation-time flow.

Inner types and their important methods (job implementation)

  • private struct BuildingConstructionJob : IJobChunk
    Burst compiled job executed in parallel for chunks matching the building query. Responsibilities:
  • Reads entity, Transform and PrefabRef component data and writes UnderConstruction.
  • Advances building construction progress (randomized initial speed and progress deltas based on simulation frame).
  • Calls UpdateCranes periodically to update PointOfInterest positions for sub-objects with cranes.
  • When construction completes, sets a new PrefabRef, removes UnderConstruction, and calls UpdatePrefab to adjust buffers and create child entities (areas, nets).
  • Uses an EntityCommandBuffer.ParallelWriter to perform structural changes and a NativeParallelHashMap to track selected spawnables when choosing area spawnable prefabs.

Key methods inside BuildingConstructionJob: - public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
The IJobChunk Execute implementation that iterates entities in the chunk and advances/finishes construction.

  • private void UpdateCranes(ref Random random, Entity entity, Transform transform, PrefabRef prefabRef)
    Updates PointOfInterest components for sub-objects that have cranes. Chooses random positions within prefab bounds, clamps by crane distance ranges and writes the PointOfInterest (position + validity).

  • private void UpdatePrefab(int jobIndex, Entity entity, Entity newPrefab, Transform transform, ref Random random, ref NativeParallelHashMap<Entity, int> selectedSpawnables)
    Called when construction completes: updates PrefabRef, marks Updated, resets mesh batch indices, toggles MeshColor buffer presence depending on color variations, flags sub-objects as Updated, removes/creates SubArea buffers and calls CreateAreas, handles SubNet cleanup/creation and calls CreateNets.

  • private void CreateAreas(int jobIndex, Entity owner, Transform transform, DynamicBuffer<Game.Prefabs.SubArea> subAreas, DynamicBuffer<SubAreaNode> subAreaNodes, ref Random random, ref NativeParallelHashMap<Entity, int> selectedSpawnables)
    For each SubArea in the prefab: chooses runtime prefab for spawnable placeholders (via AreaUtils.SelectAreaPrefab), creates a CreationDefinition entity for the area, populates its Game.Areas.Node buffer with world-space node positions computed from prefab node data and the owner's transform.

  • private void CreateNets(int jobIndex, Entity owner, Transform transform, DynamicBuffer<Game.Prefabs.SubNet> subNets, ref Random random)
    Computes averaged node positions, then iterates sub-net descriptors and calls CreateSubNet for each final sub-net (using NetUtils.GetSubNet to adapt for left-hand traffic and upgrades).

  • private void CreateSubNet(int jobIndex, Entity netPrefab, Bezier4x3 curve, int2 nodeIndex, int2 parentMesh, CompositionFlags upgrades, NativeList<float4> nodePositions, Entity owner, Transform transform, ref Random random)
    Creates a CreationDefinition entity for the sub-net and populates a NetCourse component with world-space curve, start/end positions, rotations, flags and length. Adds an Upgraded component if upgrades were specified.

  • void IJobChunk.Execute(... )
    Explicit interface implementation that forwards to Execute.

  • private struct TypeHandle
    Container for all ComponentTypeHandle, ComponentLookup and BufferLookup fields used by BuildingConstructionJob. Populated by __AssignHandles:

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] public void __AssignHandles(ref SystemState state)
    Assigns handles and lookups from the given SystemState. This is required so the job can access component and buffer data correctly (EntityTypeHandle, Transform/PrefabRef type handles, lookups for Owner/Deleted/Crane/etc, and buffer lookups such as SubObject/SubArea/SubNet/MeshBatch/MeshColor/PrefabSubArea/Prefabs SubNet/SubMesh/ColorVariation).


Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
    m_TerrainSystem = base.World.GetOrCreateSystemManaged<TerrainSystem>();
    m_ZoneSpawnSystem = base.World.GetOrCreateSystemManaged<ZoneSpawnSystem>();
    m_CityConfigurationSystem = base.World.GetOrCreateSystemManaged<CityConfigurationSystem>();
    m_EndFrameBarrier = base.World.GetOrCreateSystemManaged<EndFrameBarrier>();
    m_BuildingQuery = GetEntityQuery(
        ComponentType.ReadOnly<UnderConstruction>(),
        ComponentType.ReadOnly<Building>(),
        ComponentType.Exclude<Destroyed>(),
        ComponentType.Exclude<Deleted>(),
        ComponentType.Exclude<Temp>());
    RequireForUpdate(m_BuildingQuery);
}

Notes and modding tips: - The system runs every 64 simulation frames (UPDATE_INTERVAL). To change frequency of building construction progression you would need to override GetUpdateInterval in a derived system or replace scheduling behaviour — but be careful: other systems rely on this cadence. - The job uses a parallel EndFrameBarrier command buffer; structural changes (adding/removing components, creating entities) are deferred until the barrier plays them back. - When construction finishes the system creates CreationDefinition entities for area and net children. Mods that hook into area/net creation should watch for those CreationDefinition entities or Updated flags to track newly created sub-entities. - The system uses TerrainSystem.GetBuildingUpgradeWriter to record previous prefab -> new prefab mapping; if you alter prefab swapping logic ensure compatibility with terrain/building upgrade handling to avoid losing upgrade history.