Jump to content

Getting the nearest vehicle


gerlachmax98

Recommended Posts

This client-sided function should get you the nearest vehicle to the local player.

function getNearestVeicle() 
    local x, y, z = getElementPosition( localPlayer ) 
    local prevDistance  
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 

Link to post
[2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:85: Bad argument @ 'getElementPosition' [Expected element at argument 1, got nil] 
[2013-01-24 20:51:05] WARNING: vio_lite\reparieren\server.lua:89: Bad argument @ 'getDistanceBetweenPoints3D' 
[2013-01-24 20:51:05] ERROR: vio_lite\reparieren\server.lua:90: attempt to perform arithmetic on local 'distance' (a boolean value) 

I have it now so:

function getNearestVeicle() 
    local x, y, z = getElementPosition( localPlayer ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
            setElementHealth ( v, 800 ) 
        end 
    end 
    return nearestVehicle or false 
end 
addCommandHandler("repaircar", getNearestVeicle) 

Link to post

As I said, this function is only for client-side, to use it on server-side, i have to modify it a bit, i will do it later ( cuz im from my phone now ).

And it won't repair your vehicle, it just gets the nearest vehicle to the local player. An example how to use it:

fixVehicle( getNearestVeicle() ) 

I've just noticed that a word is missing from the name of the function, of course it will work with this name too, just doesn't look good :)

Link to post

Try this:

function getNearestVeicle(thePlayer) 
    local x, y, z = getElementPosition( thePlayer ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 )  then 
            prevDistance = distance 
            nearestVehicle = v 
            setElementHealth ( v, 800 ) 
        end 
    end 
    return nearestVehicle or false 
end 
addCommandHandler("repaircar", getNearestVeicle) 

csiguusz: You're wrong, this function isnt only for client-side, thats for both.

Link to post

Because i have a lot of time now, a made it! :)

This should work on server-side. But i think it would be better to get the nearest veh on client side, because so many calculations can have an impact on the server's performance.

function getNearestVehicle( player )  
    local x, y, z = getElementPosition( player )  
    local prevDistance  
    local nearestVehicle  
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do  
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) )  
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance  
            nearestVehicle = v 
        end  
    end  
    return nearestVehicle or false  
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        fixVehicle( vehicle ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh )  

Edited by Guest
Link to post

so?

function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        setPedAnimation( player, "wash_up", "anim") -- set your animation to a player 
        setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds 
        setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) 

Link to post
so?
function getNearestVehicle( player ) 
    local x, y, z = getElementPosition( player ) 
    local prevDistance 
    local nearestVehicle 
    for i, v in ipairs( getElementsByType( "vehicle" ) ) do 
        local distance = getDistanceBetweenPoints3D( x, y, z, getElementPosition( v ) ) 
        if distance <= ( prevDistance or distance + 1 ) then 
            prevDistance = distance 
            nearestVehicle = v 
        end 
    end 
    return nearestVehicle or false 
end 
  
function doSomethingWithTheNearestVeh( player ) 
    local vehicle = getNearestVehicle( player ) -- get the nearest vehicle to the player who used the command 
    if vehicle then 
        setPedAnimation( player, "wash_up", "anim") -- set your animation to a player 
        setTimer( setPedAnimation, 30000, 1, player ) -- set a timer to cancel the anim after 30 seconds 
        setElementHealth( vehicle, 800 ) -- fix the nearest vehicle. this is just an example, you can do here anything else 
    end 
end 
addCommandHandler( "repaircar", doSomethingWithTheNearestVeh ) 

Does it work?

Link to post

You can check if the player is in any vehicle so:

if not getPedOccupiedVehicle( player ) then 
-- the player is on foot 
end 
  

You can get the driver of a vehicle with the getVehicleController function.

It's possible to check if you are standing in front of a vehicle or not somehow ( i think with getElementMatrix ) but i cant' help u with this, i don't understand matrixes very well.

Link to post

This function, what i gave you checks the distance between the given player and every vehicles on the map. So it will work with every vehicles. It can even return a vehicle that is in an another city because it can also be the nearest.

Link to post

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