Game.RouteVehicleSystem
Assembly: Assembly-CSharp
Namespace: Game.Serialization
Type: class
Base: GameSystemBase
Summary:
RouteVehicleSystem is an ECS system that collects all vehicles that have a CurrentRoute component and appends a RouteVehicle record to the RouteVehicle buffer on the matching route entity. It uses a Burst-compiled IJobChunk (RouteVehicleJob) to iterate chunks of Vehicle + CurrentRoute entities and a BufferLookup to append RouteVehicle entries to each route's dynamic buffer. The system requires the presence of Vehicle + CurrentRoute components to run and schedules its job via the Entities job system.
Fields
-
private EntityQuery m_Query
Holds the query used to select entities that have both Vehicle and CurrentRoute components. The query is created in OnCreate and used to schedule the RouteVehicleJob in OnUpdate. -
private TypeHandle __TypeHandle
Internal struct instance that stores computed type handles (EntityTypeHandle, ComponentTypeHandle, BufferLookup ). These handles are assigned in OnCreateForCompiler (via __AssignHandles) and used to build the job data in OnUpdate.
Nested Types
private struct RouteVehicleJob : IJobChunk
- Burst-compiled chunk job that:
- Reads the entity array and CurrentRoute component array from each chunk.
- For each entity in the chunk, gets the CurrentRoute.m_Route (an Entity referring to the route).
- Uses BufferLookup
.TryGetBuffer on the route entity and, if the buffer exists, appends a new RouteVehicle entry (wrapping the vehicle entity).
-
Fields:
EntityTypeHandle m_EntityType
(ReadOnly) — to get the entity array from the chunk.ComponentTypeHandle<CurrentRoute> m_CurrentRouteType
(ReadOnly) — to get CurrentRoute components.BufferLookup<RouteVehicle> m_RouteVehicles
— to access route buffers for writing.
-
private struct TypeHandle
- Holds the individual handles required by the job:
EntityTypeHandle __Unity_Entities_Entity_TypeHandle
ComponentTypeHandle<CurrentRoute> __Game_Routes_CurrentRoute_RO_ComponentTypeHandle
BufferLookup<RouteVehicle> __Game_Routes_RouteVehicle_RW_BufferLookup
- Method:
__AssignHandles(ref SystemState state)
— fetches and stores the type handles from the provided SystemState (GetEntityTypeHandle, GetComponentTypeHandle, GetBufferLookup ).
Properties
- (none)
Constructors
public RouteVehicleSystem()
Empty constructor with [Preserve] attribute. The system initialization logic is performed in OnCreate / OnCreateForCompiler.
Methods
protected override void OnCreate()
Creates the entity query selecting entities with Vehicle and CurrentRoute components:- m_Query = GetEntityQuery(ComponentType.ReadOnly
(), ComponentType.ReadOnly ()); -
Calls RequireForUpdate(m_Query) so the system only runs when there are matching entities.
-
protected override void OnUpdate()
Builds a RouteVehicleJob instance by obtaining the compiled type handles via InternalCompilerInterface and schedules it over m_Query using JobChunkExtensions.Schedule. The scheduled job appends RouteVehicle entries to route buffers for each vehicle with a CurrentRoute. -
protected override void OnCreateForCompiler()
Internal helper called by the generated code path: - Calls __AssignQueries(ref base.CheckedStateRef) (which currently constructs and disposes an empty EntityQueryBuilder).
-
Calls __TypeHandle.__AssignHandles(ref base.CheckedStateRef) to initialize type handles used by the job.
-
private void __AssignQueries(ref SystemState state)
Internal method used at compile-time to set up any required queries; here it constructs and immediately disposes an EntityQueryBuilder(Allocator.Temp). Present for compiler compatibility. -
RouteVehicleJob.Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
The core execution method that iterates entities within a chunk and appends RouteVehicle items into the route's dynamic buffer.
Notes / Cautions:
- The system uses BufferLookup
Usage Example
Example pattern showing how this system registers its query in OnCreate and schedules a chunk job in OnUpdate (simplified extract mirroring this system's behavior):
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Only run when there are vehicles with a CurrentRoute
m_Query = GetEntityQuery(ComponentType.ReadOnly<Vehicle>(), ComponentType.ReadOnly<CurrentRoute>());
RequireForUpdate(m_Query);
}
[Preserve]
protected override void OnUpdate()
{
var jobData = new RouteVehicleJob {
m_EntityType = InternalCompilerInterface.GetEntityTypeHandle(ref __TypeHandle.__Unity_Entities_Entity_TypeHandle, ref base.CheckedStateRef),
m_CurrentRouteType = InternalCompilerInterface.GetComponentTypeHandle(ref __TypeHandle.__Game_Routes_CurrentRoute_RO_ComponentTypeHandle, ref base.CheckedStateRef),
m_RouteVehicles = InternalCompilerInterface.GetBufferLookup(ref __TypeHandle.__Game_Routes_RouteVehicle_RW_BufferLookup, ref base.CheckedStateRef)
};
base.Dependency = JobChunkExtensions.Schedule(jobData, m_Query, base.Dependency);
}
This system is intended to run inside the Cities: Skylines 2 ECS-based modding/runtime environment and interacts with the game's Route and Vehicle components to populate route buffers used by the routing/vehicle subsystems.