Game.NetLaneReservationSystem
Assembly: Assembly-CSharp.dll
Namespace: Game.Simulation
Type: class
Base: GameSystemBase
Summary:
NetLaneReservationSystem is an ECS system used by the game's simulation to reset and rotate lane reservation data for lanes each simulation slice. The system:
- Runs a Burst-compiled IJobChunk (ResetLaneReservationsJob) that iterates over LaneReservation components and updates their internal reservation state.
- Filters entities by an UpdateFrame shared component so only a subset of LaneReservation components are processed each simulation frame (using m_SimulationSystem.frameIndex % 16).
- Is designed to clear stale blockers and rotate Next → Prev, then clear Next, so reservations are advanced frame-by-frame and do not persist indefinitely.
This system is performance-sensitive (uses Burst and ScheduleParallel) and intended to keep lane reservation state consistent for traffic/vehicle reservation logic.
Fields
-
private SimulationSystem m_SimulationSystem
System reference used to read the global simulation frame index. This is used to compute the UpdateFrame index (frameIndex % 16) for the shared-component filter so the job processes a partition of lane entities each update. -
private EntityQuery m_LaneQuery
EntityQuery used to select entities that contain the LaneReservation component and the UpdateFrame shared component, while excluding Deleted and Temp. The query is reset and then a shared component filter is set to process only the current slice of entities. -
private TypeHandle __TypeHandle
A private helper struct that stores ComponentTypeHandle. It is initialized in OnCreateForCompiler to allow safe, checked access to component data in jobs via InternalCompilerInterface.GetComponentTypeHandle. -
private struct ResetLaneReservationsJob
(nested)
Burst-compiled IJobChunk that contains: public ComponentTypeHandle<LaneReservation> m_LaneReservationType
— used to obtain a NativeArrayfor the chunk. -
Execute implementation: iterates each LaneReservation in the chunk and:
- If the next reservation priority is lower than the previous, clears the m_Blocker (sets to Entity.Null).
- Rotates the reservation state: sets m_Prev = m_Next and m_Next = default(ReservationData). This job runs in parallel across matching archetype chunks.
-
private struct TypeHandle
(nested)
Contains the ComponentTypeHandleand a small helper method __AssignHandles to populate it from a SystemState. This is an internal pattern to support the compiler-managed systems.
Properties
- (None public)
This system exposes no public properties. All state is internal and managed via the EntityQuery, the SimulationSystem reference, and scheduled jobs.
Constructors
public NetLaneReservationSystem()
Default constructor (marked [Preserve] on the system methods but constructor is parameterless). The system is created/managed by the ECS world rather than constructed manually for most mod scenarios.
Methods
-
[Preserve] protected override void OnCreate()
Initializes the system: obtains a reference to SimulationSystem from the World, constructs the m_LaneQuery to select LaneReservation components that should be processed, and calls RequireForUpdate(m_LaneQuery) so the system only updates when matching entities exist. -
[Preserve] protected override void OnUpdate()
Main update method: - Computes the current UpdateFrame index using m_SimulationSystem.frameIndex % 16.
- Resets the query filter and sets a shared-component filter for UpdateFrame(index) so only a fraction of LaneReservation entities are processed this frame.
-
Builds ResetLaneReservationsJob with the ComponentTypeHandle for LaneReservation (obtained via InternalCompilerInterface.GetComponentTypeHandle) and schedules it with ScheduleParallel, combining with the system's base.Dependency.
-
protected override void OnCreateForCompiler()
Called by the compiler-generated scaffolding to assign query handles and component type handles (calls __AssignQueries and TypeHandle.__AssignHandles). This method helps the emitted/compiled system code initialize the internal handles that jobs use. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Compiler helper that currently creates an EntityQueryBuilder(Allocator.Temp).Dispose(); present to satisfy generated code patterns. It is part of the class's compiler glue and is not intended to be used by modders directly. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] public void __AssignHandles(ref SystemState state)
(on nested TypeHandle)
Initializes the ComponentTypeHandlefor later use inside jobs. -
Nested ResetLaneReservationsJob.Execute(...)
Detailed above under Fields — performs the actual per-chunk reservation reset/rotation logic. It implements IJobChunk.Execute and has BurstCompile attribute.
Notes on attributes and patterns: - The job struct is decorated with [BurstCompile] to improve performance. - Several methods are marked [Preserve] to avoid stripping by Unity's linker. - The system uses InternalCompilerInterface.GetComponentTypeHandle to obtain a ComponentTypeHandle that is safe to use with the system's CheckedStateRef.
Usage Example
// This snippet shows the kind of initialization this system performs in OnCreate.
// Systems are created and managed by the ECS world; you normally do not instantiate them manually.
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Get the SimulationSystem so we can read the frame index for UpdateFrame partitioning
m_SimulationSystem = base.World.GetOrCreateSystemManaged<SimulationSystem>();
// Query for LaneReservation components that are active for processing, exclude Deleted/Temp
m_LaneQuery = GetEntityQuery(
ComponentType.ReadWrite<LaneReservation>(),
ComponentType.ReadOnly<UpdateFrame>(),
ComponentType.Exclude<Deleted>(),
ComponentType.Exclude<Temp>()
);
RequireForUpdate(m_LaneQuery);
}
Additional modding tips and caveats: - LaneReservation, ReservationData, UpdateFrame and other component definitions are not included here; they define priorities, blockers (Entity), and per-frame update partitioning. When modifying or interacting with these components, keep data layout and Burst-compatibility in mind. - The system processes only a slice of entities each frame (frameIndex % 16). If you add or mutate UpdateFrame shared components, ensure you use the same partitioning scheme to avoid races or missed updates. - Because the reset logic is scheduled via ScheduleParallel and Burst, you must not perform managed (non-blittable) operations inside the job. All data accessed by the job must be Burst-compatible. - If you need to persist or inspect reservations for debugging, do so outside the job (e.g., in the main thread or in separate systems that run after this system completes). Use job dependencies (base.Dependency) to coordinate. - The system clears m_Blocker when next priority is lower than prev priority — this behavior affects how reservation ownership and blocking are resolved; be careful if implementing custom traffic logic that relies on blockers.