Skip to content

Game.Net.CompositionSelectSystem

Assembly: Assembly-CSharp
Namespace: Game.Net

Type: class

Base: GameSystemBase

Summary: CompositionSelectSystem is an ECS system used by the net (road/track) pipeline to select and create geometry "compositions" for net edges and nodes. It inspects edges, nodes and their prefabs / subobjects / upgrades / elevation to compute CompositionFlags that determine which geometry composition (prebaked set of pieces) should be used for rendering/physics. The system runs a parallel IJobChunk (SelectCompositionJob) to compute flags and enqueue missing compositions, and a follow-up IJob (CreateCompositionJob) to create composition entities when no matching prefab composition exists. It integrates with the game's ModificationBarrier to issue entity commands for composition creation and updated components, and respects city configuration (left/right-hand traffic) and loaded-game semantics.


Fields

  • private NetCompositionSystem m_NetCompositionSystem
    Holds a reference to the NetCompositionSystem (used for composition related services and helpers).

  • private CityConfigurationSystem m_CityConfigurationSystem
    Reference to city config (used here primarily to read leftHandTraffic setting).

  • private ModificationBarrier3 m_ModificationBarrier
    Command-buffer barrier used to safely apply structural/component changes from jobs (producer-consumer synchronization).

  • private EntityQuery m_UpdatedQuery
    EntityQuery used when only updated entities need processing (normal incremental update).

  • private EntityQuery m_AllQuery
    EntityQuery used when the system must re-process all relevant entities (e.g., when a game has just been loaded).

  • private bool m_Loaded
    Flag toggled by OnGameLoaded to force a full pass (m_AllQuery) on the next update.

  • private TypeHandle __TypeHandle
    Compiled cache of Entity/Component/Buffer handles used to build and schedule jobs (auto-generated helper).


Properties

  • (none)

The system does not expose public properties.


Constructors

  • public CompositionSelectSystem()
    Default constructor. The system also implements OnCreate to initialize references and entity queries. This constructor is preserved for the ECS system lifecycle.

Methods

  • protected override void OnCreate()
    Initializes the system: gets or creates dependent systems (NetCompositionSystem, CityConfigurationSystem, ModificationBarrier3), and constructs the two EntityQueries (m_UpdatedQuery and m_AllQuery). Called once when the system is created.

  • protected override void OnGameLoaded(Context serializationContext)
    Called by the game when a save/game load finishes. Sets m_Loaded = true so the next update will use m_AllQuery to re-evaluate compositions for all matching entities.

  • private bool GetLoaded()
    Internal helper used in OnUpdate to check+reset the m_Loaded flag. Returns true exactly once after OnGameLoaded until cleared.

  • protected override void OnUpdate()
    Main update loop for the system. Chooses between m_AllQuery and m_UpdatedQuery based on GetLoaded(). Creates a NativeQueue for composition creation requests, populates SelectCompositionJob (IJobChunk) to compute composition flags and enqueue missing compositions, then schedules CreateCompositionJob (IJob) to materialize any missing composition entities and apply component changes via an EntityCommandBuffer from the ModificationBarrier. Properly wires job dependencies and disposes the queue after the jobs finish.

  • private void __AssignQueries(ref SystemState state)
    Internal (auto-generated) query assignment helper used during the compiled/IL-to-C# translation for ECS. No custom logic in the implementation shown.

  • protected override void OnCreateForCompiler()
    Internal helper called to set up query and type handle assignments during compilation / editor usage. Calls __AssignQueries and __TypeHandle.__AssignHandles.

  • private void __AssignHandles(ref SystemState state) (in nested TypeHandle)
    Auto-generated method that binds EntityTypeHandle, ComponentTypeHandle, ComponentLookup and BufferLookup instances used by the scheduled jobs.

  • private void FindFriendEdges(Entity edge, Entity node, Layer mergeLayers, Curve curve, out Entity leftEdge, out Entity rightEdge) (in nested SelectCompositionJob)
    Helper used by the job to identify adjacent "friend" edges at a node so handedness and left/right decisions can be made.

  • SelectCompositionJob.Execute(...) (IJobChunk)
    The job that iterates matching chunks of net edge and/or orphan entities. It:

  • Reads edge/curve/prefab data, upgrades, fixed states, subobjects and elevation.
  • Computes CompositionFlags for edges, start nodes and end nodes.
  • Attempts to find a matching composition in the prefab's NetGeometryCompositions buffer.
  • If a composition entity is missing or some flags are obsolete, enqueues a CompositionCreateInfo into the NativeQueue for later creation by CreateCompositionJob. Otherwise writes the Composition component directly into the entity.

  • CreateCompositionJob.Execute() (IJob)
    Dequeues requested compositions and ensures a composition entity exists for each unique (prefab, flags) pair (using a temporary NativeParallelHashMap cache). It creates composition entities by:

  • Allocating either edge- or node-composition archetypes from NetGeometryData.
  • Appending NetGeometryComposition buffer entries to the prefab.
  • Creating NetCompositionPiece buffer contents using NetCompositionHelpers.GetCompositionPieces and copying pieces to the new composition entity.
  • Adding PrefabRef and NetCompositionData components and TerrainComposition if terrain is needed.
  • Applying Composition or Orphan components to the original entity (via the EntityCommandBuffer).
  • Removing/adjusting Upgraded components and Temp flags where obsolete flags have been cleared.

  • Other helpers in the jobs: GetSubObjectFlags, GetElevationFlags, GetEdgeFlags, GetNodeFlags, GetEdgeStates, GetNodeStates, GetHandednessFlags, GetOrCreateComposition, CreateComposition, EqualSubReplacements.
    These encapsulate logic for extracting flags from subobjects, checking terrain/elevation/handedness, testing prefab edge/node state tables, and creating/looking-up composition entities.


Usage Example

This system is an internal game ECS system, normally managed by the game's world. As a modder you typically don't instantiate it directly — you access it via the world to inspect or interact with it. Example showing how to fetch the system instance from the default world:

// Example: query the system instance from the default ECS world.
// Typically used in other systems or tools to read state or to force a world update.
using Unity.Entities;

var world = World.DefaultGameObjectInjectionWorld;
if (world != null)
{
    var compSelectSystem = world.GetExistingSystemManaged<Game.Net.CompositionSelectSystem>();
    if (compSelectSystem != null)
    {
        // You generally should not call internal methods directly.
        // Instead, let the ECS scheduler run the system normally or use other public interfaces.
        // Example: forcing a simulation update is done by the game's simulation loop, not here.
    }
}

Notes / Tips: - The system uses Burst-compiled jobs and Native containers; ensure any changes respect thread-safety and Burst constraints. - The actual geometry selection logic heavily depends on prefab NetGeometryData, NetGeometryEdgeState/NodeState buffers, and NetCompositionHelpers; to add new compositions you will typically modify prefab geometry buffers or rely on this system to create compositions on demand. - Because creation uses a ModificationBarrier's command buffer, composition entities will appear only after the barrier/job completes — avoid reading newly created composition entities from the same frame unless you handle job dependencies.