Tools for working with trains.
When this module is loaded into a mod, it automatically registers a number of new events in order to keep track of the trains as their locomotives and wagons are moved around.
To handle the events, you should use the Event module.
on_train_id_changed | This event fires when a train's ID changes. |
find_filtered (criteria) | Given a search criteria, search for trains that match the criteria. |
get_data (train) | Gets the user data that is associated with a train. |
get_main_locomotive (train) | Get the main locomotive of a train. |
get_train_id (train) | Find the ID of a LuaTrain instance. |
set_data (train, data) | Associates the user data to a train. |
to_entity (train) | Creates an entity from a train that is compatible with the Entity module. |
criteria | This table should be passed into find_filtered to find trains that match the criteria. |
train_details | find_filtered returns an array with one or more of this table based on the search criteria. |
train_entity | to_entity returns this table. |
This event fires when a train's ID changes.
The train ID is a property of the main locomotive, which means that when locomotives are attached or detached from their wagons or from other locomotives, the ID of the train changes.
For example: A train with a front and rear locomotives will get its ID from the front locomotive. If the front locomotive gets disconnected, the rear locomotive becomes the main one and the train's ID changes.
Event Parameters:Event.register(Trains.on_train_id_changed, my_handler)
Given a search criteria, search for trains that match the criteria.
Trains.find_filtered({ surface = "nauvis", state = defines.train_state.wait_station })
Gets the user data that is associated with a train.
This is a helper around Entity.get_data.
The user data is stored in the global object and it persists between loads.
Parameters:The user data will be removed from a train when the train becomes invalid.
Get the main locomotive of a train.
Parameters:
Find the ID of a LuaTrain instance.
Parameters:
Associates the user data to a train.
This is a helper around Entity.set_data.
The user data will be stored in the global object and it will persist between loads.
Parameters:The user data will be removed from a train when the train becomes invalid.
Creates an entity from a train that is compatible with the Entity module.
Parameters:
This table should be passed into find_filtered to find trains that match the criteria.
Fields:
find_filtered returns an array with one or more of this table based on the search criteria.
Fields:
to_entity returns this table.
Fields: