Game.Simulation.ElectricityBuildingGraphSystem
Assembly: Assembly-CSharp
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
ElectricityBuildingGraphSystem is responsible for creating, updating and removing electricity-related graph nodes and flow edges for buildings. It reacts to created/updated building entities (producers, consumers, transformers, batteries and installed-upgrades that expose electricity subnets) and ensures the electricity flow graph reflects the current building configuration. Internally it schedules a parallel IJobChunk (UpdateBuildingConnectionsJob) that scans matching building chunks and:
- finds marker nodes provided by building subnets and upgrades,
- creates or deletes building-specific flow nodes (producer/consumer/transformer/charge/discharge),
- creates or updates flow edges between building nodes and marker nodes (with correct direction and capacity),
- queues road-edge updates for electricity connections on roads,
- uses an EntityCommandBuffer for structural changes to avoid race conditions.
This system integrates with: - ElectricityFlowSystem (provides archetypes and global source/sink nodes), - ElectricityRoadConnectionGraphSystem (for edge updates), - ModificationBarrier4B (for buffered entity commands).
Fields
-
private ElectricityRoadConnectionGraphSystem m_ElectricityRoadConnectionGraphSystem
Handles updates for electricity connections related to road edges; used to enqueue road edge updates when building markers affect road nodes. -
private ElectricityFlowSystem m_ElectricityFlowSystem
Reference to the ElectricityFlowSystem which provides archetypes (node/edge/charge/discharge) and global source/sink nodes needed to create flow graph entities. -
private ModificationBarrier4B m_ModificationBarrier
Barrier system used to create an EntityCommandBuffer. Changes to entity structure/components are recorded through this buffer. -
private EntityQuery m_UpdatedBuildingQuery
Query used to select buildings that were created or updated and may affect electricity connections (producers, consumers, transformers, batteries, installed upgrades, or existing ElectricityBuildingConnection components). -
private TypeHandle __TypeHandle
Generated struct holding cached Entity/Component/Buffer type handles for fast access in jobs. It's assigned during OnCreateForCompiler. -
private struct UpdateBuildingConnectionsJob
Nested job type (IJobChunk, Burst-compiled) that performs the per-chunk work of updating building nodes and marker nodes. See Methods section for more details.
Properties
- None (this system does not expose public properties)
Constructors
public ElectricityBuildingGraphSystem()
Default constructor (preserved). Initialization of references occurs during OnCreate rather than in the ctor.
Methods
protected override void OnCreate() : System.Void
Initializes references to dependent systems:- Obtains ElectricityRoadConnectionGraphSystem, ElectricityFlowSystem and ModificationBarrier4B from the World.
- Builds the EntityQuery (m_UpdatedBuildingQuery) that matches buildings which have relevant electricity-related components and were created/updated.
-
Calls RequireForUpdate on the query so the system only runs when there are relevant changes.
-
protected override void OnUpdate() : System.Void
Creates and schedules UpdateBuildingConnectionsJob as a parallel JobChunk: - Fills the job struct with type handles, lookups, command buffer, archetypes and global nodes from ElectricityFlowSystem.
- Gets the road-edge update queue from ElectricityRoadConnectionGraphSystem and includes its dependency.
-
Schedules the job over m_UpdatedBuildingQuery and registers the resulting JobHandle with the modification barrier and the road graph system.
-
protected override void OnCreateForCompiler() : System.Void
Compiler helper used by generated code to assign query and type handles. Calls __AssignQueries and __TypeHandle.__AssignHandles. -
private void __AssignQueries(ref SystemState state)
Internal method used in generated code for query assignment. In this implementation it creates and disposes a temporary EntityQueryBuilder (placeholder for compiler-generated query setup). -
private struct UpdateBuildingConnectionsJob : IJobChunk
Core work is performed here. Important parts: - Uses many Buffer/Component lookups (subnets, installed upgrades, prefab refs, electricity connection data, node/valve/flow components, connected edges, owner/deleted/orphan flags).
- For each chunk/building entity it:
- Collects marker nodes from building subnets and upgrades (FindMarkerNodes).
- Creates/updates building-level nodes (transformer/producer/consumer/charge/discharge) according to building components and whether the building is destroyed (CreateOrUpdateBuildingNodes / DeleteBuildingNodes).
- Creates or updates marker nodes and valve connections, and connects them with flow edges to building nodes (CreateOrUpdateMarkerNode, CreateOrUpdateFlowEdge).
- Ensures marker nodes are connected to adjacent net edges (EnsureMarkerNodeEdgeConnections), creating flow edges to other electricity node connections if missing.
- Uses an EntityCommandBuffer.ParallelWriter for structural changes and a NativeQueue.ParallelWriter to enqueue updated road edges.
Notable nested methods:
- Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates entities in the chunk and applies the logic described above.
-
FindMarkerNodes(...)
Extracts marker nodes from buffers of Game.Net.SubNet or InstalledUpgrade; filters by prefab electricity connection data and orphan/valve/valid node status. -
IsOrphan(Entity netNode) : bool
Determines whether a net node is orphaned (no owned edges and marked Orphan component). -
CreateOrUpdateBuildingNodes(...) : BuildingNodes
Ensures building-specific nodes/edges exist (or deletes them) for transformer, producer, consumer and battery roles. Creates flow edges connecting building nodes as needed (producer->consumer, producer->charge, discharge->consumer). -
DeleteBuildingNodes(...)
Removes all flow nodes and edges associated with a building's ElectricityBuildingConnection and removes the ElectricityBuildingConnection component itself. -
CreateOrUpdateMarkerNode(...)
Ensures an ElectricityNodeConnection and ElectricityValveConnection exist on the marker net node, creates/updates a flow edge between valve and node, links building nodes to the valve node, and calls EnsureMarkerNodeEdgeConnections. -
EnsureMarkerNodeEdgeConnections(...)
Iterates connected net edges and ensures flow edges exist between this marker node and other electricity nodes connected to adjacent edges. -
CreateOrUpdateFlowEdge(...)
If both nodes are already stored and an existing flow edge exists between them, update its direction/capacity as necessary; otherwise create a new flow edge. -
CreateFlowEdge(...) : Entity
Delegates to ElectricityGraphUtils.CreateFlowEdge which uses the command buffer to create a flow-edge entity with the given archetype, endpoints, direction and capacity. -
void IJobChunk.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Explicit interface implementation forwarding to the internal Execute method.
Usage Example
// Typical usage inside another system or mod code to get the system instance:
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var electricityBuildingGraphSystem = world.GetOrCreateSystemManaged<Game.Simulation.ElectricityBuildingGraphSystem>();
// The system runs automatically during the world update when buildings are created/updated.
// If you need to ensure a synchronous update inside editor/tools, you can update the world:
world.Update();
Notes: - This system is part of the simulation step and performs structural changes via a modification barrier; modders should not directly modify the electricity flow graph entities created by this system unless coordinating through the proper command buffers or running at an appropriate simulation point. - The heavy lifting is done in a Burst-compiled JobChunk, so most logic runs in parallel and uses Unity Collections/Entities lookups.