Trains on the track
This page uses Vector3 from Unity. The glossary says what each one is.
Each TrackTrainInfo is a snapshot of one train or wagon on the track. All() gives you every one of them.
Trains lists the trains on the track
Use All() to list every train and wagon on the track.
public static class Trains
{
public static List<TrackTrainInfo> All();
}
| Method | Returns | What it does |
|---|---|---|
All() | List<TrackTrainInfo> | Every train on the track. |
TrackTrainInfo a snapshot of one train or wagon
All() gives you one of these for each train or wagon on the track. FrontCoupledTo and BackCoupledTo name what is joined at each end. It is a snapshot, so ask again for fresh values. (It reads the game’s TrackTrain.)
public sealed class TrackTrainInfo
{
public string Id { get; }
public float Speed { get; }
public Vector3 Position { get; }
public string FrontCoupledTo { get; }
public string BackCoupledTo { get; }
public string WagonType { get; }
public string CargoGuid { get; }
}
| Property | Type | What it means |
|---|---|---|
Id | string | The train’s GUID. It is the same id that Train actions uses. |
Speed | float | Its speed. Negative when it is reversing. |
Position | Vector3 | Where it is, in world coordinates. |
FrontCoupledTo | string | The id of the train joined to its front. null when nothing is. |
BackCoupledTo | string | The id of the train joined to its back. null when nothing is. |
WagonType | string | The kind of wagon, as text. "None" when the train has no wagon data. |
CargoGuid | string | The id of the wagon’s cargo container. null when there is none. |
Things to know
All()gives every train the game has on the track. It gives an empty list when the game’s train controller is not loaded yet. (It reads the game’s own registry,TrainController.instance.TrackTrains, which is keyed by GUID.)FrontCoupledToandBackCoupledToare the ids of the trains joined to each end. An end with nothing joined givesnull. (A coupler’s partner isTrainCoupler.coupledTo, and its train is that coupler’sparent.)WagonTypeis one ofNone,Orewagon,Flatbed,TankerorPassanger. The game spells the last one “Passanger”, and the framework passes the name through unchanged, so match that spelling. (It is the name ofMinitrainWagonData.WagonType. The wagon data is found withGetComponentInChildrenon the train, inactive objects included, becauseMinitrainWagonData.Awakefinds its train withGetComponentInParent<TrackTrain>.)CargoGuidnames the wagon’s cargo container. Pass it toContainers.FindByGuid(see Inventory) to get the container. A wagon with no storage givesnulltoo.
Limits
CargoGuidcan benulleven for a wagon that carries cargo, because the game only sets that id while it runs, in some places. (It isconnectedStorage.GUID. The game does not serialize that field. It is set at run time where the game assigns it (for exampleInstantiatedPrefabRef, asguid + "storage"), so it can be empty, and an empty GUID is reported asnull.connectedStorageis filled inMinitrainWagonData.AwakefromGetComponentInChildren<Storage>.)