Get rail adjacent to train stop

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
charlesrymal
Manual Inserter
Manual Inserter
Posts: 1
Joined: Sat Jan 27, 2018 5:13 pm
Contact:

Get rail adjacent to train stop

Post by charlesrymal »

Is there a way, if you have a train stop entity, to get the rail to which the train stop is attached? And vice versa?

And if you can get the rail, how could I figure out with direction to use with get_connected_rail to be forward relative to the train stop?

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7351
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Get rail adjacent to train stop

Post by bobingabout »

To find the rail next to a station, you'll probably have to resort to using a find in the area around the station, on the surface the station is on, then check to see if it is a rail.
http://lua-api.factorio.com/latest/LuaS ... ind_entity

That's how you detect what rails are connected to the rail that you find.
http://lua-api.factorio.com/latest/LuaE ... ected_rail

for which direction is forward, you can probably go off the direction that station is facing
http://lua-api.factorio.com/latest/LuaE ... .direction
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Get rail adjacent to train stop

Post by eradicator »

bobingabout wrote:for which direction is forward, you can probably go off the direction that station is facing
http://lua-api.factorio.com/latest/LuaE ... .direction
I was going to suggest using the stations direction. But i was kinda hoping someone would know a proper way to do this in the API. Going from the direciton of the station there can also be only exactly one possible position with a rail that is connected to the station, so not really any need to search the whole area once you determine the magic position offset.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7351
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Get rail adjacent to train stop

Post by bobingabout »

unfortunately, to get an entity at a location, you still need to search that location. I also linked to a point where you can see different search options, so you don't have to use the area one.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Get rail adjacent to train stop

Post by eradicator »

bobingabout wrote:unfortunately, to get an entity at a location, you still need to search that location. I also linked to a point where you can see different search options, so you don't have to use the area one.
Sure. But you also said "in the area around the station". So i just added that theoretically/logically there can/should be only one possible position. I.e. if the station is facing upwards, then the only connectable rail is on the left side of the station, probably at {station.position.x - 2, station.position.y}.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

Post Reply

Return to “Modding discussion”