Skip to content

Game.Simulation.ElectricityGraphUtils

Assembly: Assembly-CSharp (game assemblies may vary; adjust to the actual assembly used by the modding environment)
Namespace: Game.Simulation

Type: static class

Base: System.Object

Summary:
Utility helper methods for managing the electricity flow graph in the ECS-based simulation. Provides safe lookup, creation and deletion of electricity flow edges and nodes, convenience wrappers for both main-thread (EntityManager) and job-friendly (EntityCommandBuffer / ParallelWriter) APIs, and helpers to operate on building-related electricity nodes. The class assumes standard ECS components/buffers such as ElectricityFlowEdge and ConnectedFlowEdge exist on the relevant entities and uses Unity.Assertions for development-time checks.


Fields

  • None
    This static utility class does not declare any instance or static fields.

Properties

  • None

Constructors

  • None (static utility class)

Methods

  • public static bool HasAnyFlowEdge(Entity node1, Entity node2, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges)
    Checks whether there exists any ElectricityFlowEdge that connects node1 and node2 (either direction). Asserts that both entities have valid indices (> 0). Iterates the ConnectedFlowEdge buffer for node1 and compares edge endpoints.

  • public static bool TryGetFlowEdge(Entity startNode, Entity endNode, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges, out Entity entity)
    Overload that attempts to find an edge going from startNode to endNode. If found returns true and sets out entity. Delegates to the more general overload that also returns the edge component.

  • public static bool TryGetFlowEdge(Entity startNode, Entity endNode, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges, out ElectricityFlowEdge edge)
    Overload that attempts to find the edge and returns the ElectricityFlowEdge component in edge. Delegates to the more general overload that also returns the entity.

  • public static bool TryGetFlowEdge(Entity startNode, Entity endNode, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges, out Entity entity, out ElectricityFlowEdge edge)
    Core lookup implementation. Asserts that start/end entities are valid, iterates startNode's ConnectedFlowEdge buffer, reads each ElectricityFlowEdge and checks if m_Start == startNode and m_End == endNode. If found sets entity and edge and returns true. Otherwise sets out values to defaults and returns false.

  • public static bool TrySetFlowEdge(Entity startNode, Entity endNode, FlowDirection direction, int capacity, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges)
    Attempts to find the flow edge from startNode to endNode; if found, updates the edge's direction and m_Capacity and writes it back through the ComponentLookup, returning true. Returns false if the edge does not exist.

  • public static Entity CreateFlowEdge(EntityCommandBuffer commandBuffer, EntityArchetype edgeArchetype, Entity startNode, Entity endNode, FlowDirection direction, int capacity)
    Creates a new ElectricityFlowEdge entity using a non-parallel EntityCommandBuffer. Asserts start/end are not Entity.Null. Sets the ElectricityFlowEdge component (m_Start, m_End, direction, m_Capacity) and appends a ConnectedFlowEdge entry to both nodes' buffers. Returns the created edge entity.

  • public static Entity CreateFlowEdge(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, EntityArchetype edgeArchetype, Entity startNode, Entity endNode, FlowDirection direction, int capacity)
    Same as above but for use inside jobs with EntityCommandBuffer.ParallelWriter; requires jobIndex for the command calls.

  • public static Entity CreateFlowEdge(EntityManager entityManager, EntityArchetype edgeArchetype, Entity startNode, Entity endNode, FlowDirection direction, int capacity)
    Creates the flow edge immediately via EntityManager (main thread). Asserts start/end non-null, sets component data on the created entity and adds ConnectedFlowEdge buffer entries to both nodes. Returns the created entity.

  • public static void DeleteFlowNode(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, Entity node, ref BufferLookup<ConnectedFlowEdge> flowConnections)
    Marks the given node and all its connected edge entities with the Deleted component using a ParallelWriter (job-friendly delete). Iterates the ConnectedFlowEdge buffer for the node and adds Deleted to each connected edge entity as well as the node itself.

  • public static void DeleteFlowNode(EntityManager entityManager, Entity node)
    Main-thread variant: adds the Deleted component to the node and to every connected edge entity by reading the node's ConnectedFlowEdge buffer via EntityManager.

  • public static void DeleteBuildingNodes(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex, ElectricityBuildingConnection connection, ref BufferLookup<ConnectedFlowEdge> flowConnections, ref ComponentLookup<ElectricityFlowEdge> flowEdges)
    Given an ElectricityBuildingConnection, deletes (marks Deleted) the building's transformer node and any producer/consumer/charge/discharge nodes connected to the building, using flowConnections and flowEdges to locate the correct nodes. Uses helper methods like connection.GetProducerNode(ref flowEdges) to translate stored edge references to nodes. Skips any null entries.

Notes on parameters and types used: - Entity: Unity.Entities.Entity representing ECS entities. - BufferLookup: job-friendly accessor to the ConnectedFlowEdge dynamic buffer on node entities. - ComponentLookup: job-friendly accessor to ElectricityFlowEdge components by entity. - ConnectedFlowEdge: expected buffer element type holding an edge entity reference (m_Edge). - ElectricityFlowEdge: component containing at least m_Start, m_End, direction, m_Capacity fields. - FlowDirection: enum for allowed flow directions. - Deleted: marker component used by the systems that actually remove entities. - ElectricityBuildingConnection: struct/component that stores a building's related edge/entity references and helpers such as GetProducerNode/GetConsumerNode, etc. - EntityCommandBuffer / ParallelWriter / EntityManager / EntityArchetype: standard Unity ECS APIs used to create entities and manipulate components in main-thread and job contexts.

Threading and safety: - Use the EntityCommandBuffer.ParallelWriter overloads when executing inside jobs; use the EntityManager or non-parallel EntityCommandBuffer on the main thread. - BufferLookup/ComponentLookup are read (or read/write via indexer) style accessors intended for use inside systems/jobs; ensure they are properly constructed (with correct access mode) and up-to-date for your system's scheduling. - Assertions (Unity.Assertions) are present in several methods; these run in development builds (UNITY_ASSERTIONS) to catch invalid inputs but are stripped in release builds.

Tips and caveats: - Ensure the nodes you pass to CreateFlowEdge already have a ConnectedFlowEdge dynamic buffer present (i.e., the node entity archetype includes that buffer). Otherwise appending to the buffer will fail. - For CreateFlowEdge you must supply an appropriate edgeArchetype that includes the ElectricityFlowEdge component. - Deleted component here is used as a soft-delete marker; the actual removal is typically handled by another system that processes Deleted entities. - Lookup methods consider direction / endpoints exactly as m_Start == startNode && m_End == endNode. HasAnyFlowEdge performs a bidirectional check (start↔end) when checking existence.

Usage Example

// Create an edge on the main thread using EntityManager:
Entity edgeArchetype = entityManager.CreateArchetype(typeof(ElectricityFlowEdge));
Entity edge = ElectricityGraphUtils.CreateFlowEdge(entityManager, edgeArchetype, startNode, endNode, FlowDirection.Bidirectional, capacity: 100);

// Update an existing edge if present (inside a system using BufferLookup/ComponentLookup):
if (ElectricityGraphUtils.TrySetFlowEdge(startNode, endNode, FlowDirection.OneWay, 150, ref bufferLookup, ref componentLookup))
{
    // Edge existed and was updated
}

// Delete all nodes/edges for a building inside a job using a command buffer:
ElectricityGraphUtils.DeleteBuildingNodes(commandBufferParallel, jobIndex, connection, ref flowConnections, ref flowEdges);

If you want, I can also: - Generate example system code showing how to construct the BufferLookup/ComponentLookup and call these helpers inside an ISystem/Job. - Produce a small checklist of required components/archetypes to use these utilities safely.