Home

LuaTrain

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

Attributes

manual_mode

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

speed

Current speed.

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

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

max_backward_speed

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

weight

The weight of this train.

carriages

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

locomotives

Locomotives of the train.

cargo_wagons

The cargo carriages the train contains.

fluid_wagons

The fluid carriages the train contains.

schedule

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

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

This train's current state.

front_stock

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

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

station

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

has_path

If this train has a path.

path_end_rail

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

path_end_stop

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

id

The unique train ID.

passengers

The player passengers on the train

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

riding_state

The riding state of this train.

killed_players

The players killed by this train.

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

kill_count

The total number of kills by this train.

path

The path this train is using, if any.

signal

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

group

The group this train belongs to.

Setting the group will apply the schedule of the group to this train.

front_end

Front end of the train: Rail and direction on that rail where the train will go when moving forward

back_end

Back end of the train: Rail and direction on that rail where the train will go when moving backward

valid

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

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.

Methods

get_item_count

LuaTrain.get_item_count(item?: ItemFilter) -> uint32

Call As

train.get_item_count()

Call Convention

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

Parameters

Returns

get_contents

LuaTrain.get_contents() -> ItemWithQualityCounts

Call As

train.get_contents()

Call Convention

Get a mapping of the train's inventory.

Returns

remove_item

LuaTrain.remove_item(stack: ItemStackIdentification) -> uint32

Call As

train.remove_item(stack)

Call Convention

Remove some items from the train.

Parameters

Returns

insert

LuaTrain.insert(stack: ItemStackIdentification)

Call As

train.insert(stack)

Call Convention

Insert a stack into the train.

Parameters

clear_items_inside

LuaTrain.clear_items_inside()

Call As

train.clear_items_inside()

Call Convention

Clear all items in this train.

recalculate_path

LuaTrain.recalculate_path(force?: boolean) -> boolean

Call As

train.recalculate_path()

Call Convention

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

Parameters

Returns

get_fluid_count

LuaTrain.get_fluid_count(fluid?: string) -> double

Call As

train.get_fluid_count()

Call Convention

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

Parameters

Returns

get_fluid_contents

LuaTrain.get_fluid_contents() -> Dict<string, FluidAmount>

Call As

train.get_fluid_contents()

Call Convention

Gets a mapping of the train's fluid inventory.

Returns

remove_fluid

LuaTrain.remove_fluid(fluid: Fluid) -> double

Call As

train.remove_fluid(fluid)

Call Convention

Remove some fluid from the train.

Parameters

Returns

insert_fluid

LuaTrain.insert_fluid(fluid: Fluid) -> double

Call As

train.insert_fluid(fluid)

Call Convention

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

Parameters

Returns

clear_fluids_inside

LuaTrain.clear_fluids_inside()

Call As

train.clear_fluids_inside()

Call Convention

Clears all fluids in this train.

go_to_station

LuaTrain.go_to_station(index: uint32)

Call As

train.go_to_station(index)

Call Convention

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

Parameters

get_rails

LuaTrain.get_rails() -> Array<LuaEntity>

Call As

train.get_rails()

Call Convention

Gets all rails under the train.

Returns

get_rail_end

LuaTrain.get_rail_end(direction: defines.rail_direction) -> LuaRailEnd

Call As

train.get_rail_end(direction)

Call Convention

Gets a LuaRailEnd object pointing away from the train at specified end of the train

Parameters

Returns

get_schedule

LuaTrain.get_schedule() -> LuaSchedule

Call As

train.get_schedule()

Call Convention

Returns