Skip to content

Game.Net.NetComponentsSystem

Assembly: Game (inferred)
Namespace: Game.Net

Type: class

Base: GameSystemBase

Summary: NetComponentsSystem is a game ECS system responsible for keeping node and edge components in sync with prefab composition and runtime topology. It inspects nodes and connected edges to add/remove or update components such as TrafficLights (including level crossings), Roundabout, and Gate based on prefab NetCompositionData and runtime geometry. The heavy per-entity work runs in a Burst-compiled IJobChunk (CheckNodeComponentsJob) scheduled in parallel and uses a ModificationBarrier4 command buffer to safely add/set/remove components from jobs.


Fields

  • private ModificationBarrier4 m_ModificationBarrier
    This system uses a ModificationBarrier4 to create an EntityCommandBuffer.ParallelWriter so jobs can safely enqueue component add/set/remove operations that will be applied later by the barrier.

  • private EntityQuery m_UpdatedNetQuery
    Query selecting entities that have Updated plus either Node or Edge. Used to do incremental updates only for changed net entities each frame.

  • private EntityQuery m_AllNetQuery
    Query selecting all Node or Edge entities. Used to perform a full rebuild/update when the game has just been loaded.

  • private bool m_Loaded
    Flag set when the game load event occurs (OnGameLoaded). Causes the next update to use the full m_AllNetQuery once, then the flag is cleared.

  • private TypeHandle __TypeHandle
    Holds cached Component/Buffer/Entity type handles and ComponentLookup handles used by the job. Populated in OnCreateForCompiler (via __AssignHandles).

  • private struct CheckNodeComponentsJob (nested, Burst-compiled)
    A Burst-compiled IJobChunk implementing the core logic. It:

  • Reads entity, Owner, TrafficLights, Composition and ConnectedEdge buffers.
  • Reads ComponentLookup data for Edge, Composition, StartNodeGeometry, EndNodeGeometry and NetCompositionData.
  • Computes composition flags for nodes and edges, determines when to add/remove/set components:
    • Add or remove TrafficLights (including toggling LevelCrossing flag).
    • Add or remove Roundabout and compute roundabout radius from prefab data and edge geometry.
    • Add or remove Gate components on edges and resolve domain from Owner chain.
  • Uses an EntityCommandBuffer.ParallelWriter (m_CommandBuffer) to mutate components from the job safely.
  • Implemented Execute(in ArchetypeChunk, int, bool, in v128) and explicit interface wrapper.

  • private struct TypeHandle (nested)
    Container for EntityTypeHandle, ComponentTypeHandle and BufferTypeHandle fields plus read-only ComponentLookup instances. Provides __AssignHandles(ref SystemState) which initializes these handles from a SystemState. This is used to map the system's cached handles to the current World/SystemState.

Properties

  • None public.
    This system does not expose public properties; it operates internally via EntityQueries and scheduled jobs. Most data is encapsulated in private fields and nested handles.

Constructors

  • public NetComponentsSystem()
    Default constructor annotated with [Preserve] to avoid stripping. Initializes the system instance; real initialization of queries and handles happens in OnCreate/OnCreateForCompiler.

Methods

  • protected override void OnCreate()
    Called when the system is created. Retrieves/creates the ModificationBarrier4 system and builds the two EntityQueries:
  • m_UpdatedNetQuery: All: Updated; Any: Node, Edge.
  • m_AllNetQuery: Any: Node, Edge. These queries control whether the system runs incrementally or on all net entities after a load.

  • protected override void OnGameLoaded(Context serializationContext)
    Called by the game when a save is loaded. Sets m_Loaded = true so the next update will run a full pass (m_AllNetQuery) to rebuild components for all nets.

  • private bool GetLoaded()
    Helper that returns true once if m_Loaded was set. Clears m_Loaded when read so the full pass runs only once after a load.

  • protected override void OnUpdate()
    Main update entry. Chooses query = (GetLoaded() ? m_AllNetQuery : m_UpdatedNetQuery). If the chosen query is non-empty, it constructs and schedules the Burst-compiled CheckNodeComponentsJob in parallel over the query using JobChunkExtensions.ScheduleParallel, passing type handles and lookups from __TypeHandle (via InternalCompilerInterface) and the command buffer from m_ModificationBarrier. The job handle is added to the modification barrier and set as the system dependency.

  • private void __AssignQueries(ref SystemState state)
    Generated helper (called from OnCreateForCompiler) that would assign or validate queries. In current file it only creates and disposes a temporary EntityQueryBuilder (no-op in this decompiled form).

  • protected override void OnCreateForCompiler()
    Compiler helper used to perform setup in build-time generated code paths. Calls __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to initialize type handles for use in job scheduling.

  • Nested job method: CheckNodeComponentsJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    Per-chunk logic that:

  • Iterates chunk entities and their ConnectedEdge buffers.
  • Gathers composition flags derived from existing TrafficLights, Roundabout, Gate components and prefab NetCompositionData for connected edges.
  • Determines differences between current runtime composition (compositionFlags) and desired composition derived from prefabs (compositionFlags2).
  • Adds/sets/removes TrafficLights component (or toggles LevelCrossing flag).
  • Adds/removes Roundabout component and computes radius from prefab and edge geometry.
  • For edges, computes Gate domain by walking Owner chain (via Owner ComponentLookup), and adds/removes Gate component as needed.
  • Uses m_CommandBuffer (parallel writer) for structural changes.

  • Nested TypeHandle method: __AssignHandles(ref SystemState state)
    Initializes each stored type handle and ComponentLookup from the provided SystemState. Called from OnCreateForCompiler so the system has ready-to-use handles.

Usage Example

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Get or create the barrier system used to apply component changes from jobs.
    m_ModificationBarrier = base.World.GetOrCreateSystemManaged<ModificationBarrier4>();

    // Query only the updated nets (fast path).
    m_UpdatedNetQuery = GetEntityQuery(new EntityQueryDesc
    {
        All = new ComponentType[1] { ComponentType.ReadOnly<Updated>() },
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Node>(),
            ComponentType.ReadOnly<Edge>()
        }
    });

    // Query all nets (used once after a load).
    m_AllNetQuery = GetEntityQuery(new EntityQueryDesc
    {
        Any = new ComponentType[2]
        {
            ComponentType.ReadOnly<Node>(),
            ComponentType.ReadOnly<Edge>()
        }
    });
}

Notes and tips for modders: - The heavy work is performed in a Burst compiled IJobChunk which expects correct ComponentTypeHandle / BufferTypeHandle and ComponentLookup instances. If you extend or change net components, update the job logic accordingly. - Structural changes (Add/Remove/SetComponent) are performed via the ModificationBarrier4 command buffer to be safe when invoked from jobs. - The system supports a fast-path incremental update via Updated; calling or setting Updated on Node/Edge entities triggers minimal work. After game load, the system forces a full pass by setting m_Loaded during OnGameLoaded.