Skip to content

Game.Simulation.ElectricityStatusSystem

Assembly:
Assembly-CSharp (Unity game assembly)

Namespace:
Game.Simulation

Type:
class

Base:
GameSystemBase

Summary:
ElectricityStatusSystem monitors electricity flow edges and building connections to detect problems (bottlenecks, insufficient production, transformer issues) and issues or removes UI icon notifications accordingly. It schedules two Burst-compiled IJobChunk jobs (NetEdgeNotificationJob and BuildingNotificationJob) to scan edge and building entities in parallel, uses ElectricityFlowSystem for source/sink nodes, and uses ElectricityParameterData for notification prefabs. The system runs on a periodic schedule (update interval 128, offset 127) and writes icon commands through IconCommandSystem/IconCommandBuffer.


Fields

  • private ElectricityFlowSystem m_ElectricityFlowSystem
    Holds a reference to the ElectricityFlowSystem (retrieved from the World) so the system can read the global sourceNode and sinkNode used when evaluating flow edges.

  • private IconCommandSystem m_IconCommandSystem
    Used to create IconCommandBuffer instances and register command buffer writers so icon add/remove commands from the jobs are applied to the UI.

  • private EntityQuery m_EdgeQuery
    EntityQuery selecting electric edge entities (Edge + ElectricityNodeConnection, excluding Temp). This query is used to schedule NetEdgeNotificationJob.

  • private EntityQuery m_BuildingQuery
    EntityQuery selecting building entities that have electricity building connections (Building + ElectricityBuildingConnection + Transform + SubNet), excluding Temp and Deleted. Used to schedule BuildingNotificationJob.

  • private EntityQuery m_ElectricityParameterQuery
    EntityQuery for the ElectricityParameterData singleton (contains references to notification prefabs and other parameters). Required for update.

  • private TypeHandle __TypeHandle
    Internal struct caching EntityTypeHandle, ComponentTypeHandle and BufferLookup/ComponentLookup instances used by the jobs. Assigned at OnCreateForCompiler to avoid re-querying handles every frame.

Properties

  • (none exposed by this class)
    This system does not define public properties.

Constructors

  • public ElectricityStatusSystem()
    Default constructor (marked [Preserve] on the real lifecycle methods). The system relies on OnCreate to initialize its queries and references.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 128. Used by the scheduling system to run the system every 128 ticks/frames (coarse periodic update).

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns 127. Combined with the interval to stagger execution across frames.

  • [Preserve] protected override void OnCreate()
    Initializes the system:

  • Retrieves ElectricityFlowSystem and IconCommandSystem from the World.
  • Creates the edge, building, and parameter EntityQueries.
  • Calls RequireAnyForUpdate on the edge and building queries and RequireForUpdate on the electricity parameter query so the system only updates when relevant entities exist.

  • [Preserve] protected override void OnUpdate()
    Main update that:

  • Creates an IconCommandBuffer from IconCommandSystem.
  • Reads the ElectricityParameterData singleton (prefabs etc.).
  • Schedules two Burst IJobChunk jobs in parallel:
    • NetEdgeNotificationJob: iterates edge entities, inspects connected flow edges for bottlenecks (ignoring the global source/sink edge), and adds/removes a bottleneck notification icon on the owner entity.
    • BuildingNotificationJob: iterates building entities, checks transformer node edges and producer/consumer edges for bottlenecks or "beyond bottleneck" conditions, and adds/removes transformer/production/bottleneck notifications.
  • Sets base.Dependency to the combined dependency and registers the IconCommandBuffer writer via IconCommandSystem.AddCommandBufferWriter so icon commands are applied after jobs complete.

  • private void __AssignQueries(ref SystemState state)
    Compiler helper stub used when compiling with source-generated code paths; here it contains a minimal EntityQueryBuilder usage and is called from OnCreateForCompiler.

  • protected override void OnCreateForCompiler()
    Compiler-time initialization that calls __AssignQueries and __TypeHandle.__AssignHandles to populate cached type handles; used by code-generation/IL2CPP paths.

  • private struct NetEdgeNotificationJob : IJobChunk
    Burst-compiled job that:

  • Reads edge owner Entity, ElectricityNodeConnection, buffers of ConnectedFlowEdge for the electricity node and ElectricityFlowEdge components.
  • For each edge owner, iterates connected edges on the node to find any flow edge marked isBottleneck (excluding the global source/sink edge). If found, schedules an IconCommandBuffer.Add for the bottleneck notification prefab; otherwise removes it.

  • private struct BuildingNotificationJob : IJobChunk
    Burst-compiled job that:

  • Reads building owner Entity, ElectricityBuildingConnection, connected flow edges and ElectricityFlowEdge components.
  • Checks transformer's connected edges for any isBottleneck.
  • Checks the building's producer edge for isBottleneck and consumer edge for isBeyondBottleneck.
  • Based on the checks, adds or removes transformer, not-enough-production, and bottleneck notifications via IconCommandBuffer.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Typical initialization done by this system:
    m_ElectricityFlowSystem = base.World.GetOrCreateSystemManaged<ElectricityFlowSystem>();
    m_IconCommandSystem = base.World.GetOrCreateSystemManaged<IconCommandSystem>();
    m_EdgeQuery = GetEntityQuery(ComponentType.ReadOnly<Edge>(), ComponentType.ReadOnly<ElectricityNodeConnection>(), ComponentType.Exclude<Temp>());
    m_BuildingQuery = GetEntityQuery(ComponentType.ReadOnly<Building>(), ComponentType.ReadOnly<ElectricityBuildingConnection>(),
                                     ComponentType.ReadOnly<Transform>(), ComponentType.ReadOnly<Game.Net.SubNet>(),
                                     ComponentType.Exclude<Temp>(), ComponentType.Exclude<Deleted>());
    m_ElectricityParameterQuery = GetEntityQuery(ComponentType.ReadOnly<ElectricityParameterData>());
    RequireAnyForUpdate(m_EdgeQuery, m_BuildingQuery);
    RequireForUpdate(m_ElectricityParameterQuery);
}

Notes and tips for modders: - Notification prefabs and thresholds come from ElectricityParameterData — modify that asset to change which icons are used. - The jobs run Burst and operate on chunked data; to change the detection logic you can adapt the NetEdgeNotificationJob and BuildingNotificationJob implementations. - Icon commands are buffered; IconCommandSystem.AddCommandBufferWriter(base.Dependency) ensures commands are flushed after jobs complete. If you add your own jobs that also emit icon commands, register the command buffer writer dependency similarly.