Skip to content

Game.SubLaneSystem

Assembly: Game
Namespace: Game.Serialization

Type: class

Base: GameSystemBase

Summary:
SubLaneSystem is an ECS system that collects lane entities (Lane + Owner) and appends SubLane entries into the Owner's SubLane dynamic buffer. It inspects lane-component markers (PedestrianLane, CarLane, TrackLane, ParkingLane, ConnectionLane) to build a PathMethod bitmask describing what kind of path traffic the sub-lane supports (e.g., Road, Pedestrian, Track, Parking, Boarding, SpecialParking). The system runs as a Burst-compiled IJobChunk (SubLaneJob) for high performance and uses BufferLookup to append to owner buffers. It requires entities with Lane and Owner components and will be scheduled automatically by the ECS update.


Fields

  • private EntityQuery m_Query
    This query matches entities that represent lanes and have an Owner (ComponentType.ReadOnly(), ComponentType.ReadOnly()). The query is required for update in OnCreate, so the system only runs if matching entities exist.

  • private TypeHandle __TypeHandle
    Holds cached Entity/Component/Buffer type handles (wrapped in the nested TypeHandle struct) used to obtain EntityTypeHandle, ComponentTypeHandles (Owner, PedestrianLane, CarLane, TrackLane, ParkingLane, ConnectionLane) and a BufferLookup. These handles are assigned in OnCreateForCompiler (via __AssignHandles) and then converted to runtime handles in OnUpdate via InternalCompilerInterface.Get* helpers.

Properties

  • None (the system exposes no public properties)

Constructors

  • public SubLaneSystem()
    Default constructor. Marked with [Preserve]. Initializes the system instance; most runtime initialization is performed in OnCreate/OnCreateForCompiler.

Methods

  • protected override void OnCreate()
    Initializes the EntityQuery used by the system (m_Query) to match Lane entities with an Owner. Calls RequireForUpdate(m_Query) to prevent the system from updating when there are no matching entities. Marked with [Preserve].

  • protected override void OnUpdate()
    Creates and schedules the Burst-compiled SubLaneJob. It converts the cached TypeHandle fields into runtime EntityTypeHandle/ComponentTypeHandles/BufferLookup via InternalCompilerInterface.Get* methods (passing base.CheckedStateRef) and assigns them to the job struct fields. The job is scheduled with JobChunkExtensions.Schedule and the system's base.Dependency is updated accordingly.

  • protected override void OnCreateForCompiler()
    Compiler helper that assigns type handles by calling __AssignQueries and __TypeHandle.__AssignHandles(ref base.CheckedStateRef). This ensures the nested TypeHandle struct is populated for code that expects compiler-time initialization.

  • private void __AssignQueries(ref SystemState state)
    Compiler-generated helper that (in this class) contains no runtime query assignment other than a dummy EntityQueryBuilder(Allocator.Temp).Dispose(); — kept for generated-code compatibility.

Nested types and important logic:

  • SubLaneJob (private struct, BurstCompile, implements IJobChunk)
  • Purpose: iterates chunks of lane entities, reads their Owner and optional lane marker components, and appends a SubLane(laneEntity, pathMethodMask) entry to the Owner's SubLane buffer.
  • Read-only handles: EntityTypeHandle, ComponentTypeHandle, and ComponentTypeHandles for PedestrianLane, CarLane, TrackLane, ParkingLane, ConnectionLane.
  • Buffer access: BufferLookup m_SubLanes — used to get the owner's SubLane buffer (m_SubLanes.TryGetBuffer(owner.m_Owner, out var bufferData)).
  • PathMethod construction:
    • Sets PathMethod.Pedestrian if the chunk has PedestrianLane.
    • Sets PathMethod.Road if the chunk has CarLane.
    • Sets PathMethod.Track if the chunk has TrackLane.
    • If a ParkingLane exists, it inspects ParkingLaneFlags.SpecialVehicles:
    • If SpecialVehicles flag is set → adds PathMethod.Boarding | PathMethod.SpecialParking.
    • Otherwise → adds PathMethod.Parking | PathMethod.Boarding.
    • If a ConnectionLane exists, it inspects ConnectionLaneFlags and adds PathMethod flags accordingly (Pedestrian, Road, Track, Parking → Parking|Boarding).
  • For each lane entity in the chunk:

    • Attempts to get the owner's SubLane buffer; if not present, it skips.
    • Adds a new SubLane entry constructed from the lane Entity and the computed PathMethod bitmask.
  • TypeHandle (private struct)

  • Contains the EntityTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, ComponentTypeHandle, and BufferLookup.
  • __AssignHandles(ref SystemState state) assigns those handles via state.GetEntityTypeHandle(), state.GetComponentTypeHandle(isReadOnly: true) and state.GetBufferLookup().

Performance and safety notes: - The job is Burst-compiled for speed and uses IJobChunk to process archetype chunks. - ComponentTypeHandles for lane markers are requested as read-only — the job does not mutate component data aside from appending to the owner's SubLane buffer. - BufferLookup is used in a thread-safe way by calling TryGetBuffer and appending; owners without a SubLane buffer are skipped, so ensure appropriate owners have buffers added before this system runs if needed. - The system relies on CollectionUtils.TryGet for safe indexed access to optional components (ParkingLane and ConnectionLane) within the chunk.

Usage Example

// Ensure each owner entity that should receive SubLane entries has a SubLane dynamic buffer:
EntityManager.AddBuffer<SubLane>(ownerEntity);

// The SubLaneSystem is an ECS system and will run automatically when the world updates.
// No explicit call is required; however, if you need to force it manually in custom bootstrap:
var world = World.DefaultGameObjectInjectionWorld;
var system = world.GetExistingSystemManaged<Game.Serialization.SubLaneSystem>();
system.Update(); // schedules the SubLaneJob and appends SubLane entries for matching lanes.

Additional tips: - If you create or remove Lane/Owner entities at runtime, ensure the Owner entity's SubLane buffer exists (AddBuffer) before the next frame, otherwise lanes for that owner will be skipped until the buffer exists. - The PathMethod enum bitmask produced by the job encodes multiple capabilities — use bit checks to determine allowed movement types for a SubLane.