Repository navigation
Signals
How far the next signal is, and whether the train is being held by one.
Everything here is measured to the stop line, the spot a train parks at whatever color the signal shows, so all three calls measure to the same place.
Query range is 256 blocks. Past that you get nil, which means "nothing in range", not "clear forever".
A derailed train reads nil too, because there is no rail ahead of it to search along.
Signals only stop a schedule driven train. Under manual control the train runs through reds.
Blocks from the nose to the stop line of the next red ahead. Nil when there is no red inside 256 blocks.
A parked train searches both ways. With no direction of travel there is no "ahead", so it reports the nearer red on either side.
local gap = train.getGapToRed()
print(gap and string.format("red in %.1f blocks", gap) or "clear ahead")red in 42.5 blocks
The and and or pair is a nil check. Written out:
local gap = train.getGapToRed()
if gap then
print(string.format("red in %.1f blocks", gap))
else
print("clear ahead")
endThe same measurement, with a choice of which signals count. state is optional.
state |
Finds |
|---|---|
| left out | The nearest signal, whatever color it shows |
"red" |
The next red only, the same as getGapToRed()
|
Any other string throws an error. There is no third option.
Both return nil when nothing is in range.
local nearest = train.getDisToSignal()
local red = train.getDisToSignal("red")
if nearest and red and nearest < red then
print("a green one first")
enda green one first
The nearest signal ahead, whatever color, as a table. Nil when none is in range.
-
id, the signal's id, the same oneloconautics.getSignals()lists -
distance, blocks from the nose to the stop line -
state,"red","yellow","green"or"invalid" -
type,"block"or"chain" -
block, the id of the block it guards
It replaces the two step check of getDisToSignal() plus a color guess. Yellow only appears on chain
signals, see Network.
local sig = train.getNextSignal()
if sig and sig.state ~= "green" and sig.distance < 60 then
print(string.format("%s signal in %.0f blocks", sig.state, sig.distance))
endred signal in 42 blocks
True while the train is stopped and waiting for a red to clear.
Not the same as getGapToRed() returning a small number. A red 5 blocks ahead of a moving train
is not a hold. This only turns true once the train has stopped for it.
if train.isHeldAtRed() then
print("waiting on the signal")
endTo react without checking in a loop, use the events instead:
local _, id, gap = os.pullEvent("train_signal_red")
print(string.format("held at red, %.1f blocks short", gap))
os.pullEvent("train_signal_clear")
print("signal clear")Those two events only fire for schedule driven trains, the same as the hold itself.
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