Skip to content

Game.Prefabs.TrainData

Assembly: Game
Namespace: Game.Prefabs

Type: struct

Base: IComponentData, IQueryTypeParameter, ISerializable

Summary:
Represents prefab-level configuration data for trains. Contains track/energy type identifiers, flags, physical performance parameters (speed, acceleration, braking), and layout offsets used for bogies and attachment points. Implements ISerializable to read/write itself to the game's binary prefab serialization format and IComponentData to be used in ECS contexts.


Fields

  • public TrackTypes m_TrackType
    Defines the track classification this train uses (e.g., standard gauge, monorail, etc.). Serialized as a byte.

  • public EnergyTypes m_EnergyType
    Defines the energy/power source of the train (e.g., diesel, electric). Serialized as a byte.

  • public TrainFlags m_TrainFlags
    Bitflags controlling optional train behaviors/features. Serialized as a byte when present in the serialized version. Presence depends on reader.context.version compared against Version.trainPrefabFlags.

  • public float m_MaxSpeed
    Maximum speed value for the train. Serialized as a float.

  • public float m_Acceleration
    Acceleration rate used by the train. Serialized as a float.

  • public float m_Braking
    Braking deceleration used by the train. Serialized as a float.

  • public float2 m_Turning
    Turning-related parameters stored in a float2 (Unity.Mathematics.float2). Serialized as a float2.

  • public float2 m_BogieOffsets
    Offsets for bogie placement (front/rear or left/right as intended by the prefab). Serialized as a float2.

  • public float2 m_AttachOffsets
    Offsets describing attachment points for cars/wagons. Serialized as a float2.

Properties

  • None (this struct exposes public fields and no explicit properties).

Constructors

  • public TrainData()
    Default value-type constructor provided by C#. All fields will have their default values unless explicitly initialized. Use object initializer syntax to populate before use or deserialization to populate from data.

Methods

  • public void Serialize<TWriter>(TWriter writer) where TWriter : IWriter
    Writes the TrainData fields to a writer in a specific order:
  • m_TrackType (byte)
  • m_EnergyType (byte)
  • m_TrainFlags (byte)
  • m_MaxSpeed (float)
  • m_Acceleration (float)
  • m_Braking (float)
  • m_Turning (float2)
  • m_BogieOffsets (float2)
  • m_AttachOffsets (float2)
    Used when saving prefab data or sending prefab state across the network. The exact binary layout must match counterpart Deserialize implementation.

  • public void Deserialize<TReader>(TReader reader) where TReader : IReader
    Reads TrainData fields from reader in the same order used during serialization. Notes:

  • Reads two initial bytes for track type and energy type, then conditionally reads train flags only if reader.context.version >= Version.trainPrefabFlags.
  • Then reads floats/float2s for speed/accel/braking/turning/bogie/attach offsets.
  • Finally casts the initial bytes into TrackTypes and EnergyTypes and assigns them to m_TrackType and m_EnergyType.
  • Be mindful of version checks: older serialized data may not include m_TrainFlags.

Usage Example

// Example: creating and serializing a TrainData instance
TrainData data = new TrainData {
    m_TrackType = TrackTypes.Standard,
    m_EnergyType = EnergyTypes.Electric,
    m_TrainFlags = TrainFlags.None,
    m_MaxSpeed = 80f,
    m_Acceleration = 2.5f,
    m_Braking = 3.0f,
    m_Turning = new float2(10f, 15f),
    m_BogieOffsets = new float2(1.2f, -1.2f),
    m_AttachOffsets = new float2(0.5f, -0.5f)
};

// Assuming 'writer' implements IWriter
data.Serialize(writer);

// Deserializing back (assuming 'reader' implements IReader)
TrainData loaded = new TrainData();
loaded.Deserialize(reader);

{{ Notes: - Unity.Mathematics.float2 is used for small 2D vector parameters; ensure Unity.Mathematics is referenced. - The struct is suitable for ECS (IComponentData) and may be attached to prefab entity data; populate fields before using in runtime systems. - Pay attention to serialization versioning: Version.trainPrefabFlags gate controls whether m_TrainFlags is present in the stream. }}