SA-MP Forums Archive
OnPlayerStateChange run time error - 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: OnPlayerStateChange run time error (/showthread.php?tid=557861)



OnPlayerStateChange run time error - ahmedkoki - 15.01.2015

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)



Re: OnPlayerStateChange run time error - MBilal - 15.01.2015

Show on Player State Change !


Re: OnPlayerStateChange run time error - ahmedkoki - 15.01.2015

Here it is http://pastebin.com/Kb0S6Wyz


Re: OnPlayerStateChange run time error - ahmedkoki - 05.02.2015

bump


Re: OnPlayerStateChange run time error - Vince - 05.02.2015

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?


Re: OnPlayerStateChange run time error - ahmedkoki - 05.02.2015

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


Re: OnPlayerStateChange run time error - Schneider - 05.02.2015

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