Accessing element at negative index -400
#1

Код:
[10:19:33] [debug] AMX backtrace:
[10:19:33] [debug] #0 00105138 in public Veh_Speedo () at E:\GM.pwn:19834
[10:19:34] [debug] Run time error 4: "Array index out of bounds"
[10:19:34] [debug]  Accessing element at negative index -400
pawn Код:
function Veh_Speedo( )
{
    new sp_string[ 256 ], liSpeed;
    foreach( Player, i )
    {
        new vehicleid = GetPlayerVehicleID( i );
        if( vehicleid != 0 && SpeedoStats[ i ] == true )
        {
            liSpeed = GetPlayerSpeed( i, true );
            format( sp_string, sizeof(sp_string), "~w~~h~'  ~r~~h~Vehicle: ~w~~h~%s", vehName[ GetVehicleModel( vehicleid ) - 400 ] );
            PlayerTextDrawSetString( i, Speedo_TD[ 0 ], sp_string );
            format( sp_string, sizeof(sp_string), "~b~~h~KM/h: ~w~~h~%d", liSpeed );
            PlayerTextDrawSetString( i, Speedo_TD[ 1 ], sp_string );
        }
    }
    return true;
}
Reply
#2

sp_string[128]
Try that, see if he still gives an error, and I highly reccomend you not to make your variables 256 cells big, it causes a lot of problems most of the time.
Reply
#3

Is most likely related to
vehName[ GetVehicleModel( vehicleid ) - 400 ]
not sp_string as mentioned above. Although I do agree that 256 cells seems too high.
Reply
#4

So ?
Reply
#5

pawn Код:
function Veh_Speedo( )
{
    new sp_string[ 60 ], liSpeed;
    foreach( Player, i )
    {
        if( ! SpeedoStats[ i ] ) continue;
        new model = GetVehicleModel( GetPlayerVehicleID( i ) );
        if(!model) continue;
        liSpeed = GetPlayerSpeed( i, true );
        format( sp_string, sizeof(sp_string), "~w~~h~'  ~r~~h~Vehicle: ~w~~h~%s", vehName[ model - 400 ] );
        PlayerTextDrawSetString( i, Speedo_TD[ 0 ], sp_string );
        format( sp_string, sizeof(sp_string), "~b~~h~KM/h: ~w~~h~%d", liSpeed );
        PlayerTextDrawSetString( i, Speedo_TD[ 1 ], sp_string );
    }
    return true;
}
Using 256 cells when only 60 cells or even less is necessary is not practical. You need to make sure both vehicle model and vehicle ID are valid before even thinking of putting them into an array. Unexpected or invalid values will cause out of bounds errors. 'GetVehicleModel' returns either the vehicle model or '0' if the vehicle does not exist. So you can do both a valid vehicle and valid model check in one simple execution of 'GetVehicleModel(GetPlayerVehicleID(i));'
Reply
#6

Now i understand..thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)