Game.Serialization.ElectricityGraphSystem
Assembly:
Game (assembly inferred from namespace and typical project structure)
Namespace:
Game.Serialization
Type:
class (sealed by compiler-generated attribute pattern; contains nested Burst-compiled IJobChunk structs)
Base:
GameSystemBase
Summary:
ElectricityGraphSystem is a systems-level class used by the simulation to update and ensure electricity flow edges are present and correctly configured for electricity network edges and building electrical connections. It schedules two Burst-compiled IJobChunk jobs each frame: EdgeJob (processes network edges and middle nodes) and BuildingJob (processes building-related marker nodes and connections). The system reads prefab and connection metadata (ElectricityConnectionData) and uses ElectricityGraphUtils helper methods to create or update ElectricityFlowEdge components (including direction, capacity and flow sign). It emits warnings when expected flow edges are not found or cannot be updated. The system relies on EntityQueries and Component/Buffer lookups to find and modify relevant entities and flow edges.
Fields
-
private EntityQuery m_NetEdgeQuery
Query selecting entities that represent electricity connections (net edges). Created in OnCreate to locate Game.Net.ElectricityConnection/ElectricityNodeConnection/Edge/PrefabRef entities to be processed by the EdgeJob. -
private EntityQuery m_BuildingQuery
Query selecting building entities that have ElectricityBuildingConnection and PrefabRef. Created in OnCreate and used by BuildingJob to update building-related flow markers (valves, transformers, producer/consumer/charge/discharge nodes). -
private TypeHandle __TypeHandle
Internal struct (defined inside the class) that stores cached EntityTypeHandle / ComponentTypeHandle / BufferTypeHandle / ComponentLookup / BufferLookup instances. __TypeHandle.__AssignHandles is invoked to populate these handles for use in job setup and scheduling.
Properties
- None (the system does not expose public properties).
Nested Types
private struct EdgeJob : IJobChunk
Burst-compiled job that iterates net-edge chunks. For each net edge it:- Resolves start/end entities and associated electricity nodes.
- Reads PrefabRef to lookup ElectricityConnectionData (defaults capacity/direction if missing).
- Ensures there are ElectricityFlowEdge entries connecting start -> mid and mid -> end (or reverses existing edges appropriately).
- Updates flow-edge entries (direction, capacity and flips m_Flow sign if edge direction is reversed).
- Processes any connected middle nodes (ConnectedNode buffer) and attempts to update flow edges for each connected node.
-
Logs warnings when expected flow edges can't be located or updated. Notes: uses BufferLookup
and ComponentLookup (the latter with NativeDisableParallelForRestriction for writes). -
private struct BuildingJob : IJobChunk
Burst-compiled job that iterates building chunks. Responsibilities: - Gather marker node metadata (net sub-nodes that have electricity valves and connection data) from SubNet buffers and installed upgrades.
- For each marker node, map net node -> electricity node and valve node via ElectricityNodeConnection and ElectricityValveConnection lookups.
- Create or update flow edges between valve/marker nodes and various building nodes (transformer, producer/consumer/charge/discharge) using ElectricityGraphUtils.TrySetFlowEdge.
-
Logs warnings when expected flow edges cannot be created/updated. Internal helper MarkerNodeData groups a marker's net node entity, capacity and direction.
-
private struct TypeHandle
Holds the set of Entity/Component/Buffer handles used by the system and provides __AssignHandles(ref SystemState) which fetches handles from the SystemState. This is an internal pattern used for safety and performance when scheduling jobs.
Constructors
public ElectricityGraphSystem()
Default constructor. Marked with [Preserve] on OnCreate and OnUpdate methods rather than the constructor, but the class includes a public parameterless constructor created by the compiler. No additional initialization beyond base constructor.
Methods
protected override void OnCreate()
Initializes the system queries:- m_NetEdgeQuery = GetEntityQuery(ReadOnly: Game.Net.ElectricityConnection, ElectricityNodeConnection, Edge, PrefabRef)
-
m_BuildingQuery = GetEntityQuery(ReadOnly: ElectricityBuildingConnection, Building, PrefabRef) Purpose: prepare EntityQueries used by EdgeJob and BuildingJob. Marked with [Preserve] to prevent stripping.
-
protected override void OnUpdate()
Creates and populates EdgeJob and BuildingJob instances with the appropriate EntityTypeHandle / ComponentTypeHandle / BufferLookup / ComponentLookup instances via the __TypeHandle entries and InternalCompilerInterface helper. Schedules both jobs in parallel using JobChunkExtensions.ScheduleParallel and chains them on base.Dependency. This method is Burst-compatible thanks to the jobs it schedules. Marked with [Preserve]. -
protected override void OnCreateForCompiler()
Internal method used by generated code: calls __AssignQueries(ref CheckedStateRef) and __TypeHandle.__AssignHandles(ref CheckedStateRef). Ensures handles and queries are initialized for compiler-generated scenarios. Typically not called directly by user code. -
private void __AssignQueries(ref SystemState state)
A small helper that currently performs a no-op construct of EntityQueryBuilder(Allocator.Temp).Dispose(); left as a stub for query assignment during compilation/authoring. -
Nested/Job Execute methods:
EdgeJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates a chunk of net-edge entities and updates/ensures ElectricityFlowEdge relations between nodes, using helper UpdateFlowEdge and UpdateEdgeMiddleNodeConnections. Uses PrefabRef -> ElectricityConnectionData lookup, defaulting connection data (capacity=400000 and direction=Both) if missing.BuildingJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
Iterates building chunks, collects marker nodes (including those on installed upgrades and sub-nets), and updates flow edges between building nodes and valve/electricity nodes. Uses helper UpdateMarkerNode and UpdateFlowEdge which call ElectricityGraphUtils.TrySetFlowEdge. Both jobs include explicit IJobChunk.Execute implementations that forward to the typed Execute method.
Important behaviour notes:
- If ElectricityConnectionData is not present for a prefab, EdgeJob falls back to a default capacity (400000) and FlowDirection.Both.
- When an edge exists in the reverse direction, UpdateFlowEdge will flip the stored edge's start/end and invert its flow sign and update direction/capacity accordingly.
- Warnings are logged (UnityEngine.Debug.LogWarning) whenever expected ElectricityFlowEdge entries are not found or cannot be created/updated.
- The system writes to ElectricityFlowEdge via ComponentLookup
Usage Example
// Ensure the system exists in the current World (typical modding usage):
var world = Unity.Entities.World.DefaultGameObjectInjectionWorld;
var sys = world.GetOrCreateSystem<Game.Serialization.ElectricityGraphSystem>();
// The system will run automatically if part of the injection world.
// If needed from code, you can force a manual update (be careful with dependencies and job scheduling):
world.Update();
Additional example (how the system initializes queries — illustrative):
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// m_NetEdgeQuery and m_BuildingQuery are created here to target electricity edges and building connections.
}
If you are modding: - Do not remove or change expected component types (PrefabRef, ElectricityConnectionData, ElectricityNodeConnection, ElectricityValveConnection, ElectricityFlowEdge, etc.) unless you understand the impact on electricity graph creation. - Use ElectricityGraphUtils.TrySetFlowEdge / TryGetFlowEdge if you need to create or query flow edges in your own code to maintain compatibility with the system's conventions and data formats.