Jump to content

Automatic Train System


Recommended Posts

  • Replies 78
  • Created
  • Last Reply

Top Posters In This Topic

well, I've an error when i modify "getrootelement"

function makeTrain(source) 
    loco = createVehicle(537,1725.9,-1954,16) 
    locool = createVehicle(538,1840,-1954,16) 
    setTrainDerailable(loco, false) 
    setTrainDerailable(locool, false) 
    removeStreamable( loco ) 
    removeStreamable( locool ) 
    local pilot1 = createPed(241,1693.38,-1959,15.6) 
    local pilot2 = createPed(241,1711,-1959,15.6) 
    warpPedIntoVehicle (pilot1, loco) 
    warpPedIntoVehicle (pilot2, locool) 
    removeStreamable( pilot1 ) 
    removeStreamable( pilot2 ) 
    createBlipAttachedTo(loco, 42) 
    setTrainSpeed(loco, 0) 
    setTrainSpeed(locool,0.203) 
    outputChatBox("Train is here", source, 255, 255, 0) 
    removeStreamable(markerGare2) 
    setTimer(depart, 20000, 1) 
end 
addCommandHandler("traintest", makeTrain) 
  
local markerGare2 = createMarker(787, -1341.8, -1.8, "cylinder", 3, 255, 255, 255, 5) 
function markerHit(hitElement, matchingDimension) 
     if matchingDimension and hitElement == loco then 
          setTrainSpeed(hitElement, 0) 
          outputChatBox("Your message here") 
          setTimer(depart, 20000, 1) 
     end 
    setTimer(depart, 20000, 1) 
end 
addEventHandler( "onMarkerHit", markerGare2, stop ) 
  
function depart () 
    setTrainSpeed(loco, 1) 
    setTrainSpeed(locool, 1) 
end 
  
function removeStreamable( theElement ) 
    triggerClientEvent( "removeStreamable", getRootElement(), theElement ) 
end 
  

Link to comment
  • Moderators

Ok so I made few tests and I found the problem, the problem is that when we set the streamable of the trains to false, then ok there aren't streamed but their positions stay streamed. If I go to the 2nd station, I see the trains moving but not their positions. So their positions aren't updated but If I go to the position where they spawned, the position is "unstreamed" and the brake function works :|

Maybe someone can add this bug in bugtracker ( idk how to use it :mrgreen: )

If you want to make a test, then take this:

Server:

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) 
    -- removeStreamable( loco ) 
    -- removeStreamable( locool ) 
    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) 
    -- removeStreamable( pilot1 ) 
    -- removeStreamable( pilot2 ) 
    createBlipAttachedTo(loco, 42) 
    setTrainSpeed(loco, 0) 
    setTrainSpeed(locool,0.203) 
    outputChatBox("Train is here", source, 255, 255, 0) 
    removeStreamable( loco ) 
    setTimer(depart, 20000, 1) 
    nextgare = 1 
    setTimer( check, 2000, 0 ) 
end 
addCommandHandler("traintest", makeTrain) 
  
-- local markerGare2 = createMarker(787, -1341.8, -1.8, "cylinder", 3, 255, 255, 255, 5) 
function stop (hitElement) 
    -- outputChatBox("HIT") 
    if not( getElementType( hitElement ) == "vehicle" ) then return end 
    -- outputChatBox("c'est un véhicule") 
    if not( getElementModel( hitElement ) == 537 ) then return end 
    outputChatBox("Brake") 
    setTrainSpeed(loco, 0) 
    setTrainSpeed(locool, 0) 
    -- if ( source ) then 
        -- if ( source == markerGare2 )then 
            -- outputChatBox("Le Train est dans Market Station.", getRootElement(), 255, 255, 0) 
        -- end   
    -- end 
    setTimer(depart, 20000, 1) 
end 
-- addEventHandler( "onMarkerHit", getRootElement(), stop ) 
  
function depart () 
    -- nextgare = nextgare+1 
    setTrainSpeed(loco, 1) 
    setTrainSpeed(locool, 1) 
end 
  
function removeStreamable( theElement ) 
    outputChatBox( "remove_s") 
    triggerClientEvent( "removeStreamableLocos", getRootElement() ) 
end 
  
function check() 
    local x, y, z = getElementPosition( getElementByID( "loco1" ) ) 
    outputChatBox( x..", "..y..", "..z) 
    if nextgare == 2 then 
        if y >= -1341.8 then 
            stop( loco ) 
            nextgare = nextgare+1 
        end 
    -- elseif nextgare == 3 then -- a decommenter pour continuer 
        -- if   >=  then 
            -- stop( loco ) 
        -- end 
    end 
end 
  

Client:

addEvent("removeStreamableLocos", true ) 
function removeStreamableLocos( theElement ) 
    setElementStreamable( getElementByID( "loco1" ), false ) 
    setElementStreamable( getElementByID( "wagon1" ), false ) 
    setElementStreamable( getElementByID( "pilot1" ), false ) 
    setElementStreamable( getElementByID( "pilot2" ), false ) 
    outputChatBox("destream") 
end 
addEventHandler("removeStreamableLocos", getRootElement(), removeStreamableLocos ) 

So go to the the station underground of LS then type /traintest ( the T on the map is the train :roll: ) wait 10 sec then the train will move.

You will see that the position stay to 1725.9000244141, -1954, 16 and the train will not stop to the station.

ok so now go to the spawn position and then the position will instantanly change to another position. :|

Link to comment
Ok so I made few tests and I found the problem, the problem is that when we set the streamable of the trains to false, then ok there aren't streamed but their positions stay streamed. If I go to the 2nd station, I see the trains moving but not their positions. So their positions aren't updated but If I go to the position where they spawned, the position is "unstreamed" and the brake function works :|

Maybe someone can add this bug in bugtracker ( idk how to use it :mrgreen: )

its not a bug, its a MTA sync limit (100m for peds, 140 for vehicles)

even if someone will "fix" this, you wont find a server that will handle it...

Link to comment

View my code :

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" )) 
end 
addEventHandler("getTrain", getRootElement(), getTrain) 

Server side :

function setTrain (theElement) 
    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  

Link to comment

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.

Link to comment

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