SA-MP Forums Archive
foreach help, out of bounds 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: foreach help, out of bounds error. (/showthread.php?tid=467670)



foreach help, out of bounds error. - Richie© - 04.10.2013

I am using foreach in advancespectate code, and on occations it gives OOB error.
pawn Код:
[10:30:19] [debug] Run time error 4: "Array index out of bounds"
[10:30:19] [debug]  Accessing element at index 111 past array upper bound 110
[10:30:19] [debug] AMX backtrace:
[10:30:19] [debug] #0 00223164 in public AdvanceSpectate (playerid=15) at C:\Users\me\Desktop\Dev\gamemodes\sa-tdm_7.004.pwn:22989
Here is the code and i cant seem to figure out why, when i tested alot without getting error. But after some uptime, there it is, run time error.
Im using y_iterate 20, MAX_PLAYERS = 110 and here is code;

pawn Код:
if(Iter_Last(Character) == playerid) last = Iter_Prev(Character, playerid); // if playerid is the last id, the prev id will be set as last
else last = Iter_Last(Character); // if not playerid is last, use the actual last id
if(gSpectateID[playerid] == last) gSpectateID[playerid] = Iter_First(Character); // if player was spectating last player, set to first.
for(new i = gSpectateID[playerid]; (i = Iter_Next(Character, i)) != Iter_End(Character); ) // OOB :(



Re: foreach help, out of bounds error. - Dragonsaurus - 04.10.2013

Can you show us the line 22989 please?
Could be something regarding MAX_PLAYERS I think.


Re: foreach help, out of bounds error. - Richie© - 04.10.2013

Line 22989 is there, commented with OOB


Re: foreach help, out of bounds error. - Richie© - 05.10.2013

Bump


Re: foreach help, out of bounds error. - ]Rafaellos[ - 05.10.2013

You actually set i 2 times on the loop, the first one with gSpectateID[playerid]; and then with Iter_Next(Character, i). You also don't increase i number at the end of the loop..


Re: foreach help, out of bounds error. - Richie© - 05.10.2013

Quote:
Originally Posted by ]Rafaellos[
Посмотреть сообщение
You actually set i 2 times on the loop, the first one with gSpectateID[playerid]; and then with Iter_Next(Character, i). You also don't increase i number at the end of the loop..
The thing is working, its based of the internal foreach macro:
pawn Код:
for (new i = Iter_Begin(Some); (i = Iter_Next(Some, i)) != Iter_End(Some); )
Instead of using Iter_Begin, my loop starts at the ID player was spectating.
The problem is that sometimes it gives out of bound error.
But thanks for trying..