Skip to content

Game.ElectricityOutsideConnectionGraphSystem

Assembly: Assembly-CSharp (game assembly)
Namespace: Game.Simulation

Type: class

Base: GameSystemBase

Summary:
ElectricityOutsideConnectionGraphSystem is an ECS system that creates graph connections between the game's outside electricity nodes and in-world electricity nodes when an ElectricityOutsideConnection entity is created. It looks for newly created ElectricityOutsideConnection entities (with an Owner and Created tag, excluding Temp) and, for each matching Owner, locates the associated ElectricityNodeConnection and:

  • Adds a TradeNode component to the connected electricity node entity.
  • Creates two flow edges in the electricity graph: one from the global source node to the node, and one from the node to the global sink node.

The system performs these structural changes via a parallel IJobChunk (CreateOutsideConnectionsJob) and records the commands through a ModificationBarrier3 command buffer. It uses ElectricityFlowSystem to obtain the edge archetype and the global source/sink nodes. Edge creation sets FlowDirection.None and uses a very large capacity (1073741823) to represent effectively unlimited capacity for outside connections.


Fields

  • private ElectricityFlowSystem m_ElectricityFlowSystem
    Reference to the ElectricityFlowSystem used to obtain graph metadata (edge archetype, source node, sink node). Initialized in OnCreate via World.GetOrCreateSystemManaged(). Used to supply the job with edgeArchetype, sourceNode and sinkNode.

  • private ModificationBarrier3 m_ModificationBarrier
    Barrier system used to produce EntityCommandBuffer changes safely from jobs. The job writes structural changes (adding components, creating edges) using this barrier's command buffer (AsParallelWriter). The barrier is acquired in OnCreate.

  • private EntityQuery m_CreatedConnectionQuery
    EntityQuery that matches newly created outside-connection entities: ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.Exclude(). The query is required for update (RequireForUpdate) so the system only runs when there are new outside connections.

  • private TypeHandle __TypeHandle
    Internal struct holding ComponentTypeHandle and ComponentLookup handles used by the job. Populated in OnCreateForCompiler via __TypeHandle.__AssignHandles.

  • (Inner struct) CreateOutsideConnectionsJob — fields used by the job:

  • ComponentTypeHandle<Owner> m_OwnerType (ReadOnly) — used to read Owner components from matched chunks.
  • ComponentLookup<ElectricityNodeConnection> m_ElectricityNodeConnections (ReadOnly) — used to map Owner entity -> ElectricityNodeConnection data.
  • EntityCommandBuffer.ParallelWriter m_CommandBuffer — parallel command buffer for structural changes.
  • EntityArchetype m_EdgeArchetype — archetype for created electricity edge entities.
  • Entity m_SourceNode — global electricity source node entity.
  • Entity m_SinkNode — global electricity sink node entity.

Properties

  • No public properties. The system exposes no public getters/setters.

Constructors

  • public ElectricityOutsideConnectionGraphSystem()
    Default constructor. The system is initialized by the ECS runtime; real initialization happens in OnCreate.

Methods

  • protected override void OnCreate()
    Initializes the system references and query:
  • Grabs the ElectricityFlowSystem and ModificationBarrier3 from the World.
  • Builds the m_CreatedConnectionQuery to find newly created ElectricityOutsideConnection entities (Owner + Created, excluding Temp).
  • Calls RequireForUpdate(m_CreatedConnectionQuery) so the system runs only when there are new matches. It also prepares handles via OnCreateForCompiler call path in codegen scenarios.

  • protected override void OnUpdate()
    Prepares and schedules CreateOutsideConnectionsJob in parallel:

  • Fills the job struct with ComponentTypeHandle and ComponentLookup obtained via InternalCompilerInterface helpers and the pre-acquired ElectricityFlowSystem fields.
  • Creates a parallel EntityCommandBuffer from m_ModificationBarrier.
  • Schedules the job over m_CreatedConnectionQuery using JobChunkExtensions.ScheduleParallel and assigns the returned dependency to base.Dependency.
  • Calls m_ModificationBarrier.AddJobHandleForProducer to register the producer dependency so command buffer playback waits on the job.

  • protected override void OnCreateForCompiler()
    Compiler-time helper invoked by generated code paths: calls __AssignQueries and sets up __TypeHandle handles for the SystemState used by this system. This is part of the codegen/IL2CPP interplay and should be left as-is.

  • private void __AssignQueries(ref SystemState state)
    Internal helper that creates any required EntityQueryBuilder entries (here it ensures the query construction is present for static analysis/codegen). Implementation uses an empty EntityQueryBuilder(Allocator.Temp).Dispose() pattern.

  • TypeHandle.__AssignHandles(ref SystemState state)
    Assigns the ComponentTypeHandle and ComponentLookup handles from the provided SystemState. This is an aggressive-inlining helper used to prepare read-only handles for the job.

  • (Inner struct) CreateOutsideConnectionsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    For each Owner component in the chunk:

  • Uses m_ElectricityNodeConnections.TryGetComponent(ownerEntity, out componentData) to locate the ElectricityNodeConnection mapping.
  • If found, issues commands via m_CommandBuffer:

    • AddComponent(componentData.m_ElectricityNode)
    • Create two outside flow edges (source -> node, node -> sink) using ElectricityGraphUtils.CreateFlowEdge with FlowDirection.None and capacity 1073741823. The job runs in parallel across chunks and uses the provided unfilteredChunkIndex when calling the parallel command buffer.
  • (Inner struct) CreateOutsideConnectionsJob.CreateOutsideFlowEdge(int jobIndex, Entity startNode, Entity endNode)
    Helper used by the job to call ElectricityGraphUtils.CreateFlowEdge with the job's m_CommandBuffer, m_EdgeArchetype, start/end nodes, FlowDirection.None and capacity 1073741823.

Notes about behavior and performance: - The system schedules a parallel job (IJobChunk) and writes structural changes via a parallel command buffer. Because structural changes are deferred to the barrier, runtime safety and deterministic ordering are preserved. - The job uses ComponentLookup.TryGetComponent to map from Owner entity to an ElectricityNodeConnection; this is a per-item lookup and can fail if the mapping isn't present — then no edge is created for that owner. - Capacity constant 1073741823 is used for outside edges to effectively represent a non-limiting transport capacity. - The system only executes when there are newly created ElectricityOutsideConnection entities matching the query.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // This system acquires the ElectricityFlowSystem and a ModificationBarrier3
    // and will automatically create outside electricity edges when an
    // ElectricityOutsideConnection entity with Created tag appears.
    m_ElectricityFlowSystem = World.GetOrCreateSystemManaged<ElectricityFlowSystem>();
    m_ModificationBarrier = World.GetOrCreateSystemManaged<ModificationBarrier3>();
    m_CreatedConnectionQuery = GetEntityQuery(
        ComponentType.ReadOnly<ElectricityOutsideConnection>(),
        ComponentType.ReadOnly<Owner>(),
        ComponentType.ReadOnly<Created>(),
        ComponentType.Exclude<Temp>());
    RequireForUpdate(m_CreatedConnectionQuery);
}

If you want, I can also generate a short diagram of the created graph edges (source -> node -> sink) or extract the exact component types used (Owner, ElectricityNodeConnection, TradeNode, ElectricityOutsideConnection, Created, Temp) into a reference table.