Skip to content

Game.Simulation.TransformerAISystem

Assembly:
Assembly-CSharp (game runtime assembly; typical Unity mod code)

Namespace:
Game.Simulation

Type:
public class TransformerAISystem : GameSystemBase

Base:
GameSystemBase

Summary:
TransformerAISystem is a Unity ECS system that updates transformer buildings' electricity flow edges each system tick it runs. It schedules a Burst-compiled IJobChunk (TransformerTickJob) which iterates transformer entities, reads the building efficiency buffer, and sets the direction of connected electricity flow edges to FlowDirection.Both when the building efficiency is greater than 0, or FlowDirection.None when the efficiency is 0. The system runs on a periodic interval (GetUpdateInterval returns 128) and only runs while there are Transformer prefabs matching the query (RequireForUpdate called).


Fields

  • private EntityQuery m_TransformerQuery
    Query selecting transformer prefab entities with ElectricityBuildingConnection. Constructed in OnCreate and used to schedule the job and to gate system updates (RequireForUpdate).

  • private TransformerAISystem.TypeHandle __TypeHandle
    Container for cached ComponentTypeHandle / BufferTypeHandle / BufferLookup / ComponentLookup used to build job data. Assigned via __AssignHandles in OnCreateForCompiler / compiler-generated initialization.

Properties

  • (none public)
    This system exposes no public properties.

Constructors

  • public TransformerAISystem()
    Default constructor (marked with [Preserve] in source). No runtime initialization beyond base constructor; real setup happens in OnCreate / OnCreateForCompiler.

Methods

  • public override int GetUpdateInterval(SystemUpdatePhase phase)
    Returns 128. Indicates the system's update interval (it runs once every 128 frames for the given update phase).

  • public override int GetUpdateOffset(SystemUpdatePhase phase)
    Returns 0. The update offset within the interval.

  • [Preserve] protected override void OnCreate()
    Creates the EntityQuery: GetEntityQuery(ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.ReadOnly(), ComponentType.Exclude(), ComponentType.Exclude());
    Calls RequireForUpdate(m_TransformerQuery) so the system only updates when matching entities exist.

  • [Preserve] protected override void OnUpdate()
    Builds and schedules the TransformerTickJob. It populates the job's component/buffer handles using InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle / GetBufferLookup / GetComponentLookup, then calls JobChunkExtensions.ScheduleParallel(jobData, m_TransformerQuery, base.Dependency) to run the job in parallel and updates base.Dependency.

  • protected override void OnCreateForCompiler()
    Called during compiler-time setup in generated code paths. Calls __AssignQueries and __TypeHandle.__AssignHandles to initialize cached handles.

  • [MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
    Currently a stub that creates and disposes an EntityQueryBuilder; present for compiler-generated query assignment points.

  • (Nested) private struct TransformerTickJob : IJobChunk

  • Fields:
    • [ReadOnly] ComponentTypeHandle<ElectricityBuildingConnection> m_BuildingConnectionType
    • [ReadOnly] BufferTypeHandle<Efficiency> m_EfficiencyType
    • [ReadOnly] BufferLookup<ConnectedFlowEdge> m_ConnectedFlowEdges
    • [NativeDisableParallelForRestriction] ComponentLookup<ElectricityFlowEdge> m_FlowEdges
  • Execute logic:
    • For each chunk, get ElectricityBuildingConnection array and Efficiency buffer accessor.
    • For each entity in chunk:
    • Read ElectricityBuildingConnection and determine building efficiency via BuildingUtils.GetEfficiency(bufferAccessor, index).
    • If electricityBuildingConnection.m_TransformerNode == Entity.Null, log an error: "Transformer is missing transformer node!" and skip.
    • Otherwise, iterate the ConnectedFlowEdge buffer for the transformer node; for each edge, read the ElectricityFlowEdge component, set value.direction to FlowDirection.Both if efficiency > 0f, otherwise FlowDirection.None, and write it back via the ComponentLookup.
  • Marked [BurstCompile].

  • (Nested) private struct TypeHandle

  • Fields store component/buffer handle types and a method __AssignHandles(ref SystemState) which initializes them from the SystemState:
    • ComponentTypeHandle (read-only)
    • BufferTypeHandle
    • BufferLookup
    • ComponentLookup
  • Used by OnCreateForCompiler to populate __TypeHandle.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    m_TransformerQuery = GetEntityQuery(
        ComponentType.ReadOnly<Game.Buildings.Transformer>(),
        ComponentType.ReadOnly<ElectricityBuildingConnection>(),
        ComponentType.ReadOnly<PrefabRef>(),
        ComponentType.Exclude<Temp>(),
        ComponentType.Exclude<Deleted>()
    );
    RequireForUpdate(m_TransformerQuery);
}

[Preserve]
protected override void OnUpdate()
{
    var jobData = new TransformerTickJob
    {
        m_BuildingConnectionType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Simulation_ElectricityBuildingConnection_RO_ComponentTypeHandle, ref base.CheckedStateRef),
        m_EfficiencyType = InternalCompilerInterface.GetBufferTypeHandle(ref __TypeHandle.__Game_Buildings_Efficiency_RW_BufferTypeHandle, ref base.CheckedStateRef),
        m_ConnectedFlowEdges = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Simulation_ConnectedFlowEdge_RW_BufferLookup, ref base.CheckedStateRef),
        m_FlowEdges = InternalCompilerInterface.GetComponentLookup(ref __TypeHandle.__Game_Simulation_ElectricityFlowEdge_RW_ComponentLookup, ref base.CheckedStateRef)
    };
    base.Dependency = JobChunkExtensions.ScheduleParallel(jobData, m_TransformerQuery, base.Dependency);
}

Notes and implementation details: - The job is Burst-compiled for performance; it uses ComponentLookup to read/write ElectricityFlowEdge components from arbitrary entities (ConnectedFlowEdge entries reference other entities). - If a transformer entity lacks a valid transformer node (m_TransformerNode is Entity.Null), the system logs an error and skips updating its edges. - This system depends on the Efficiency buffer (from Buildings.Efficiency) and ConnectedFlowEdge buffers to determine which flow edges to modify.