Get locomotive position in train

Place to get help with not working mods / modding interface.
gheift
Fast Inserter
Fast Inserter
Posts: 188
Joined: Tue Mar 03, 2015 9:20 pm
Contact:

Get locomotive position in train

Post by gheift »

I want to get the positon of a locomotive in relation of the train. Let's assume there is a train with the following layout:

Code: Select all

<Foo] [cargo] [cargo] <Bar]
With the Lua/Train interface one gets both locomotives:

Code: Select all

{
    front_movers = {
        [1] = {backer_name = "Foo" --[[, ...]] },
        [2] = {backer_name = "Bar" --[[, ...]] },
   },
}
What I want is the information, that Foo is at position 1 and Bar is at position 4. Any chance to get this information?
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Get locomotive position in train

Post by prg »

You can iterate over train.carriages for that.

Code: Select all

for k, v in pairs(game.player.selected.train.carriages) do
    print("Position " .. k .. " contains a " .. v.name .. (v.type=="locomotive" and (" called " .. v.backer_name) or ""))
end

Code: Select all

Position 1 contains a diesel-locomotive called Blake Reinhart
Position 2 contains a cargo-wagon
Position 3 contains a cargo-wagon
Position 4 contains a cargo-wagon
Position 5 contains a diesel-locomotive called Rockstaricp420
Position 6 contains a diesel-locomotive called Hasselpoof
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
gheift
Fast Inserter
Fast Inserter
Posts: 188
Joined: Tue Mar 03, 2015 9:20 pm
Contact:

Re: Get locomotive position in train

Post by gheift »

Just saw it, thanks!
Post Reply

Return to “Modding help”