PDA

View Full Version : Custom function problem

moof2010
05/05/2014, 07:30 PM
i use this:
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?

Smileys
05/05/2014, 07:39 PM

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:

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

new Float:p_X;
new Float:p_Y;
new Float:p_Z;

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

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

Vince
05/05/2014, 07:48 PM

False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.

Teemo
05/05/2014, 07:50 PM
False. Vehicleids do start at 1. This is probably done to give GetPlayerVehicleID a proper return value.

Right

Konstantinos
05/05/2014, 07:51 PM
http://pastebin.com/4p3c0wba

Smileys
05/05/2014, 07:51 PM
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 :cool:

moof2010
05/05/2014, 07:54 PM

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:

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

new Float:p_X;
new Float:p_Y;
new Float:p_Z;

new Float:Distance;
new Float:PretendentDistance = range +1;
new Pretendent;
to this.
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

moof2010
05/05/2014, 08:08 PM
http://pastebin.com/4p3c0wba

0 all times

http://i.imgur.com/4cI4BFo.jpg

Konstantinos
05/05/2014, 08:18 PM
I guess GetVehicleDistanceFromPoint returns 0 if a vehicle doesn't exist so check if it does.

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;
}

moof2010
05/05/2014, 08:37 PM
thank you very much, it works. sorry for late, i ate

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