optimize code
#1

I made a code, but want to let him 100 percent functional, the problem is that when I approach a vehicle it checks another vehicle farther than what I desire, sometimes function checks as far even though vehicle one in front of you .. how can I fix it?

PHP Code:
public PlacaVeiculoPerto(playerid)
{
    for( new 
iMAX_VEHICLESi++) {
        new 
Float:Pos[3];
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        if(
IsPlayerInRangeOfPoint(playerid7.0Pos[0], Pos[1], Pos[2])) {
            return 
i;
        }
    }
    return 
0;

Reply
#2

well, you should not return on IsPlayerInRange of point, but instead save in variable distance and vehicle id and continue with loop, when you get to another vehicle that is in radius check distance again and compare it with last one, if it is smaller than update variable with vehicle id and variable with distance to new values.

And after that return vehicle id out of loop.

something like this

PHP Code:
public PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Pos[3];
    for( new 
iMAX_VEHICLESi++) {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
   
    return 
vehicleid;

Reply
#3

error


Code:
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
C:\Users\2\Desktop\gamemode\gamemodes\2.pwn(3075) : warning 213: tag mismatch
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


3 Warnings.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Pos[3];
    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]); //error <<<<<<<<<<<
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;

Please
Reply
#4

Hello!

You forgot the Float:-Tag.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Float:Pos[3];
    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;


- Mencent
Reply
#5

Quote:
Originally Posted by Mencent
View Post
Hello!

You forgot the Float:-Tag.
PHP Code:
stock PlacaVeiculoPerto(playerid)
{
    new 
Float:distance 10.0FloatcurrentDistance 11.0vehicleid 0Float:Pos[3];
    for(new 
iMAX_VEHICLESi++)
    {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        
currentDistance GetPlayerDistanceFromPoint(playeridPos[0], Pos[1], Pos[2]);
        if(
currentDistance <= 7.0 && currentDistance distance) {
            
vehicleid i;
            
distance currentDistance;
        }
    }
    return 
vehicleid;


- Mencent
Tranks!

@topic
Sorry, but it was not that above code that was in error, is the code below that checks the id of the vehicle, and it checks where this really the vehicle, this is the code with the error:

PHP Code:

public PlayerPertoVeiculo(playerid)
{
    static 
Float:Pos[3];
    for( new 
iMAX_VEHICLESi++) {
        
GetVehiclePos(iPos[0], Pos[1], Pos[2]);
        if(
IsPlayerInRangeOfPoint(playerid7.0Pos[0], Pos[1], Pos[2])) {
            return 
1;
        }
    }
    return 
0;

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)