PDA

View Full Version : Issues with a function.


vIBIENNYx
01/10/2012, 11:12 PM
The function is used to get the closest vehicle to a player, however, it doesn't work as expected. It doesn't check the vehicles as expected.

Here is the code:
forward GetClosestVehicleFromPlayer(playerid);
public GetClosestVehicleFromPlayer(playerid)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(GetVehicleModel(playerid) != 0)
{
new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
if(distance < 3)
{
return i;
}
}
}
return -1;
}

Here is the exact issue. I tried the function before a vehicle was spawned and it worked correctly, however. Once a vehicle is spawn it is as if the for loop doesn't check the ID's properly. Here is a screenshot.

http://i1209.photobucket.com/albums/cc390/vIBIENNYx/sa-mp-045.png

Any ideas?

Edit: For Player ID 0, the Returning ID is -1, for Player ID 1, the the ID returns 0.

Vince
01/10/2012, 11:20 PM
if(GetVehicleModel(playerid) != 0)
???

vIBIENNYx
01/10/2012, 11:23 PM
if(GetVehicleModel(playerid) != 0)
???

That basically checks to see if the vehicle exists.

ReneG
02/10/2012, 12:33 AM
That would check if a player was in a car, or if the model id of the car the player is in is not 0.

It should be
public GetClosestVehicleFromPlayer(playerid)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(IsValidVehicle(i))
{
new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
if(distance < 3)
{
return i;
}
}
}
return -1;
}

vIBIENNYx
02/10/2012, 03:33 AM
That would check if a player was in a car, or if the model id of the car the player is in is not 0.

It should be
public GetClosestVehicleFromPlayer(playerid)
{
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(IsValidVehicle(i))
{
new Float:distance = GetVehicleDistanceFromPoint(i, x, y, z);
if(distance < 3)
{
return i;
}
}
}
return -1;
}


Thank you very much. This has solved the issue, repped you both.