OnPlayerStateChange run time error
#1

I get these warnings when I join my server how to fix it?

Код:
[14:56:46] [debug] Run time error 4: "Array index out of bounds"
[14:56:46] [debug]  Accessing element at index 999 past array upper bound 499
[14:56:46] [debug] AMX backtrace:
[14:56:46] [debug] #0 00089bac in public OnPlayerStateChange (0x00000000, 0x00000001, 0x00000000)
Reply
#2

Show on Player State Change !
Reply
#3

Here it is http://pastebin.com/Kb0S6Wyz
Reply
#4

bump
Reply
#5

It's fairly obvious that it's related to this part;
pawn Код:
if(GettingSpectated[playerid] != INVALID_PLAYER_ID) // suppose this contains 999. Obviously not equal to INVALID_PLAYER_ID so this is true
    {
        new spectator = GettingSpectated[playerid]; // spectator now contains 999
        if(!IsPlayerConnected(spectator)) // player 999 does not exist and is therefore not connected so this is also true
        {
            GettingSpectated[playerid] = 999;
            Spectate[spectator] = 999; // 'spectator' is still 999, which is an invalid array index, hence causing the OOB error
        }
        // bunch of code
    }
Why do you use INVALID_PLAYER_ID in one place and 999 in another?
Reply
#6

so what is the fix? I tried to put in that INVALID_PLAYER_ID and -1 it also gave me the same errors
Reply
#7

The problem lies a few lines lower at line 46 in your pastebin.
pawn Код:
if(PlayerInfo[spectator][pAdmin] >= 2)
You have given the PlayerInfo-array a size of 500 (MAX_PLAYERS) so in this array only cell '0' till '499' exist.
A few lines earlier (the ones Vince pointed you to) you give the variable 'spectator' a value of 999 and now you try to access cell '999' in the PlayerInfo-array which does not exist. (499 is the maximum). For the same reason giving 'spectator' a value of '-1' will not work as well. (Since it starts at '0').

To solve this you have to check if a valid value is passed, like this:
pawn Код:
new spectator = GettingSpectated[playerid];
if(0 <= spectator < MAX_PLAYERS)
{
    if(PlayerInfo[spectator][pAdmin] >= 2) {
        // Preventing possible buffer overflows with the arrays
        TogglePlayerSpectating(spectator, true);
        PlayerSpectatePlayer( spectator, playerid );
        SetPlayerInterior( spectator, GetPlayerInterior( playerid ) );
        SetPVarInt(spectator, "SpecState", newstate);
        SetPlayerInterior( spectator, GetPlayerInterior( playerid ) );
        SetPlayerVirtualWorld( spectator, GetPlayerVirtualWorld( playerid ) );
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)