Problems with a function
#1

Okay so I have this function

Код:
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 when I try to check for closest vehicle, it always give me the 1999 ID of a car that doesn't exists, I don't know why is this.
Reply
#2

First, check if the vehicle ID is valid using GetVehicleModel. Second, show me the GetVehicleDistanceFromPoint function.
Reply
#3

Quote:
Originally Posted by Marricio
Посмотреть сообщение
First, check if the vehicle ID is valid using GetVehicleModel. Second, show me the GetVehicleDistanceFromPoint function.
The GetVehicleDistanceFromPoint function is a native samp function. The problem is the car ID, that it always shows me that the ID is 1999
Reply
#4

Quote:
Originally Posted by Antenastyle
Посмотреть сообщение
The GetVehicleDistanceFromPoint function is a native samp function. The problem is the car ID, that it always shows me that the ID is 1999
TIL.

It always show you the ID is 1999 because the loop runs until the end. The code structure goods look though, what range are you using? And, you're near 0.0, 0.0, 0.0 in Blueberry?
Reply
#5

Quote:
Originally Posted by Marricio
Посмотреть сообщение
TIL.

It always show you the ID is 1999 because the loop runs until the end. The code structure goods look though, what range are you using? And, you're near 0.0, 0.0, 0.0 in Blueberry?
I'm using range 10, and when I try using it, I'm in Montgomery
Reply
#6

Код HTML:
GetClosestVehicle(playerid, Float:range) {

	new Float:POS[3];
	if(GetPlayerPos(playerid, POS[0], POS[1], POS[2])) // check is player valid + get player pos
	{
		new Float:distance, cveh;
		for(new v; v < MAX_VEHICLES; v++)
		{
			if(!IsValidVehicle(v)) return INVALID_VEHICLE_ID;
			distance = GetVehicleDistanceFromPoint(v, POS[0], POS[1], POS[2]);
			if(distance <= range) cveh = v;
		}
		return cveh;
	}
	return false; // smthng wrong
}
Reply
#7

Quote:
Originally Posted by JustNothing
Посмотреть сообщение
Код HTML:
GetClosestVehicle(playerid, Float:range) {

	new Float:POS[3];
	if(GetPlayerPos(playerid, POS[0], POS[1], POS[2])) // check is player valid + get player pos
	{
		new Float:distance, cveh;
		for(new v; v < MAX_VEHICLES; v++)
		{
			if(!IsValidVehicle(v)) return INVALID_VEHICLE_ID;
			distance = GetVehicleDistanceFromPoint(v, POS[0], POS[1], POS[2]);
			if(distance <= range) cveh = v;
		}
		return cveh;
	}
	return false; // smthng wrong
}
When I put it on a command it returns me Server:Unknown command.
Console:
Reply
#8

Quote:
Originally Posted by Antenastyle
Посмотреть сообщение
When I put it on a command it returns me Server:Unknown command.
Console:
https://sampwiki.blast.hk/wiki/IsValidVehicle

IVV isnt defined by default xd define it like in wiki page xd
Reply
#9

Quote:
Originally Posted by JustNothing
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/IsValidVehicle

IVV isnt defined by default xd define it like in wiki page xd
It's defined xd
Reply
#10

Try this out:

PHP код:
#define LoopVehicles(%1) \
    
for(new %GetVehiclePoolSize(); != %1; %1--)
stock GetClosestVehicle(playeridFloat:range)
{
    new 
Floatp_XFloatp_YFloatp_ZFloatDistance 9999.9Vehicle INVALID_VEHICLE_IDFloatTemp;
    
GetPlayerPos(playeridp_Xp_Yp_Z);
    
LoopVehicles(i)
    {
        
Temp GetVehicleDistanceFromPoint(ip_Xp_Yp_Z);
        if(
range <= Temp <= Distance//if(Temp <= Distance && Temp <= range)
        
{
            
Vehicle i;
            
Distance Temp;
        }
    }
    return 
Vehicle;

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)