SA-MP Forums Archive
Run time error 4: "Array index out of bounds" [+REP] - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Server (https://sampforum.blast.hk/forumdisplay.php?fid=6)
+--- Forum: Server Support (https://sampforum.blast.hk/forumdisplay.php?fid=19)
+--- Thread: Run time error 4: "Array index out of bounds" [+REP] (/showthread.php?tid=526715)



Run time error 4: "Array index out of bounds" [+REP] - driftpower - 19.07.2014

Error:
Код:
[02:41:35] [debug] AMX backtrace:
[02:41:35] [debug] #0 000025d8 in public AngleUpdate () from driftpoints.amx
[02:41:35] [debug] Run time error 4: "Array index out of bounds"
[02:41:35] [debug]  Accessing element at index 200 past array upper bound 199
[02:41:35] [debug] AMX backtrace:
[02:41:35] [debug] #0 0000356c in public CheckPlayerState () from driftpoints.amx
[02:41:35] [debug] Run time error 4: "Array index out of bounds"
[02:41:35] [debug]  Accessing element at index 200 past array upper bound 199
[02:41:35] [debug] AMX backtrace:
[02:41:35] [debug] #0 0000356c in public CheckPlayerState () from driftpoints.amx
Код:
public CheckPlayerState(){ //by Abhinav
	new i,cs;

	for(i=0;i<=MAX_PLAYERS;i++){
	if(i != INVALID_PLAYER_ID)
	{
	    cs=GetPlayerState(i);
	    if(DriftMode[i] && cs==PLAYER_STATE_DRIVER && DriftPointsNow[i]>70){
	        new Float:h;
	        GetVehicleHealth(GetPlayerVehicleID(i),h);
	        if(h<HealthInit[i]){
	        	KillTimer(DriftTimer[i]);
	        	DriftExit(i);
	        	GameTextForPlayer(i,"~n~~n~~n~~n~~n~~n~~n~~r~Boom",800,5);
	        	DriftMode[i]=false;
			}
		}
	    if(cs==PLAYER_STATE_DRIVER && DriftMode[i]==false){
	        if(GetVType(GetPlayerVehicleID(i))){
		        DriftMode[i]=true;
		        GetVehicleHealth(GetPlayerVehicleID(i),HealthInit[i]);
		        AutoFixBool[i]=false;
		        DriftTimer[i]=SetTimerEx("Drift", 200, true, "i", i);
			}
		}
		else if(cs!=PLAYER_STATE_DRIVER && DriftMode[i]==true){
		    KillTimer(DriftTimer[i]);
		    DriftMode[i]=false;
		    AutoFixBool[i]=true;
		}
		else{}
		}
	}
	return 1;
}

public AutoFix(){ // By abhinav
	new i;
	for(i=0;i<=MAX_PLAYERS;i++){
	if(i != INVALID_PLAYER_ID)
	{
		if(AutoFixBool[i] && IsPlayerInAnyVehicle(i)){
			SetVehicleHealth(GetPlayerVehicleID(i),HealthInit[i]);
		}
 	}
	}
}



Re: Run time error 4: "Array index out of bounds" [+REP] - BroZeus - 19.07.2014

make sure the arrays containing "i" in them are defined as below --
AutoFixBool[MAX_PLAYERS]
DriftMode[MAX_PLAYERS]......
and so on others also like this

i recommend you using enums for storing MAX_PLAYERS things


Re: Run time error 4: "Array index out of bounds" [+REP] - MehranGta - 19.07.2014

Check that AutoFixBool and DriftMode are defined as a bool or not.


Re: Run time error 4: "Array index out of bounds" [+REP] - Konstantinos - 19.07.2014

When you declare an array with size of MAX_PLAYERS, the valid bounds are between 0 and MAX_PLAYERS-1. So using "<=" instead of "<" or "!=" will give the run time error 4.

Replace every player loop from:
pawn Код:
for(i=0;i<=MAX_PLAYERS;i++)
to:
pawn Код:
// correct:
for(i=0;i<MAX_PLAYERS;i++)
and also a suggestion - use foreach for player loops. It's much faster!


Re: Run time error 4: "Array index out of bounds" [+REP] - driftpower - 19.07.2014

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
When you declare an array with size of MAX_PLAYERS, the valid bounds are between 0 and MAX_PLAYERS-1. So using "<=" instead of "<" or "!=" will give the run time error 4.

Replace every player loop from:
pawn Код:
for(i=0;i<=MAX_PLAYERS;i++)
to:
pawn Код:
// correct:
for(i=0;i<MAX_PLAYERS;i++)
and also a suggestion - use foreach for player loops. It's much faster!
That solved it, thank you for the explanation & helping out, I understand now.