SA-MP Forums Archive
Gamemode crash - 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: Gamemode crash (/showthread.php?tid=346867)



Gamemode crash - ChristofferHoffmann - 30.05.2012

Код:
[09:39:16] [debug] Run time error 4: "Array index out of bounds"
[09:39:16] [debug]   Accessing element at index 999 past array upper bound 500
[09:39:16] [debug] Backtrace (most recent call first):
[09:39:16] [debug] #0  000603e0 in OnPlayerStateChange () from OGHRP.amx
This is what I get before it crashes

Код:
public OnPlayerStateChange(playerid, newstate, oldstate)
{
    if(newstate != 2) NOPTrigger[playerid] = 0;
    if(IsPlayerNPC(playerid))
	{
	    if(newstate == PLAYER_STATE_SPECTATING)
	    {
        	TogglePlayerSpectating(playerid, false);
		}
		return 1;
	}
	if(GettingSpectated[playerid] != 999)
	{
	    new spectator = GettingSpectated[playerid];
	    if(!IsPlayerConnected(spectator))
	    {
	        GettingSpectated[playerid] = 999;
	        Spectate[spectator] = 999;
		}

	    if(newstate == PLAYER_STATE_DRIVER && PlayerInfo[spectator][pAdmin] >= 2 || newstate == PLAYER_STATE_PASSENGER && PlayerInfo[spectator][pAdmin] >= 2)
	    {
	        TogglePlayerSpectating(spectator, true);
			new carid = GetPlayerVehicleID( playerid );
			PlayerSpectateVehicle( spectator, carid );
			SetPVarInt(spectator, "SpecState", newstate);
	    }
	    else if(newstate == PLAYER_STATE_ONFOOT && PlayerInfo[spectator][pAdmin] >= 2)
	    {
	        TogglePlayerSpectating(spectator, true);
		   	PlayerSpectatePlayer( spectator, playerid );
		   	SetPlayerInterior( spectator, GetPlayerInterior( playerid ) );
			SetPVarInt(spectator, "SpecState", newstate);
	    }
	}



Re: Gamemode crash - ViniBorn - 30.05.2012

Check your array sizes


Re: Gamemode crash - ChristofferHoffmann - 30.05.2012

I still can't seem to figure out the problem, I'm trying to learn from these errors.. but for now they're killing the learning part, lol.


Re: Gamemode crash - iggy1 - 30.05.2012

Your problem is the "spectator" var you are using as an index. When it is set to 999, you are using it as an index for an array that isn't that large.

EDIT: Print the value of "spectate" before you use it on an array and check if the array is large enough for it.


Re: Gamemode crash - ChristofferHoffmann - 31.05.2012

Hey

Thank you for the answers, yet I am not completely sure how I am supposed to "Print the value of spectate before you use it on an array"

I'm a newbie to this, and I'm still in the learning process. So if you could simplify your answer, I'd be happy. Hope it's not too much trouble.