Page 1 of 1

Get locomotive position in train

Posted: Mon Oct 26, 2015 11:10 am
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?

Re: Get locomotive position in train

Posted: Mon Oct 26, 2015 11:27 am
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

Re: Get locomotive position in train

Posted: Mon Oct 26, 2015 11:46 am
by gheift
Just saw it, thanks!