list of train stations

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: list of train stations

Post by daniel34 »

I don't think there is a specific function in the API capable of doing this, but you can iterate over all train stops:

Code: Select all

function get_trainstop_names(surface)
	trainstops = {}
	for _, trainstop in pairs(surface.find_entities_filtered{name="train-stop"}) do
		table.insert(trainstops, trainstop.backer_name)
	end
	return trainstops
end
The downside is that the code is very performance intensive, it takes a few ticks to run on my midsized map and the freeze is very noticeable. It also doesn't account for duplicate names, but you could add them as table keys first and then return a new list where the keys are converted to values.
Factorio probably has an internal list of current train stops, you could ask in the Modding interface requests subforum.

You could also maintain a list of all train stops by initially filling the list going over every train stop, then listening for on_built/on_mined/on_died events and changing the list accordingly. The only exception is the renaming of stops, but an event for this will be implemented in 0.15: Event: on_entity_renamed(entity)
quick links: log file | graphical issues | wiki
Post Reply

Return to “Implemented mod requests”