Page 1 of 1

How to check if a station's connected rail segment is free?

Posted: Wed Jan 08, 2020 9:45 am
by Jorn86
I'm trying to check if a rail segment connected to a train stop is unoccupied, so I can safely spawn a train there.

So far, I've got:

Code: Select all

local station = -- some LuaEntity of type 'train-stop'
local rail = station.connected_rail
-- repeating this next step zero or more times doesn't seem to help
rail = rail.get_connected_rail {
    rail_direction = defines.rail_direction.back,
    rail_connection_direction = defines.rail_connection_direction.straight
}
local signal = rail.get_rail_segment_entity(defines.rail_direction.back, true)
Unfortunately, it just keeps returning the train stop. How do I get it to return the entrance signal, so I can check its state?

Re: How to check if a station's connected rail segment is free?

Posted: Wed Jan 08, 2020 1:17 pm
by Jorn86
I tried putting the signals a lot closer, that actually causes them to be detected. However, that doesn't explain why they aren't detected further out (i.e. if the block is bigger).

Any suggestions would be welcome.