Repository navigation
Identity
Calls that identify the train. Only setName changes anything, and none of them need
a control lease.
The train's UUID as a string, the same string every event carries as its first parameter.
The id survives reloads, so you can save it and compare against it later.
Every assembled train has one, so it is never nil.
At 36 characters it is long, so cut it with :sub(1, 8) when
printing.
print(train.getId()) --> 8f14e45f-ceea-467a-9f1b-2c3d4e5f6a7b
print(train.getId():sub(1, 8)) --> 8f14e45fThis is how a computer watching several trains tells their events apart:
local mine = train.getId()
while true do
local event, id, station = os.pullEvent("train_arrived")
if id == mine then
print("my train is at " .. station)
end
endmy train is at Central
The display name, or an empty string if nobody named it.
Never nil. An unnamed train reads "", and an empty string is truthy in Lua, so
getName() or something never reaches the fallback. Test the string itself:
local name = train.getName()
local label = name ~= "" and name or ("Train " .. train.getId():sub(1, 8))
print(label)Express 01
Renames the whole consist, the same field the train screen edits.
Takes effect at once, no lease and no reassembly.
Every carriage behind the first gets named too, "<name> Carriage 1", "<name> Carriage 2" and
so on. getName() reads the first one back.
"" clears it, and getName() goes back to "".
Nil is not accepted. The argument has to be a string, so setName(nil) errors with
bad argument #1 (string expected, got nil). Pass "" instead.
train.setName("Express 01")
print(train.getName()) --> Express 01
train.setName("")
print(train.getName()) --> prints an empty lineHow many carriages are coupled into the consist, counting the locomotive.
Use it as the limit for every carriage loop. Indices run from 1 to this number, and anything outside that throws an error, so read it instead of guessing.
for i = 1, train.getCarriageCount() do
print("carriage " .. i .. ": " .. train.getCarriagePassengerCount(i) .. " aboard")
endcarriage 1: 0 aboard
carriage 2: 3 aboard
carriage 3: 1 aboard
Read it again after coupling. The number changes when carriages are added or dropped, so read it inside the loop that uses it instead of storing it once at the top of a long running program.
The length of the whole consist in blocks, from the nose of the first carriage to the end of the last, with the gaps between carriages counted. It is the same length the station stop uses, so it is the number to compare with a platform.
It changes when carriages are added or dropped, read it again after coupling.
For one carriage, use train.getCarriage(i).getLength(), see Carriages.
local length = train.getLength()
local platform = 60
print(string.format("%.1f blocks, %s", length, length <= platform and "fits the platform" or "too long"))42.5 blocks, fits the platform
The other two sizes of the consist, in blocks, for checking a tunnel, a bridge or a platform edge.
-
getWidth()is the side to side size, across the track. It is the widest carriage. -
getHeight()is the up and down size. It is the tallest carriage, measured from the lowest block to the highest, so the bogeys are included.
Both are box sizes, measured on the box that holds all of a carriage's blocks, so one roof vent or one step makes the whole carriage that wide or that tall. Round up when you compare them with a gap.
For one carriage, use c.getWidth() and c.getHeight().
print(string.format("%.0f wide, %.0f tall", train.getWidth(), train.getHeight()))
if train.getHeight() > 5 then print("will not clear a 5 block tunnel") end5 wide, 6 tall
will not clear a 5 block tunnel
Start here
Reading the train
- Identity
- Motion
- State
- Station
- Signals
- Track ahead
- Orientation
- World position
- Carriages
- Consist wide cargo
Driving the train
Schedules
Doors and body
Links
The whole railway
Reacting to changes
Programs and reference
Start here
Controlling the plate
Automatic modes
Reading the turntable
Reference