Factorio.com|Forums|Wiki|Mod Portal|API Docs

# Factorio Runtime Docs Version 2.0.77

# LuaTrainManager - Runtime Docs

## Class LuaTrainManager

The train manager manages all the train in the game. LuaTrainManager allows to perform some direct queries to the train manager.

There is always exactly one train manager instance in a game, it can be obtained from LuaGameScript::train_manager. This object is always valid and is equal to any other instance of LuaTrainManager from this game.

## Members

get_train_by_id(train_id) | → LuaTrain? | Searches for a train with given ID.
Searches for a train with given ID.
get_trains(filter) | → array[LuaTrain] | Gets all trains that pass given filter
Gets all trains that pass given filter
get_train_stops(filter) | → array[LuaEntity] | Gets all train stops that pass given filter
Gets all train stops that pass given filter
request_train_path{type?=…, train?=…, goals=…, return_path?=…, starts?=…, search_direction?=…, in_chain_signal_section?=…, steps_limit?=…, shortest_path?=…} | → TrainPathFinderOneGoalResult or TrainPathAllGoalsResult | Direct access to train pathfinder. [...]
Direct access to train pathfinder. [...]
valid | :: R boolean | Is this object valid? [...]
Is this object valid? [...]
object_name | :: R string | The class name of this object. [...]
The class name of this object. [...]

## Methods

### get_train_by_id(train_id) → LuaTrain?

Searches for a train with given ID.

#### Parameters

train_id | :: uint32 | Train ID to search
Train ID to search

#### Return values

→ LuaTrain? | Train if found

### get_trains(filter) → array[LuaTrain]

Gets all trains that pass given filter

#### Parameters

filter | :: TrainFilter | Filters the train must pass in order to be returned here
Filters the train must pass in order to be returned here

### get_train_stops(filter) → array[LuaEntity]

Gets all train stops that pass given filter

#### Parameters

filter | :: TrainStopFilter | Filters the train stop must pass in order to be returned here
Filters the train stop must pass in order to be returned here

### request_train_path{type?=…, train?=…, goals=…, return_path?=…, starts?=…, search_direction?=…, in_chain_signal_section?=…, steps_limit?=…, shortest_path?=…} → TrainPathFinderOneGoalResult or TrainPathAllGoalsResult

Direct access to train pathfinder. Allows to search rail paths or querying which stops are accessible

#### Parameters

Table with the following fields:

type | :: TrainPathRequestType? | Request type. Determines the return type of the method. Defaults to `"path"`.
Request type. Determines the return type of the method. Defaults to `"path"`.
train | :: LuaTrain? | Mandatory if `starts` is not provided, optional otherwise. Selects a context for the pathfinder to decide which train to exclude from penalties and which signals are considered possible to reacquire. If `starts` is not provided, then it is also used to collect front and back ends for the search
Mandatory if `starts` is not provided, optional otherwise. Selects a context for the pathfinder to decide which train to exclude from penalties and which signals are considered possible to reacquire. If `starts` is not provided, then it is also used to collect front and back ends for the search
goals | :: array[TrainPathFinderGoal] |
return_path | :: boolean? | Only relevant if request type is `"path"`. Returning a full path is expensive due to multiple LuaEntity created. In order for path to be returned, true must be provided here. Defaults to false in which case a path will not be provided.
Only relevant if request type is `"path"`. Returning a full path is expensive due to multiple LuaEntity created. In order for path to be returned, true must be provided here. Defaults to false in which case a path will not be provided.
starts | :: array[RailEndStart]? | Manually provided starting positions.
Manually provided starting positions.
search_direction | :: `"respect-movement-direction"` or `"any-direction-with-locomotives"`? | Only relevant if `starts` was not provided in which case 2 starts (front and back) are deduced from the train. Selects which train ends should be considered as starts. Defaults to `"any-direction-with-locomotives"`.
Only relevant if `starts` was not provided in which case 2 starts (front and back) are deduced from the train. Selects which train ends should be considered as starts. Defaults to `"any-direction-with-locomotives"`.
in_chain_signal_section | :: boolean? | Defaults to `false`. If set to true, pathfinder will not return a path that cannot have its start immediately reserved. A path that cannot have its start immediately reserved could cause a train to stop inside of an intersection.
Defaults to `false`. If set to true, pathfinder will not return a path that cannot have its start immediately reserved. A path that cannot have its start immediately reserved could cause a train to stop inside of an intersection.
steps_limit | :: uint32? | Maximum amount of steps pathfinder is allowed to perform.
Maximum amount of steps pathfinder is allowed to perform.
shortest_path | :: boolean? | Defaults to `false`. If set to true, only length of rails is added to penalties causing search to look for shortest path (not smallest penalty)
Defaults to `false`. If set to true, only length of rails is added to penalties causing search to look for shortest path (not smallest penalty)

#### Return values

→ TrainPathFinderOneGoalResult or TrainPathAllGoalsResult | The type of the returned value depends on `type`.

## Attributes

### valid :: Read boolean

Is this object valid? This Lua object holds a reference to an object within the game engine. It is possible that the game-engine object is removed whilst a mod still holds the corresponding Lua object. If that happens, the object becomes invalid, i.e. this attribute will be `false`. Mods are advised to check for object validity if any change to the game state might have occurred between the creation of the Lua object and its access.

### object_name :: Read string

The class name of this object. Available even when `valid` is false. For LuaStruct objects it may also be suffixed with a dotted path to a member of the struct.

Class used in

- LuaGameScript::train_manager