Game.Prefabs.TrainEnginePrefab
Assembly:
Assembly-CSharp
Namespace:
Game.Prefabs
Type:
class
Base:
TrainPrefab
Summary:
TrainEnginePrefab is a prefab MonoBehaviour used by the entity conversion/prefab system to configure train engine entities. It exposes inspector-editable fields that control how many engines a train may have, how many carriages each engine can pull, and an optional tender (a TrainCarPrefab) that is attached ahead of the carriage group. During entity conversion (LateInitialize) it writes a TrainEngineData component and populates the VehicleCarriageElement dynamic buffer for the resulting entity.
Fields
-
public int m_MinEngineCount
Minimum number of engines allowed for this train. Default from code is 2. This value is written into the TrainEngineData component during LateInitialize. -
public int m_MaxEngineCount
Maximum number of engines allowed for this train. Default from code is 2. This value, together with m_MinEngineCount, defines the engine count range stored in TrainEngineData. -
public int m_MinCarriagesPerEngine
Minimum number of carriages that each engine should pull. Default from code is 5. During LateInitialize a VehicleCarriageElement is added using this value. -
public int m_MaxCarriagesPerEngine
Maximum number of carriages per engine. Default from code is 5. Used together with m_MinCarriagesPerEngine when adding the carriage buffer element. -
public TrainCarPrefab m_Tender
Optional reference to a TrainCarPrefab representing a tender car. If set, LateInitialize resolves the tender prefab to an entity and adds a VehicleCarriageElement for it (with fixed counts 1..1 and a default direction).
Properties
- None declared on this class.
This class exposes state via public fields intended to be set in the Unity inspector. The relevant runtime state is written to entity components during LateInitialize (TrainEngineData and VehicleCarriageElement buffer).
Constructors
public TrainEnginePrefab()
Default parameterless constructor (implicit). The public integer fields default to the values initialized in the class source (m_MinEngineCount = 2, m_MaxEngineCount = 2, m_MinCarriagesPerEngine = 5, m_MaxCarriagesPerEngine = 5). The m_Tender field defaults to null.
Methods
public override void GetPrefabComponents(HashSet<ComponentType> components)
Adds the component types this prefab will require on the converted entity. Calls base.GetPrefabComponents(components) and then adds:- TrainEngineData (ReadWrite)
-
VehicleCarriageElement (ReadWrite) This informs the prefab conversion/creation system which components and buffers will be present.
-
public override void LateInitialize(EntityManager entityManager, Entity entity)
Performs final initialization on the converted entity after the base LateInitialize. Behavior: - Writes a TrainEngineData component to the entity using m_MinEngineCount and m_MaxEngineCount.
- Retrieves the entity's DynamicBuffer
. - If m_Tender is non-null, resolves the tender prefab to an entity via PrefabSystem.GetEntity(m_Tender) and adds a VehicleCarriageElement for the tender with counts 1..1 and VehicleCarriageDirection.Default.
- Adds another VehicleCarriageElement representing the engine's carriage group using Entity.Null and the min/max carriage counts, with VehicleCarriageDirection.Random.
Notes and side-effects:
- PrefabSystem is queried via entityManager.World.GetExistingSystemManaged
Usage Example
// Example: adjust prefab fields in code (commonly set in the inspector)
TrainEnginePrefab trainPrefab = /* obtain TrainEnginePrefab from a GameObject */;
trainPrefab.m_MinEngineCount = 3;
trainPrefab.m_MaxEngineCount = 4;
trainPrefab.m_MinCarriagesPerEngine = 4;
trainPrefab.m_MaxCarriagesPerEngine = 6;
trainPrefab.m_Tender = tenderTrainCarPrefab; // assign a TrainCarPrefab reference
// During conversion, LateInitialize will be called by the prefab system and:
// - TrainEngineData(min=3, max=4) will be set on the entity
// - VehicleCarriageElement entries for the tender (if set) and carriage group will be added to the entity's buffer
Additional notes: - This prefab is used as part of the Entities-based prefab system — it does not itself spawn trains at runtime, but configures entity data used by the train spawning/management systems. - Ensure the referenced TrainCarPrefab (m_Tender) is also converted to a prefab entity so PrefabSystem.GetEntity can resolve it during LateInitialize.