Skip to content

Game.Prefabs.VehicleInitializeSystem

Assembly: Game
Namespace: Game.Prefabs

Type: class

Base: GameSystemBase

Summary:
Initializes vehicle-related prefab component data (cars, trains, tractors, trailers and their shared vehicle data) when prefabs are created. Reads prefab authoring data (via PrefabSystem) and object geometry / procedural bone buffers to compute runtime parameters such as steering bone indices, pivot offsets, swaying/spring/damping factors, bogie offsets, attach positions and various vehicle flags. The system performs part of the initialization on the main thread (reading prefabs) and part in a parallel job (scanning procedural bones and computing derived geometry-based values) for performance.


Fields

  • private EntityQuery m_PrefabQuery
    Holds the query that selects newly created prefab entities with PrefabData and any of CarData / TrainData / CarTractorData / CarTrailerData. Required for update; used to gather archetype chunks to initialize.

  • private PrefabSystem m_PrefabSystem
    Cached reference to the PrefabSystem (authoring runtime) used to resolve prefab objects (TrainPrefab, CarBasePrefab, CarTrailerPrefab, VehiclePrefab etc.) and read their properties (max speed, acceleration, trailer types, offsets, components, etc.).

  • private TypeHandle __TypeHandle
    Container struct that stores ComponentTypeHandle and BufferTypeHandle instances used by the system. Populated via __AssignHandles / InternalCompilerInterface at runtime.

  • private struct InitializeVehiclesJob (nested)
    IJobParallelFor that performs chunk-parallel initialization of procedural-bone-based data: sets VehicleData.m_SteeringBoneIndex, computes SwayingData (spring factors, damping, max position, velocity factors) and Car/Train geometry-derived parameters such as pivot offsets and bogie offsets. Uses BufferLookup to access per-submesh bone buffers.

  • private struct TypeHandle (nested)
    Wrapper for all ComponentTypeHandle and BufferTypeHandle/BufferLookup used by the system. Contains method __AssignHandles(ref SystemState) which calls state.GetComponentTypeHandle / GetBufferTypeHandle / GetBufferLookup to populate handles.

Properties

  • (none public)
    This system exposes no public properties.

Constructors

  • public VehicleInitializeSystem()
    Default constructor; preserved for managed system creation.

Methods

  • protected override void OnCreate()
    Creates and configures the system on startup. Obtains PrefabSystem from the World, builds m_PrefabQuery to match entities with Created and PrefabData and any of CarData/TrainData/CarTractorData/CarTrailerData, and calls RequireForUpdate(m_PrefabQuery) so the system runs only when matching prefabs exist.

  • protected override void OnUpdate()
    Main runtime logic:

  • Converts m_PrefabQuery to an ArchetypeChunk array (Allocator.TempJob).
  • Obtains ComponentTypeHandle and BufferTypeHandle instances from __TypeHandle via InternalCompilerInterface.GetComponentTypeHandle / GetBufferTypeHandle.
  • Calls CompleteDependency() to ensure previous dependent jobs are finished.
  • Iterates chunks on the main thread:
    • For TrainData chunks: reads TrainPrefab via m_PrefabSystem and maps prefab values (track/energy type, max speed in m/s, acceleration, braking, turning, bogie & attach offsets).
    • For CarData chunks: reads CarBasePrefab and maps values (size class, energy type, max speed, acceleration, braking, turning, stiffness -> swaying spring factors).
    • For CarTractorData: reads VehiclePrefab component CarTractor to set trailer type and attach positions; resolves fixed trailer entity if specified.
    • For CarTrailerData: reads CarTrailerPrefab to set trailer/movement type, attach positions and resolve fixed tractor entity if specified.
  • Schedules InitializeVehiclesJob (IJobParallelFor) over the chunk array to compute procedural-bone dependent values (steering bone index, swaying data adjustments, pivots, bogie offsets, multi-unit train flag, pantograph flag).
  • Stores the returned JobHandle into base.Dependency.

  • private void __AssignQueries(ref SystemState state)
    Generated helper used by compiler-time setup; current implementation creates/disposes a temporary EntityQueryBuilder (no runtime behavior beyond compiler plumbing in this file).

  • protected override void OnCreateForCompiler()
    Compiler-time helper: calls __AssignQueries and __TypeHandle.__AssignHandles to ensure the system's handles and queries are registered for the Burst/AOT compiled paths.

  • private void TypeHandle.__AssignHandles(ref SystemState state) (nested)
    Populates all ComponentTypeHandle and Buffer handles required by the system (PrefabData, ObjectGeometryData, MultipleUnitTrainData, TrainData, CarData, SwayingData, VehicleData, CarTractorData, CarTrailerData, SubMesh buffer, ProceduralBone buffer lookup). Called from OnCreateForCompiler / system initialization.

  • private void InitializeVehiclesJob.Execute(int index) (nested)
    Job execution for each chunk:

  • Retrieves native arrays for ObjectGeometryData, CarTrailerData, MultipleUnitTrainData flag, CarData, TrainData, SwayingData, VehicleData and buffer accessor for SubMesh.
  • For each VehicleData in the chunk: scans submeshes' ProceduralBone buffers (via m_ProceduralBones buffer lookup) and sets VehicleData.m_SteeringBoneIndex when it finds BoneType.SteeringRotation (or counts/skips as necessary).
  • For each CarData: computes pivot offset from wheel/steering bones, bounds, and sets SwayingData parameters (spring factors, damping, velocityFactors, max position) based on object geometry sizes and found procedural bones; includes fallbacks when bones are missing.
  • For each TrainData: scans for TrainBogie bones and PantographRotation bones, sets TrainFlags.MultiUnit if MultipleUnitTrainData exists, computes bogie offsets based on found bogie positions and object bounds, with fallbacks if fewer bogies found.

  • private bool InitializeVehiclesJob.HasSteering(DynamicBuffer<ProceduralBone> bones, ProceduralBone bone) (nested)
    Utility used in the job: checks whether a bone or any of its parent chain has a steering-related bone type (SteeringTire, SteeringRotation, SteeringSuspension). Used to separate steering vs non-steering wheels when computing pivots and offsets.

Usage Example

// This system is a managed ECS system. Typical usage is letting the world create it,
// but you can observe its lifecycle by overriding OnCreate / OnUpdate in derived systems.
// Example (conceptual):

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // This system obtains the PrefabSystem and registers its EntityQuery in its OnCreate,
    // so normally you don't need to do additional setup here.
}

[Preserve]
protected override void OnUpdate()
{
    base.OnUpdate();
    // The system will gather created prefab chunks, initialize Car/Train/Trailer/Tractor
    // data from authoring prefabs and schedule the InitializeVehiclesJob to compute
    // procedural-bone derived parameters.
}

Notes and tips for modders: - VehicleInitializeSystem relies on PrefabSystem to resolve authoring prefabs (TrainPrefab, CarBasePrefab, CarTrailerPrefab, VehiclePrefab). If you introduce new vehicle prefab types, ensure they are registered and that authoring components provide expected fields. - Procedural bone names/types are important: steering-related bones (SteeringRotation/Tire/Suspension) and train bogie bones (TrainBogie) are used to compute runtime offsets. Adding or altering bones in the model can change runtime computed pivots and swaying behavior. - The system schedules a parallel job (IJobParallelFor) that reads procedural bone buffers via a BufferLookup. Ensure your added buffers / components are accessible and consistent with packing expected by the job.