Skip to content

Game.Prefabs.TrainPrefab

Assembly: Assembly-CSharp.dll
Namespace: Game.Prefabs

Type: abstract class

Base: VehiclePrefab

Summary:
TrainPrefab is the base prefab definition for rail vehicles in the game. It declares train-specific configuration (track type, energy type, performance and physical offsets) and supplies the prefab/component/archetype wiring required to spawn train entities with the correct ECS components. The class is marked with [ExcludeGeneratedModTag], merges base mod tags with track-type enum flags, and overrides the component/archetype setup routines to create specialized archetypes used by moving and stopped train controllers. It also initializes UpdateFrameData for spawned prefabs.


Fields

  • public TrackTypes m_TrackType = TrackTypes.Train
    Specifies which track type this train uses (default Train). Used to generate mod tags and for runtime behaviour that depends on rail type.

  • public EnergyTypes m_EnergyType = EnergyTypes.Electricity
    Energy/fuel type the train consumes (default Electricity). Affects service/economy and compatibility with energy systems.

  • public float m_MaxSpeed = 200f
    Maximum speed of the train in game units (default 200).

  • public float m_Acceleration = 5f
    Acceleration value applied when the train accelerates.

  • public float m_Braking = 10f
    Braking (deceleration) value applied when slowing/stopping.

  • public float2 m_Turning = new float2(90f, 15f)
    Turning limits / parameters (x,y) used by train steering/turning logic. Defaults imply turn behavior tuned for rail vehicles.

  • public float2 m_BogieOffset = new float2(4f, 4f)
    Offsets for bogies (wheel assemblies). Typically used to position bogie frames relative to the vehicle body; first/second element are two offset values.

  • public float2 m_AttachOffset = new float2(0f, 0f)
    Attachment offset used when coupling wagons or connecting to other elements (default zero).

Properties

  • public override IEnumerable<string> modTags { get; }
    Returns mod tags for this prefab. This implementation yields the base VehiclePrefab modTags and additionally yields enum-flag derived tags for the configured m_TrackType (via ModTags.GetEnumFlagTags). These tags are used by the mod/content system to filter or identify compatible assets.

Constructors

  • public TrainPrefab()
    No explicit constructor is declared in source; the default parameterless constructor is available. Use subclass constructors to set defaults or perform setup if needed.

Methods

  • public override void GetPrefabComponents(HashSet<ComponentType> components)
    Adds the train-specific component types to the provided set of prefab components. Calls base.GetPrefabComponents then adds:
  • TrainData
  • TrainObjectData
  • UpdateFrameData
    These ensure the prefab has data storages required for train runtime data and update scheduling.

  • public override void GetArchetypeComponents(HashSet<ComponentType> components)
    Augments archetype component sets required when building entity archetypes from this prefab. It:

  • Adds Train component.
  • If Stopped is present, adds ParkedTrain.
  • If Moving is present, adds TrainNavigation, TrainCurrentLane, TrainBogieFrame.
  • If LayoutElement is present alongside Moving, also adds PathOwner, PathElement, Target, Blocker, TrainNavigationLane.
    This builds conditional component lists so moving vs stopped controllers get the correct components.

  • protected override void RefreshArchetype(EntityManager entityManager, Entity entity)
    Creates and assigns runtime archetypes used by the prefab. Implementation overview:

  • Collects all ComponentBase instances from GetComponents(list).
  • Prepares three data structures: ObjectData, MovingObjectData, TrainObjectData (local componentData variables).
  • Builds multiple component-type sets and creates archetypes with entityManager.CreateArchetype:
    1. A general archetype for controller + Moving (plus components from components list) assigned to ObjectData.m_Archetype.
    2. A stopped archetype for controller + Stopped assigned to MovingObjectData.m_StoppedArchetype.
    3. A controller archetype including LayoutElement for controller+Moving+LayoutElement assigned to TrainObjectData.m_ControllerArchetype.
    4. A stopped controller archetype including LayoutElement for controller+Stopped+LayoutElement assigned to TrainObjectData.m_StoppedControllerArchetype.
  • Ensures Created and Updated component types are always included.
  • Writes these component-data structs to the entity via entityManager.SetComponentData. This method centralizes archetype creation so instantiated trains use optimized ECS archetypes matching their movement/layout states.

  • public override void Initialize(EntityManager entityManager, Entity entity)
    Calls base.Initialize then sets UpdateFrameData on the prefab entity to new UpdateFrameData(3). This configures update frame scheduling for train instances (the value 3 is a scheduling parameter used by the engine).

Usage Example

// Example: custom train prefab that tweaks defaults and optionally extends initialization.
public class MyCustomTrainPrefab : TrainPrefab
{
    public MyCustomTrainPrefab()
    {
        // Change defaults for this custom train
        m_MaxSpeed = 160f;
        m_Acceleration = 6f;
        m_Braking = 12f;
        m_TrackType = TrackTypes.Metro;
        m_EnergyType = EnergyTypes.Diesel;
        m_BogieOffset = new float2(3.5f, 3.5f);
    }

    public override void GetPrefabComponents(HashSet<ComponentType> components)
    {
        base.GetPrefabComponents(components);
        // Add custom component types if needed:
        // components.Add(ComponentType.ReadWrite<MyCustomTrainComponent>());
    }

    public override void Initialize(EntityManager entityManager, Entity entity)
    {
        base.Initialize(entityManager, entity);
        // Additional per-prefab initialization logic, if required.
        // e.g. Set extra componentdata for the prefab entity
    }
}