Jump to content

Automatic Train System


Recommended Posts

Posted
Client side :
addEvent("getTrain", true ) 
function getTrain () 
    local x, y, z = getElementPosition(getElementByID( "loco1" )) 
    local a, b, c = getElementPosition(getElementByID( "wagon1" )) 
    local g, h, i = getElementPosition(getElementByID( "pilot2" )) 
    local d, e, f = getElementPosition(getElementByID( "pilot1" )) 
    triggerServerEvent("setTrainPosition",getRootElement(),x,y,z,a,b,c,g,h,i,d,e,f) 
end 
addEventHandler("getTrain", getRootElement(), getTrain) 

Server side :

addEvent("setTrainPosition",true) 
function setTrain (x,y,z,a,b,c,g,h,i,d,e,f) 
    triggerClientEvent( "getTrain", getRootElement()) 
    setElementPosition(loco, x, y, z, false) 
    setElementPosition(locool, a, b, c, false) 
    setElementPosition(pilot1, d, e, f, false) 
    setElementPosition(pilot2, g, h, i, false) 
end 
addEventHandler("setTrainPosition",getRootElement(),setTrain)  

I don't know if that's what you wanted.

This may work with one player on the server, but most likely result with high ping or timeout.

if there will be more than one client on the server, events will multiply and crach the server, not to mention the data conflict, if somehow the coordinates on each client will be diferent

  • Replies 78
  • Created
  • Last Reply

Top Posters In This Topic

Posted
What are you guys doing? Just make a train serverside, make it move, on use setElementStreamable to ensure all clients have it loaded all the time. It isn't hard...

bother reading some pages before?streaming is bugged

Posted
I doubt that, I've done similar stuff before on 1.0.5 with no issues.

It has nothing to do with streaming, vehicles that are located more than 140 m from any player are not synced, even if it is still moving, the movment is lical, and the position for the server is not updated, when the vehicle get close enough to a player again, it vill jump to last synced position.

Posted
Client side :
addEvent("getTrain", true ) 
function getTrain () 
    local x, y, z = getElementPosition(getElementByID( "loco1" )) 
    local a, b, c = getElementPosition(getElementByID( "wagon1" )) 
    local g, h, i = getElementPosition(getElementByID( "pilot2" )) 
    local d, e, f = getElementPosition(getElementByID( "pilot1" )) 
    triggerServerEvent("setTrainPosition",getRootElement(),x,y,z,a,b,c,g,h,i,d,e,f) 
end 
addEventHandler("getTrain", getRootElement(), getTrain) 

Server side :

addEvent("setTrainPosition",true) 
function setTrain (x,y,z,a,b,c,g,h,i,d,e,f) 
    triggerClientEvent( "getTrain", getRootElement()) 
    setElementPosition(loco, x, y, z, false) 
    setElementPosition(locool, a, b, c, false) 
    setElementPosition(pilot1, d, e, f, false) 
    setElementPosition(pilot2, g, h, i, false) 
end 
addEventHandler("setTrainPosition",getRootElement(),setTrain)  

I don't know if that's what you wanted.

It don't work, the train can't move, and it's very laggy.

Posted

There's no need to sync the train so often. Just create a timer which executes the function like every 5 or 10 seconds, checks if the train has the syncer, and if it doesn't, find the nearest player, use triggerClientEvent to ask for train position, on the client side, get the position, use triggerServerEvent to send the position to the server and again, on the server side, use those coordinates in setElementPosition.

Posted

I don't think I will use that, because, it's to laggy.

I'm working on a system of marker, to put the player into the train. But only when it is here "if nextgare == number of the station "

Posted

There's an optional argument for triggerClientEvent. And to find the nearest player, you need to loop through all of them and calculate the distances.

  • 2 weeks later...
  • 2 weeks later...
Posted

Well, I've a new idea.

view my code :

It don't work.

Client Side :

addEvent("removeStreamableLocos", true ) 
function removeStreamableLocos( theElement ) 
    setElementStreamable( getElementByID( "loco1" ), false ) 
    setElementStreamable( getElementByID( "wagon1" ), false ) 
    setElementStreamable( getElementByID( "pilot1" ), false ) 
    setElementStreamable( getElementByID( "pilot2" ), false ) 
end 
addEventHandler("removeStreamableLocos", getRootElement(), removeStreamableLocos ) 
  
addEvent("getTrain", true ) 
function getTrain () 
    local x, y, z = getElementPosition(getElementByID( "loco1" )) 
    local a, b, c = getElementPosition(getElementByID( "wagon1" )) 
    local g, h, i = getElementPosition(getElementByID( "pilot2" )) 
    local d, e, f = getElementPosition(getElementByID( "pilot1" )) 
    triggerServerEvent("setTrainPosition",getRootElement(),x,y,z,a,b,c,g,h,i,d,e,f) 
end 
addEventHandler("getTrain", getRootElement(), getTrain) 

and Server Side :

function makeTrain(source) 
    loco = createVehicle(537,1725.9,-1954,16) 
    locool = createVehicle(538,1840,-1954,16) 
    setElementID( loco, "loco1" ) 
    setElementID( locool, "wagon1" ) 
    setTrainDerailable(loco, false) 
    setTrainDerailable(locool, false) 
    local pilot1 = createPed(241,1693.38,-1959,15.6) 
    local pilot2 = createPed(241,1711,-1959,15.6) 
    setElementID( pilot1, "pilot1" ) 
    setElementID( pilot2, "pilot2" ) 
    warpPedIntoVehicle (pilot1, loco) 
    warpPedIntoVehicle (pilot2, locool) 
    setTimer(setTrain, 5000, 0) 
    createBlipAttachedTo(loco, 42) 
    setTrainSpeed(loco, 0) 
    setTrainSpeed(locool,0.203) 
    setTimer(assemblage, 10000, 1) 
    outputChatBox("Train is here", source, 255, 255, 0) 
    removeStreamable( loco ) 
    setTimer(depart, 20000, 1) 
    nextgare = 0 
end 
addCommandHandler("traintest", makeTrain) 
  
 function assemblage () 
    nextgare = nextgare+1 
    setTrainSpeed(loco, 0) 
    setTrainSpeed(locool, 0) 
    setTimer(depart, 20000,1) 
end 
  
function depart () 
    nextgare = nextgare+1 
    setTrainSpeed(loco, 1) 
    setTrainSpeed(locool, 1) 
    if nextgare == 0 then 
    setTimer(assemblage, 88000, 1) 
    else 
    if nextgare == 2 then 
    setTimer(assemblage, 23000, 1) 
    else 
    if nextgare == 4 then 
    setTimer(assemblage, 93000, 1) 
    else 
    if nextgare == 6 then  
    setTimer(assemblage,102000, 1) 
    else 
    if nextgare == 8 then 
    setTimer(assemblage, 50000, 1) 
    else 
    if nextgare == 10 then 
    nextgare = 0 
    end 
    end 
    end 
    end 
    end 
    end 
end 
  
function speed () 
    setTrainSpeed(loco, 1) 
    setTrainSpeed(locool, 1) 
end 
  
      
function removeStreamable( theElement ) 
    triggerClientEvent( "removeStreamableLocos", getRootElement() ) 
end 
  
addEvent("setTrainPosition",true) 
function setTrain (x,y,z,a,b,c,g,h,i,d,e,f) 
    triggerClientEvent( "getTrain", getRootElement()) 
    setElementPosition(loco, x, y, z, false) 
    setElementPosition(locool, a, b, c, false) 
    setElementPosition(pilot1, d, e, f, false) 
    setElementPosition(pilot2, g, h, i, false)  
end 
addEventHandler("setTrainPosition",getRootElement(),setTrain) 

Posted

Is it "double posting" to post a message a few days later from the last message?

And now, the script problems: server-side function setTrain and client-side function getTrain trigger events, calling each other. That means the train position will be refreshed very fast, as the server asks for the new position right after it gets the previous. And the number of such cycles is increased every 5 seconds. That will make the game unplayable very soon.

I will try to explain better what you need to do:

1. Make the server-side timer do NOTHING more than just triggering the client-side event (asking for position).

2. When server gets the position, the function called by setTrainPosition event should do NOTHING more than setting element positions. No event triggering.

Posted
I nedd help ! The train don't stop on the marker, when no body is near it.

That is one bug you cannot fix, in MTA the train will only work with 'setTrainSpeed' if there is any player in syncing range near the train. I made a train resource myself and it was working fabulous until I noticed that as soon as I go far away from it, the train stops moving.

  • MTA Team
Posted
YOU TRIPLE POSTED.

Dont double post use the edit button.

Please refrain from backseat moderating

"OOPS, I just moderated xD"

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