Page 1 of 1

Attach cargo wagon to exactly 1 train

Posted: Tue Jan 16, 2018 12:57 am
by DaveMcW
I created 2 trains using this command:

Code: Select all

/c game.player.surface.create_entity{name="locomotive",position={0,1},force=game.player.force}
/c game.player.surface.create_entity{name="locomotive",position={14,1},force=game.player.force}
1-0-1.jpg
1-0-1.jpg (94.63 KiB) Viewed 1104 times
Now I want to attach a cargo wagon to ONLY the first train.


I tried this command, but it attaches to both.

Code: Select all

/c game.player.surface.create_entity{name="cargo-wagon",position={7,1},force=game.player.force}
1-1-1.jpg
1-1-1.jpg (110.59 KiB) Viewed 1104 times

Re: Attach cargo wagon to exactly 1 train

Posted: Tue Jan 16, 2018 3:17 am
by quyxkh
Since the autoconnect happens on placement I don't see how to stop it, but calling `disconnect_rolling_stock(defines.rail_direction.back)` on the create_entity result

Code: Select all

/c _G.p=game.player _G.e=game.player.selected _G pos=e.position
/c p.surface.create_entity{name='cargo-wagon',position={pos.x+7,pos.y},force=p.force}.disconnect_rolling_stock(defines.rail_direction.front)
works, I just tried it.. Figuring out which way's forward looks like the hard part, I'm guessing checking train.carriages to see the relative position of the frontmost.

Re: Attach cargo wagon to exactly 1 train

Posted: Sat Jan 20, 2018 8:28 am
by Optera
Figuring out which way is forward for a train is indeed a bit tricky.

front_stock and back_stock are deceptive, they have nothing to do with the train driving direction. From limited testing those seems to be set when trains are created. I didn't test what triggers front and back in detail though.

In your example you can probably use train.locomotives.front_movers and train.locomotives.back_movers to find which locomotive is which.
A more robust way, I'm also using in LTN, is to compare the distance to the train stop for front_stock and back_stock.