Page 1 of 1

Detecting trains on the track

Posted: Fri Feb 06, 2015 12:06 pm
by Hanse00
Hey there everyone!

I've decided to try my hands at modding, and wanted to make a railway crossing, which flashes it's lights when a train is approaching, so I don't die.

An obvious part of the process is detecting an inbound train, to do this, I've been trying to find out how the signals work, however I can't seem to find the code for them.
The signal entity itself just defines that it's type is "rail-signal", but I haven't been able to find anything defining that type anywhere.

If any of you lovely people would be able to point in in the right direction, that'd be awesome :)

Re: Detecting trains on the track

Posted: Fri Feb 06, 2015 5:59 pm
by FreeER
Hanse00 wrote:The signal entity itself just defines that it's type is "rail-signal", but I haven't been able to find anything defining that type anywhere.
All actual types are in the C++ code, so that would explain why you can't find anything.

Perhaps the best option here would be using findentitiesfiltered with a bounding box around the crossing

Code: Select all

local crossing = {position = {x=3200, y=1234}, size = {x=50, 20}}
local train = game.findentitiesfiltered{type="locomotive", area={
  {crossing.position.x - crossing.size.x, crossing.position.y - crossing.size.y},
  {crossing.position.x + crossing.size.x, crossing.position.y + crossing.size.y}
}

if #train then -- findentitiesfiltered returns a table, if it's not empty
  -- do code for flashing lights
end
local crossing would probably come from a loop over a table that contains multiple railroad crossings, but I kept it simpler here so the concept is more obvious. Crossing.size could be hardcoded into the findentitiesfiltered function call instead but I wanted to leave the option of different 'sized' crossings open :)