Detecting trains on the track

Place to get help with not working mods / modding interface.
Post Reply
Hanse00
Fast Inserter
Fast Inserter
Posts: 100
Joined: Mon Feb 25, 2013 6:07 pm
Contact:

Detecting trains on the track

Post 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 :)

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Detecting trains on the track

Post 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 :)
<I'm really not active any more so these may not be up to date>
~FreeER=Factorio Modding
- Factorio Wiki
- My Factorio Modding Guide
- Wiki Modding Guide
Feel free to pm me :)
Or drop into #factorio on irc.esper.net

Post Reply

Return to “Modding help”