Array index out of bounds - 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: Array index out of bounds (
/showthread.php?tid=363292)
Array index out of bounds -
maramizo - 27.07.2012
pawn Код:
public SaveAccount(playername[])
{
new string3[32];
//GetPlayerName(MAX_PLAYERS, playername, MAX_PLAYER_NAME);
format(string3, sizeof(string3), "users/%s.ini", playername);
new File: hFile = fopen(string3, io_write);
if (hFile)
{
new var[156];
format(var, 32, "BT=%d\n",BT[MAX_PLAYERS]);fwrite(hFile, var);
fclose(hFile);
}
return 1;
}
returns error:
Quote:
error 032: array index out of bounds (variable "BT")
|
Re: Array index out of bounds -
MP2 - 27.07.2012
What is the size of BT? I assume it's MAX_PLAYERS. Therefore you can't access element 'MAX_PLAYERS' - because the first element index is 0, so if MAX_PLAYERS is 69 you can only access array indexes from 0 to 68.
Re: Array index out of bounds -
Goobiiify - 27.07.2012
You're outside the bounds of the BT[MAX_PLAYERS]. Simple put [MAX_PLAYERS] on where you have defined the array.
Re: Array index out of bounds -
grand.Theft.Otto - 27.07.2012
Make sure when you create global variables, MAX_PLAYERS is out of a callback, so when you use that variable later on, you can use the playerid inside of the variable. Example:
pawn Код:
// top of script
new BT[MAX_PLAYERS];
// in a command you would use it like this for example:
BT[playerid]
So basically, you need to change BT[MAX_PLAYERS] to BT[playerid]
Re: Array index out of bounds -
maramizo - 27.07.2012
The way to fix it is to change BT[MAX_PLAYERS] to BT[MAX_PLAYERS-1] on the save part.
Thank you all for attempting to solve it.
Re: Array index out of bounds -
iggy1 - 27.07.2012
Quote:
Originally Posted by maramizo
The way to fix it is to change BT[MAX_PLAYERS] to BT[MAX_PLAYERS-1] on the save part.
Thank you all for attempting to solve it.
|
That will stop the error but your code will not do what you want it to do. Whatever BT is it will be the same for every player because you only ever use MAX_PLAYERS-1 as an index and not the playerid. So you might aswel use a normal var (none array) since you only ever use one index.