SA-MP Forums Archive
Accessing element at negative index -400 - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Accessing element at negative index -400 (/showthread.php?tid=546828)



Accessing element at negative index -400 - nGen.SoNNy - 18.11.2014

Код:
[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;
}



Re: Accessing element at negative index -400 - xCrazyMonkey - 18.11.2014

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.


Re: Accessing element at negative index -400 - oliverrud - 18.11.2014

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.


Re: Accessing element at negative index -400 - nGen.SoNNy - 18.11.2014

So ?


Re: Accessing element at negative index -400 - Threshold - 18.11.2014

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));'


Re: Accessing element at negative index -400 - nGen.SoNNy - 18.11.2014

Now i understand..thanks!