Factorio API Docs

1.1.62 <>

Class LuaTrain

A train. Trains are a sequence of connected rolling stocks -- locomotives and wagons.

uint

Get the amount of a particular item stored in the train.


→ dictionary[stringuint]

Get a mapping of the train's inventory.


remove_item(stack)
uint

Remove some items from the train.


insert(stack)

Insert a stack into the train.


Clear all items in this train.


boolean

Checks if the path is invalid and tries to re-path if it isn't.


double

Get the amount of a particular fluid stored in the train.


→ dictionary[stringdouble]

Gets a mapping of the train's fluid inventory.


remove_fluid(fluid)
double

Remove some fluid from the train.


insert_fluid(fluid)
double

Inserts the given fluid into the first available location in this train.


Clears all fluids in this train.


go_to_station(index)

Go to the station specified by the index in the train's schedule.


→ array[LuaEntity]

Gets all rails under the train.


help()
string

All methods and properties that this object supports.


:: boolean
[RW]

When true, the train is explicitly controlled by the player or script.


:: double
[RW]

Current speed.


:: double
[R]

Current max speed when moving forward, depends on locomotive prototype and fuel.


:: double
[R]

Current max speed when moving backwards, depends on locomotive prototype and fuel.


:: double
[R]

The weight of this train.


:: array[LuaEntity]
[R]

The rolling stocks this train is composed of, with the numbering starting at the front of the train.


:: dictionary[string → array[LuaEntity]]
[R]

Arrays of locomotives.


:: array[LuaEntity]
[R]

The cargo carriages the train contains.


:: array[LuaEntity]
[R]

The fluid carriages the train contains.


[RW]

This train's current schedule, if any.


[R]

This train's current state.


:: LuaEntity?
[R]

The rail at the front end of the train, if any.


:: LuaEntity?
[R]

The rail at the back end of the train, if any.




:: LuaEntity?
[R]

The front stock of this train, if any.


:: LuaEntity?
[R]

The back stock of this train, if any.


:: LuaEntity?
[R]

The train stop this train is stopped at, if any.


:: boolean
[R]

If this train has a path.


:: LuaEntity?
[R]

The destination rail this train is currently pathing to, if any.


:: LuaEntity?
[R]

The destination train stop this train is currently pathing to, if any.


id
:: uint
[R]

The unique train ID.


:: array[LuaPlayer]
[R]

The player passengers on the train


[R]

The riding state of this train.


:: dictionary[uintuint]
[R]

The players killed by this train.


:: uint
[R]

The total number of kills by this train.


[R]

The path this train is using, if any.


:: LuaEntity?
[R]

The signal this train is arriving or waiting at, if any.


:: boolean
[R]

Is this object valid?


:: string
[R]

The class name of this object.

Methods

get_item_count(item) → uint

Get the amount of a particular item stored in the train.

Parameters

item
:: string?

Item name to count. If not given, counts all items.


get_contents() → dictionary[stringuint]

Get a mapping of the train's inventory.

Return values

:: dictionary[stringuint]

The counts, indexed by item names.


remove_item(stack) → uint

Remove some items from the train.

Parameters

stack

The amount and type of items to remove

Return values

:: uint

Number of items actually removed.


insert(stack)

Insert a stack into the train.

Parameters


clear_items_inside()

Clear all items in this train.


recalculate_path(force) → boolean

Checks if the path is invalid and tries to re-path if it isn't.

Parameters

force
:: boolean?

Forces the train to re-path regardless of the current path being valid or not.

Return values

:: boolean

If the train has a path after the repath attempt.


get_fluid_count(fluid) → double

Get the amount of a particular fluid stored in the train.

Parameters

fluid
:: string?

Fluid name to count. If not given, counts all fluids.


get_fluid_contents() → dictionary[stringdouble]

Gets a mapping of the train's fluid inventory.

Return values

:: dictionary[stringdouble]

The counts, indexed by fluid names.


remove_fluid(fluid) → double

Remove some fluid from the train.

Parameters

fluid
:: Fluid

Return values

:: double

The amount of fluid actually removed.


insert_fluid(fluid) → double

Inserts the given fluid into the first available location in this train.

Parameters

fluid
:: Fluid

Return values

:: double

The amount inserted.


clear_fluids_inside()

Clears all fluids in this train.


go_to_station(index)

Go to the station specified by the index in the train's schedule.

Parameters

index
:: uint

get_rails() → array[LuaEntity]

Gets all rails under the train.


help() → string

All methods and properties that this object supports.

Attributes

manual_mode :: boolean [Read/Write]

When true, the train is explicitly controlled by the player or script. When false, the train moves autonomously according to its schedule.


speed :: double [Read/Write]

Current speed.

Note

Changing the speed of the train is potentially an unsafe operation because train uses the speed for its internal calculations of break distances, etc.


max_forward_speed :: double [Read]

Current max speed when moving forward, depends on locomotive prototype and fuel.


max_backward_speed :: double [Read]

Current max speed when moving backwards, depends on locomotive prototype and fuel.


weight :: double [Read]

The weight of this train.


carriages :: array[LuaEntity] [Read]

The rolling stocks this train is composed of, with the numbering starting at the front of the train.


locomotives :: dictionary[string → array[LuaEntity]] [Read]

Arrays of locomotives. The result is two arrays, indexed by "front_movers" and "back_movers" containing the locomotives. E.g. {front_movers={loco1, loco2}, back_movers={loco3}}.


cargo_wagons :: array[LuaEntity] [Read]

The cargo carriages the train contains.


fluid_wagons :: array[LuaEntity] [Read]

The fluid carriages the train contains.


schedule :: TrainSchedule? [Read/Write]

This train's current schedule, if any. Set to nil to clear.

Note

The schedule can't be changed by modifying the returned table. Instead, changes must be made by assigning a new table to this attribute.


state :: defines.train_state [Read]

This train's current state.


front_rail :: LuaEntity? [Read]

The rail at the front end of the train, if any.


back_rail :: LuaEntity? [Read]

The rail at the back end of the train, if any.


rail_direction_from_front_rail :: defines.rail_direction [Read]


rail_direction_from_back_rail :: defines.rail_direction [Read]


front_stock :: LuaEntity? [Read]

The front stock of this train, if any. The front of the train is in the direction that a majority of locomotives are pointing in. If it's a tie, the North and West directions take precedence.


back_stock :: LuaEntity? [Read]

The back stock of this train, if any. The back of the train is at the opposite end of the front.


station :: LuaEntity? [Read]

The train stop this train is stopped at, if any.


has_path :: boolean [Read]

If this train has a path.


path_end_rail :: LuaEntity? [Read]

The destination rail this train is currently pathing to, if any.


path_end_stop :: LuaEntity? [Read]

The destination train stop this train is currently pathing to, if any.


id :: uint [Read]

The unique train ID.


passengers :: array[LuaPlayer] [Read]

The player passengers on the train

Note

This does not index using player index. See LuaPlayer::index on each player instance for the player index.


riding_state :: RidingState [Read]

The riding state of this train.


killed_players :: dictionary[uintuint] [Read]

The players killed by this train.

The keys are the player indices, the values are how often this train killed that player.


kill_count :: uint [Read]

The total number of kills by this train.


path :: LuaRailPath? [Read]

The path this train is using, if any.


signal :: LuaEntity? [Read]

The signal this train is arriving or waiting at, if any.


valid :: boolean [Read]

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 :: string [Read]

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.

|<

Classes

Events

Concepts

Defines

Builtin types

>|