What does this 'GetNearestVehicle' really do?
#1

I have trying to figoure out, how this works for few hour now with my friend lol. And still not 100% sure, what does this 'Distance' part should do.. could someone explain me? lol

pawn Код:
stock GetNearestVehicle(playerid, Float:distance)
{
    new
            Float:xX,
            Float:yY,
            Float:zZ,
            retElement = -1
    ;
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        GetVehiclePos(i, xX, yY, zZ);
        new Float:odist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);
        if (retElement == -1)
        {
            retElement = i;
            distance = odist;
        }
        else if (odist < distance)
        {
            retElement = i;
            distance = odist;
        }
    }
    return retElement;
}
Reply
#2

it brings to you the nearest vehicle. i think, because that's what happens in the mod sobit
Reply
#3

It allows you to search in an area I believe,

like
pawn Код:
GetNearestVehicle(playerid, 66);
Will only look for vehicles within 66 units of the player.
Reply
#4

Quote:
Originally Posted by Lauder
Посмотреть сообщение
it brings to you the nearest vehicle. i think, because that's what happens in the mod sobit
Obvious troll is obvious.

Quote:
Originally Posted by milanosie
Посмотреть сообщение
It allows you to search in an area I believe,

like
pawn Код:
GetNearestVehicle(playerid, 66);
Will only look for vehicles within 66 units of the player.
BUT, when i am out range of 66, it will still get me the closest vehicle... so, where i need the distance then?
Reply
#5

Because if the nearest vehicle is 67 units away, the function will fail to find a vehicle.
Reply
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
Because if the nearest vehicle is 67 units away, the function will fail to find a vehicle.
But it still finds one?
Reply
#7

Look at the code, it shows you what it's suppose to do if it doesn't find a vehicle within 66 units.
It will just return the ID of the last vehicle looped through.


This will return -1 if there is no vehicle within 66 units.
Код:
stock GetNearestVehicle(playerid, Float:distance)
{
    new
            Float:xX,
            Float:yY,
            Float:zZ,
            retElement = -1
    ;
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        GetVehiclePos(i, xX, yY, zZ);
        new Float:odist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);
        if (retElement == -1)
        {
            retElement = i;
            distance = odist;
        }
        else if (odist < distance)
        {
            retElement = i;
            distance = odist;
        }
        else if (odist > distance)
        {
            return -1;
        }
    }
    return retElement;
}
Reply
#8

Quote:
Originally Posted by Shetch
Посмотреть сообщение
Look at the code, it shows you what it's suppose to do if it doesn't find a vehicle within 66 units.
It will just return the ID of the last vehicle looped through.


This will return -1 if there is no vehicle within 66 units.
Код:
stock GetNearestVehicle(playerid, Float:distance)
{
    new
            Float:xX,
            Float:yY,
            Float:zZ,
            retElement = -1
    ;
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        GetVehiclePos(i, xX, yY, zZ);
        new Float:odist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);
        if (retElement == -1)
        {
            retElement = i;
            distance = odist;
        }
        else if (odist < distance)
        {
            retElement = i;
            distance = odist;
        }
        else if (odist > distance)
        {
            return -1;
        }
    }
    return retElement;
}
Thanks man. I suppouse it can return to 0, as vehicleid's starts from ID: 1? So if no vehicle found in 66meters, it will return 0, yes?
Reply
#9

Yup, you can return 0 there, my mistake.

https://sampwiki.blast.hk/wiki/Starting_IDs
Reply
#10

That code didnt work, it returned the vehicleid even when i was outside of the range. Thanks for trying though!

Anyway i got what i've wanted + some more!

All credits and thanks goes to Pottus, for making such a multi-function, thanks dyyd!

Usage:
pawn Код:
new vehicleID, Float:distance;
GetNearestVehicleEx(playerid, 5.0, true, vehicleID, distance);
if(vehicleID == INVALID_VEHICLE_ID) return SendClientMessage(playerid, -1, "You are not near any vehicle.");
printf("%f meters distance from a player");
printf("Vehicle ID: %d is closest in a range of 5meters");
pawn Код:
stock GetNearestVehicleEx(playerid, Float:distance, StreamedOnly, &retElement, &Float:retDistance)
{
    new
        Float:xX,
        Float:yY,
        Float:zZ,
        Float:tempdist;
   
    retDistance = 0.0;
    retElement = INVALID_VEHICLE_ID;
       
    for(new i = 1; i < MAX_VEHICLES; i++)
    {
        if(StreamedOnly == 1) { if(!IsVehicleStreamedIn(i, playerid)) continue; }

        GetVehiclePos(i, xX, yY, zZ);
        tempdist = GetPlayerDistanceFromPoint(playerid, xX, yY, zZ);

        if(distance > 0.0)
        {
            if(tempdist < distance)
            {
                if(retDistance == 0.0)
                {
                    retDistance = tempdist;
                    retElement = i;
                }
                else if(tempdist < distance)
                {
                    retDistance = tempdist;
                    retElement = i;
                }
            }
        }
        else
        {
            if(retDistance == 0.0)
            {
                retDistance = tempdist;
                retElement = i;
            }
            else
            {
                if(tempdist < retDistance)
                {
                    retDistance = tempdist;
                    retElement = i;
                }
            }
        }
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)