Game.Simulation.ElectricityEdgeGraphSystem
Assembly: Game
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
System that reacts to newly created net edges that have an electricity connection and builds the corresponding electricity "flow" graph. For each created net edge it:
- ensures an electricity flow node exists for each net node (creating a flow node entity and adding ElectricityNodeConnection as needed),
- creates a middle flow node for the edge and links the edge to that middle node,
- creates ElectricityFlowEdge entries (flow edges) between flow nodes according to the prefab's ElectricityConnectionData (direction & capacity),
- ensures existing adjacent node/edge flow connections are created, and
- uses a ModificationBarrier (ModificationBarrier2B) + an EntityCommandBuffer inside a Burst IJobChunk (CreateEdgeConnectionsJob) so structural changes are deferred and scheduled safely with the ECS job system.
The job uses PrefabRef -> ElectricityConnectionData lookup, ConnectedNode/ConnectedEdge buffers on net entities, and existing ElectricityNodeConnection/ElectricityFlowEdge components to avoid duplicate flow edges. The system depends on ElectricityFlowSystem for node/edge archetypes.
Fields
-
private ElectricityFlowSystem m_ElectricityFlowSystem
Reference to the ElectricityFlowSystem used to obtain preconfigured node/edge archetypes and any shared flow graph utilities. -
private ModificationBarrier2B m_ModificationBarrier
Barrier system used to create an EntityCommandBuffer for deferred structural changes (creating flow nodes/edges, adding components). -
private EntityQuery m_CreatedEdgeQuery
Query used to select newly created net edges that need electricity flow graph setup. Built to match Game.Net.ElectricityConnection + Edge + PrefabRef + Created and exclude Temp. -
private TypeHandle __TypeHandle
Generated container that holds Entity/Component type handles and buffer/component lookups used by the job. It is assigned in OnCreateForCompiler to bind handles to the SystemState.
Properties
None
This system exposes no public properties.
Constructors
public ElectricityEdgeGraphSystem()
Default constructor. The system is created by the ECS world; initialization logic is performed in OnCreate.
Methods
-
protected override void OnCreate()
Initializes the system: grabs or creates the ElectricityFlowSystem and ModificationBarrier2B, builds the EntityQuery used to find newly created electricity-capable edges, and calls RequireForUpdate to ensure the system runs only when matching entities exist. -
protected override void OnUpdate()
Creates a temporary NativeParallelHashMap to map net nodes -> flow nodes for this update, constructs the CreateEdgeConnectionsJob (populating type handles, lookups, the command buffer, archetypes and the node map), schedules it over the m_CreatedEdgeQuery, disposes the temporary map with the produced job dependency, and registers the dependency with the ModificationBarrier2B.
Notes about the scheduled job (CreateEdgeConnectionsJob): - Burst compiled IJobChunk that iterates created net-edge chunks and, per edge, reads Edge and PrefabRef and uses buffer/component lookups to: - get or create flow-node connections for the start/end net nodes (GetOrCreateNetNodeConnection), - create a middle flow node for the edge and attach an ElectricityNodeConnection to the net edge, - create flow edges (ElectricityFlowEdge) using ElectricityGraphUtils.CreateFlowEdge with direction & capacity from ElectricityConnectionData, - connect middle node to connected net nodes found in ConnectedNode buffers (CreateEdgeMiddleNodeConnections), - ensure flow connections exist between a net node's flow node and any connected edges' flow nodes (EnsureNodeEdgeConnections). - Uses an EntityCommandBuffer for all entity creation and AddComponent calls to avoid structural changes inside the job.
-
private void __AssignQueries(ref SystemState state)
Compiler helper called from OnCreateForCompiler. In this implementation it creates/initializes any queries or temporary builder state; primary type handle assignments occur in __TypeHandle.__AssignHandles. -
protected override void OnCreateForCompiler()
Compiler helper that executes __AssignQueries and instructs the internal TypeHandle to assign its handles against the SystemState. Used by generated/converted code paths.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Acquire systems and set up the query that will drive this system
m_ElectricityFlowSystem = World.GetOrCreateSystemManaged<ElectricityFlowSystem>();
m_ModificationBarrier = World.GetOrCreateSystemManaged<ModificationBarrier2B>();
m_CreatedEdgeQuery = GetEntityQuery(
ComponentType.ReadOnly<Game.Net.ElectricityConnection>(),
ComponentType.ReadOnly<Edge>(),
ComponentType.ReadOnly<PrefabRef>(),
ComponentType.ReadOnly<Created>(),
ComponentType.Exclude<Temp>()
);
RequireForUpdate(m_CreatedEdgeQuery);
}
protected override void OnUpdate()
{
// The system schedules a Burst IJobChunk (CreateEdgeConnectionsJob)
// that creates required electricity flow nodes/edges for newly created net edges.
// No manual structural changes here: the job uses an ECB from m_ModificationBarrier.
}
Additional notes for modders: - If you add custom electricity-related prefabs, ensure an ElectricityConnectionData component exists on the prefab with proper m_Direction and m_Capacity values so this system can create appropriate flow edges. - The system relies on ConnectedNode and ConnectedEdge buffers on the net graph entities; mods that alter net connectivity must keep those buffers accurate. - Because structural changes are deferred via the ModificationBarrier2B command buffer, any systems that expect the flow graph to be present immediately after edge creation should either run after the barrier or observe the same barrier's produced dependency.