Array index out of bounds
#1

[18:14:40] [debug] Run time error 4: "Array index out of bounds"
[18:14:40] [debug] Accessing element at index 500 past array upper bound 499
[18:14:40] [debug] AMX backtrace:
[18:14:40] [debug] #0 00063278 in public cmd_respawncars (4, 18271080) from usf.amx
[18:14:40] [debug] #1 native CallLocalFunction () from samp03svr
[18:14:40] [debug] #2 0000622c in public OnPlayerCommandText (4, 1827102 from usf.amx

and the code is

pawn Code:
CMD:respawncars(playerid,params[])
{
    new string[145];
    if (PlayerInfo[playerid][Admin] < 2) return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"You are not authorized to use this command!");
    for(new i=0;i<MAX_VEHICLES;i++)
    {
        if(IsVehicleOccupied(i) == 0)
        {
            DestroyVehicle(PlayerInfo[i][pSpawnVehicle]);
            SetVehicleToRespawn(i);
        }
    }
    if(AdminHide[playerid] == 0)
    {
        format(string, sizeof(string), ""ADMINPINK"- USF - Administrator {%06x}%s(%d) "ADMINPINK"has respawned Unoccupied vehicles",(GetPlayerColor(playerid) >>> 8),GetName(playerid),playerid);
    }
    if(AdminHide[playerid] == 1)
    {
        format(string, sizeof(string), ""ADMINPINK"- USF - An Administrator "ADMINPINK"has respawned Unoccupied vehicles");
    }
    SendClientMessageToAll(-1, string);
    return 1;
}

stock IsVehicleOccupied(vehicleid)
{
    for(new i =0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerInVehicle(i,vehicleid))
        {
            return 1;
        }
    }
    return 0;
}
Please help me
Thanks in advance
Reply
#2

show line
Reply
#3

Quote:
Originally Posted by ATGOggy
View Post
show line
Crashdetector cant detect line
it only shows the snippet
Reply
#4

use this loop in command
pawn Code:
for(new i=0;i<MAX_VEHICLES;i++)
    {
        if(IsVehicleOccupied(i) == 0)
        {
            //DestroyVehicle(PlayerInfo[i][pSpawnVehicle]);  this is the line that is causing the error
            SetVehicleToRespawn(i);
        }
    }
Reply
#5

How did u find that
how can u know only that line is the problem...

Please tell me as i wanna know to face future situations
Reply
#6

First note this line from nativechecher
Quote:

[debug] Accessing element at index 500 past array upper bound 499
Meaning:
This Line means that a array that has a size of 500 that means valid indexes in array are from 0 to 500-1, i.e., 0 to 499
The "500" in the message shows that error was caused when pawn tried to access 500th element/index of the array.
But as you see that valid locations are from 0 to 499 so it caused error

Tracing it:
To trace it down you have to find the array which are declared like this arr[500], arr[500][enum]...etc
Now as default value of MAX_PLAYERS is 500 and MAX_VEHICLES is 2000 so its obvious that array with MAX_PLAYERS is causing it and its obvious that pInfo is declared with MAX_PLAYERS, hence traced.

Why is it happening?
Quote:

for(new i=0;i<MAX_VEHICLES;i++)
{
if(IsVehicleOccupied(i) == 0)
{
DestroyVehicle(PlayerInfo[i][pSpawnVehicle]);

As you can see loop is going from 0 to MAX_VEHICLES, i.e., 2000. But PlayerInfo is declared like this PlayerInfo[MAX_PLAYERS][enum], i.e., PlayerInfo[500][enum]
So here loop works fine till 499 index but as you try to access 500th index which is not valid, the script crashes
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)