Jump to content

[HELP] Attach carriages to train


Martin54

Recommended Posts

Hi community!

So I have created train by adding carriages and locomotive near eachothers but how do I attach them to eachothers and make them follow eachother, how can I be sure that they stay in the same track? Is there any function for this, attachTrailerToVehicle wont work. Please help.

Link to comment
  • Moderators

There is no function for attach trailers to the train (read it would be done soon by devs a long time ago, so idk).

The simpliest workaround I can imagine, is to logically attach them (using setElementParent for example) and then use getElementVelocity on the parent to set it to the child (the trailer of the locomotive, or the trailer of another trailer). Be sure to do that on the server side if you want it to be synced with everyone.

2 problems:

1 - it will need a lot of server resources (I mean the cpu) because the speed of the trailer needs to be updated all the time and as often as possible.

2 - you will surelly get the trailers getting at the wrong place while driving it. I mean that the children can go away from their parent slowly and you will be able to see it at some point.

I also have another idea but harder than the first one. It will also use setElementVelocity but this time it will be based on the distance between the two attached parts (the trailer with the locomotive, or the trailer with another trailer).

:?: "It doesn't look hard"

It is, because you can't just use getElementPosition on the trailers/locomotive because it will return the world position of the center of the trailer/locomotive. So you need to do some math (pretty much like in this function) to get the position of the back side of the parent and the front side of the child and only then use getDistanceBetweenPoints3D using the 2 calculated positions.

This way, it will totally resolve the 2nd problem from the first idea, and partially resolve the 1st one (as it will still need to do some updates about the speed of each trailer but not as often as the 1st idea. I'm sure I can implement this 2nd idea using a setTimer of hmmmm let's say 800ms. I know it's still pretty fast, but the 1st idea will at least need a setTimer of 50 or 100 ms to looks good.

I'll probably do a resource for that someday.

(Remember I did that a long time ago, but the code is on the client-side and using invisible peds to drive the trailers and they are accelerating and braking at the same time as the visible ped in the locomotive. http://www.dailymotion.com/video/xcegb0 ... videogames :lol:)

Link to comment

I have only loop so far but I want the train carriages to follow eachothers like a train in single player.

  
local, loco = createVehicle ( 537, 1467, -1532, 10 ) 
for k=1, 5 do 
local carriage = createVehicle ( 590, 1467+(k*18), -1532, 10 ) 
attachTrailerToVehicle( carriage, loco ) 
end 
  

Link to comment

@Citizen I remember that video, that's actually what's inspired me to make my train script which also has all the features you need to attach a train, see the videos:

https://www.youtube.com/watch?v=ap6h331MfRo

https://www.youtube.com/watch?v=cxEMsUCEsUI

And here's the script:

https://community.multitheftauto.com/index.php?p=resources&s=details&id=8060 Automatic trains

https://community.multitheftauto.com/index.php?p=resources&s=details&id=8338 Vehicle system with support for trains with multiple locomotives attached.

This includes a sync algorithm, creation of trains and sync of tracks as well as distance between carriages, the first video demonstrates a script similar to what you saw in Citizen's video but with a player that drives the train instead. Since the carriages is spawned in the same location they will sync the distance to eachothers as soon a player is nearby.

See this: https://forum.multitheftauto.com/viewtopic.php?f=108&t=72085 for more details on how you can use this feature and good luck.

Link to comment
  • Moderators

Try this just to be sure the function is still not working with trains:

local loco = createVehicle ( 537, 1467, -1532, 10 ) 
local parent = loco 
for k=1, 5 do 
    local carriage = createVehicle ( 590, 1467+(k*18), -1532, 10 ) 
    attachTrailerToVehicle( carriage, parent ) 
    parent = carriage 
end 

Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...