Custom function problem
#1

i use this:
Code:
stock GetClosestVehicle(playerid, Float:range)
{
    new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;

    GetPlayerPos(playerid, p_X, p_Y, p_Z);

    for(new vehicleid=1; vehicleid < MAX_VEHICLES; vehicleid++)
    {
        Distance = GetVehicleDistanceFromPoint(vehicleid, p_X, p_Y, p_Z);

        if(Distance <= range && Distance <= PretendentDistance)
        {
            Pretendent = vehicleid;
            PretendentDistance = Distance;
        }
    }

    return Pretendent;
}
but it returned me 1999 all time. I check at less than 10m from an vehicle end it tell me that nearrest vehicle is 1999 too

Why?
Reply
#2

I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:

pawn Code:
for(new vehicleid=0; vehicleid < MAX_VEHICLES; vehicleid++)
unless you have a specific reason to start at 1.

not sure if this will fix it, but the rest of the code seems fine to me; this is the only thing I could find.

tho, I also wonder why you do this:

pawn Code:
new     Float:PretendentDistance = range +1; // creates a variable that has the range + 1;

if(Distance <= range && Distance <= PretendentDistance) // checks if the vehicle is within the normal range, and within the range of the normal range +1;
if it inside the normal range then obviously it's also inside the range + 1; so I don't really understand why you do that.


also, you should change this
pawn Code:
new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;
to this.
pawn Code:
new    
    Float:p_X,
    Float:p_Y,
    Float:p_Z,

    Float:Distance,
    Float:PretendentDistance = range +1,
    Pretendent
;
Reply
#3

Quote:
Originally Posted by Smileys
View Post
I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
Reply
#4

Quote:
Originally Posted by Vince
View Post
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
Right
Reply
#5

http://pastebin.com/4p3c0wba
Reply
#6

Quote:
Originally Posted by Vince
View Post
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.
hm, I didn't know that; probably because other stuff does start at 0.

well, thanks for telling anyway
Reply
#7

Quote:
Originally Posted by Smileys
View Post
I wonder why you start with vehicleid 1, instead of 0, vehicle ids start with 0, not with one, so what you should be using is:

pawn Code:
for(new vehicleid=0; vehicleid < MAX_VEHICLES; vehicleid++)
unless you have a specific reason to start at 1.

not sure if this will fix it, but the rest of the code seems fine to me; this is the only thing I could find.

tho, I also wonder why you do this:

pawn Code:
new     Float:PretendentDistance = range +1; // creates a variable that has the range + 1;

if(Distance <= range && Distance <= PretendentDistance) // checks if the vehicle is within the normal range, and within the range of the normal range +1;
if it inside the normal range then obviously it's also inside the range + 1; so I don't really understand why you do that.


also, you should change this
pawn Code:
new     Float:p_X;
    new     Float:p_Y;
    new     Float:p_Z;

    new     Float:Distance;
    new     Float:PretendentDistance = range +1;
    new     Pretendent;
to this.
pawn Code:
new    
    Float:p_X,
    Float:p_Y,
    Float:p_Z,

    Float:Distance,
    Float:PretendentDistance = range +1,
    Pretendent
;

i don't see what is the diference?

and about pretendentdistance i change it inside for
Reply
#8

Quote:
Originally Posted by Konstantinos
View Post
0 all times

for
Code:
new testrsadas[128];
							format(testrsadas,sizeof(testrsadas),"masina cea mai apropriata %i",GetClosestVehicle(playerid, 10));
					        SendClientMessage(playerid,0xFF0000FF, testrsadas);
Reply
#9

I guess GetVehicleDistanceFromPoint returns 0 if a vehicle doesn't exist so check if it does.

pawn Code:
GetClosestVehicle(playerid, Float: range)
{
    new
        vehicleid = INVALID_VEHICLE_ID,
        Float: pX,
        Float: pY,
        Float: pZ,
        Float: distance = 9999.0,
        Float: tmp_distance;
   
    GetPlayerPos(playerid, pX, pY, pZ);
 
    for (new v = 1; v != MAX_VEHICLES; ++v)
    {
        if (!GetVehicleModel(v)) continue;
        tmp_distance = GetVehicleDistanceFromPoint(v, pX, pY, pZ);
        if (tmp_distance <= range && tmp_distance < distance)
        {
            distance = tmp_distance;
            vehicleid = v;
        }
    }
    return vehicleid;
}
Reply
#10

thank you very much, it works. sorry for late, i ate

http://imgur.com/RMdzKIx,DaQQIyg,AoSCcvu#0
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)