Get closest problem
#1

Alright so the problem is simple, getting closest vehicle to me isnt working. Nothing basically happends
PHP код:
if(PRESSED(KEY_FIRE )) {
        if(
pState == PLAYER_STATE_ONFOOT) {
             new 
somecar GetClosestVehicleForPlayer(playerid);
             new 
enginelightsalarmdoorsbonnetbootobjective;
             if(
VehInfo[somecar][VehMasterID] == AccInfo[playerid][ID] || VehInfo[somecar][VehFaction] == AccInfo[playerid][PlayerFaction]) {
                if(
VehInfo[somecar][VLock] == 0) {
                            
VehInfo[somecar][VLock] = 1;
                            
GameTextForPlayer(playerid,"~r~Vehicle Locked",2000,4);
                              
GetVehicleParamsEx(somecarenginelightsalarmdoorsbonnetbootobjective);
                            
SetVehicleParamsEx(somecarenginelightsalarm1bonnetbootobjective);
                }
                else if(
VehInfo[somecar][VLock] == 1) {
                            
VehInfo[somecar][VLock] = 0;
                            
GameTextForPlayer(playerid,"~r~Vehicle Unlocked",2000,4);
                            
GetVehicleParamsEx(somecarenginelightsalarmdoorsbonnetbootobjective);
                            
SetVehicleParamsEx(somecarenginelightsalarm0bonnetbootobjective);
                }
            }
        }
    }
GetClosestVehicleForPlayer(playerid)
{
    new 
vehicleid INVALID_VEHICLE_ID;
    new 
Float:distance 99999.0;
    new 
Float:XFloat:YFloat:Z;
    
GetPlayerPos(playeridXYZ);
    for(new 
0MAX_VEHICLESi++)
    {
        if(
GetVehicleDistanceFromPoint(iXYZ) < distance && distance 5.0//you can change this value
        
{
            
vehicleid i;
            
distance GetVehicleDistanceFromPoint(iXYZ);
        }
    }
    return 
vehicleid;

Reply
#2

You instantiated variable distance with a value of 99999.0, then in your first if statement you check if the distance is less than 5.0 which is false. So all the possible combinations are (false && false) = false and (true, false) = false. which means it will never enter first if block.

Код:
GetClosestVehicleForPlayer(playerid)
{
    new vehicleid = INVALID_VEHICLE_ID;
    new Float:distance = 99999.0;
    new Float:X, Float:Y, Float:Z;
    GetPlayerPos(playerid, X, Y, Z);
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(!IsValidVehicle(i)) continue;
        new Float: tmp = GetVehicleDistanceFromPoint(i, X, Y, Z);
        if(tmp > distance) continue;
        vehicleid = i;
        distance = tmp;
    }
    return vehicleid;
}
NOTE:code not tested
Reply
#3

Ahaaaaaaaaaaaaa thanks ! +rep it works
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)