Switches and loaders
This page uses GameObject from Unity. The glossary says what each one is.
A track switch is a lever that sends a train down one of several branches of track. A wagon loader is a machine that puts goods in a wagon.
Trains switches and wagon loaders
Use Switches and Loaders to find the track switches and wagon loaders, then SetBranch and SetLoaderOn to work them.
public static class Trains
{
public static List<GameObject> Switches { get; }
public static List<GameObject> Loaders { get; }
public static int GetBranch(GameObject target);
public static bool SetBranch(GameObject target, int branch, bool force);
public static bool SetLoaderOn(GameObject target, bool on);
public static bool SetLoaderFreeToTake(GameObject target, bool freeToTake);
}
| Method | Returns | What it does |
|---|---|---|
GetBranch(target) | int | Which branch a switch points at. -1 when the object is not a switch. |
SetBranch(target, branch, force) | bool | Points a switch at another branch. true when the change was accepted. |
SetLoaderOn(target, on) | bool | Turns a wagon loader on or off. true when it ends up in that state. |
SetLoaderFreeToTake(target, freeToTake) | bool | Sets whether the player may take the loader’s goods without stealing. |
| Property | Type | What it means |
|---|---|---|
Switches | List<GameObject> | Every track switch in the scene you are in. |
Loaders | List<GameObject> | Every wagon loader in the scene you are in. |
Things to know
SwitchesandLoaderslist the objects that are switched on in the scene you are in. Pass one of them to the calls below. (They are theMinitrainSwitchandMinitrainWagonLoaderobjects found among the active objects of the active scene.)GetBranchtells you which branch a switch points at. The branch number only changes when the lever has finished moving, not when you callSetBranch. (It readsMinitrainSwitch.currentBranchIndex, which is updated when the animation ends (ChangeTrackCO).)SetBranchwithforceset tofalsefollows the game’s own rule: it won’t move a switch that has a train on it. If the change is accepted, it starts the lever and returnstrue. The game keeps waiting for the track to clear during the lever’s movement as well. (falsehere is the game’s occupancy check inChangeTrack,MinitrainSwitch: the switch’strackSegment.occupied, or, whenisReversedis set, the previous segment’soccupied. On success it callsChangeTrack(branch).)SetBranchwithforceset totruemoves the switch even with a train on it. The branch changes at once, and then the lever moves. (It callsChangeTrackForce(branch), which setscurrentBranchIndexat once and then animates.)SetLoaderOnturns a wagon loader on or off. It returnstruewhen the loader ends up in the state you asked for. (It callsMinitrainWagonLoader.Toggle(on)and comparesactive.)SetLoaderFreeToTakesets whether the player may take the goods the loader stocks without that counting as stealing. When it is on, each item the loader newly stocks is marked as allowed to take. It returnstruewhen the loader ends up with the value you asked for. (It callsSetOwnerPermissions(freeToTake), which setspermissionToTake. When it istrue, the loader tags each newly stocked item (owner ID above 1) withItemOwnerPermission { ownerPermission = true }.)
Limits
GetBranchthrows anArgumentNullExceptionfor anullobject.SetBranchreturnsfalsewhen a train is on the switch’s piece of track, or, for a reversed switch, on the piece before it.SetBranchreturnsfalsewhen the switch has no such branch, and throws anArgumentOutOfRangeExceptionfor a negative branch. It also returnsfalsewhen the object is not a switch, and while a save is loading or being written. (The number of branches is the length of the privateMinitrainSwitch.branchRotations, read through reflection.)SetBranch,SetLoaderOnandSetLoaderFreeToTakethrow anArgumentNullExceptionfor anullobject. They returnfalsewhile a save is loading or being written, andfalsewhen the object is not the right kind of object.