Attach cargo wagon to exactly 1 train

Place to get help with not working mods / modding interface.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Attach cargo wagon to exactly 1 train

Post 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 1105 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 1105 times
quyxkh
Smart Inserter
Smart Inserter
Posts: 1032
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Attach cargo wagon to exactly 1 train

Post 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.
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2920
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Attach cargo wagon to exactly 1 train

Post 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.
Post Reply

Return to “Modding help”