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_train_id(train) | Find the ID of a LuaTrain instance. |
get_main_locomotive(train) | Get the main locomotive of a train. |
to_entity(train) | Creates an entity from a train that is compatible with the Entity.Entity module. |
set_data(train, data) | Associates the user data to a train. |
get_data(train) | Gets the user data that is associated with a train. |
register_events() | This needs to be called to register events for this 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.
If criteria.surface is not supplied, this function searches through all existing surfaces. If criteria.force is not supplied, this function searches through all existing forces. If criteria.state is not supplied, this function gets trains in any state.
Parameters:Trains.find_filtered({ surface = "nauvis", state = defines.train_state.wait_station })
Find the ID of a LuaTrain instance.
Parameters:
Get the main locomotive of a train.
Parameters:
Creates an entity from a train that is compatible with the Entity.Entity module.
Parameters:
Associates the user data to a train.
This is a helper around Entity.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.
Gets the user data that is associated with a train.
This is a helper around Entity.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.
This needs to be called to register events for this module
Returns:
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: