SA-MP Forums Archive
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=628830)



Array index out of bounds. - danielpalade - 15.02.2017

-- DELETED --


Re: Array index out of bounds. - Rdx - 15.02.2017

Any details? Which array?


Re: Array index out of bounds. - Freedom. - 15.02.2017

If you are using YSI use iterator too.

Код:
stock GetRandomPlayer()
{
	my_label:
	new id = Iter_Random(Player);
	
	if (pInfo[id][pMember] == 8 && pInfo[id][pScore] < 3 && pInfo[id][aVar][23] != 0)
	{
		goto my_label
	}
	return id;
}



Re: Array index out of bounds. - danielpalade - 15.02.2017

Quote:
Originally Posted by Rdx
Посмотреть сообщение
Any details? Which array?
I have no clue. I think it happens when there are no users online, so the variable users returns 0, but when the timer check for an ID, it see ID 0 from variable users, thus it gives arrat index out of bounds in my pInfo because the player doesn't exist.


Re: Array index out of bounds. - danielpalade - 16.02.2017

Bump!


Re: Array index out of bounds. - JesterlJoker - 16.02.2017

the logs could have helped you the most.

Show us the logs and people will be able to help more.

Quote:
Originally Posted by danielpalade
Посмотреть сообщение
I have no clue. I think it happens when there are no users online, so the variable users returns 0, but when the timer check for an ID, it see ID 0 from variable users, thus it gives array index out of bounds in my pInfo because the player doesn't exist.
arrays would not go index out of bounds on MAX_PLAYERS it returns 0 to infinity

Try using GetPlayerPoolSize() rather than MAX_PLAYERS from now on, it's much better and more complex MAX_PLAYERS already.

EDIT:
Quote:
Originally Posted by danielpalade
Посмотреть сообщение
I'm getting the error Array index out of bounds with this code:

Код:
#include <YSI\y_timers>

stock GetRandomPlayer()
{
	new users[MAX_PLAYERS], pl = 0;
	for(new i; i < MAX_PLAYERS; i++)
	{
		if(IsPlayerConnected(i) && pInfo[i][pMember] != 8 && pInfo[i][pScore] >= 3 && pInfo[i][aVar][23] == 0)
		{
			users[pl] = i;
			pl++;
		}
	}
	return users[random(pl)];
}

task AutoContracts[180000]()
{
	new string[128], amount = RandomEx(6000, 30000), id = GetRandomPlayer();
	if(IsPlayerConnected(id))
	{
		pInfo[id][aVar][23] += amount;
		mysql_format(MySQLCon, quMYSQL, 200, "UPDATE `players` SET `Contract`=%d WHERE `ID`=%d", pInfo[id][aVar][23], pInfo[id][pID]), mysql_tquery(MySQLCon, quMYSQL);

		format(string, sizeof(string), "HITMAN: A new hit is available. Name: %s | Amount: %s$, (/hits).", GetName(id), FormatNumber(pInfo[id][aVar][23]));
	    SendTeamMessage(8, COLOR_HITMAN, string);
	}
    return 1;
}
also rather than using for(new i; i < MAX_PLAYERS; i++)
foreach(new i : Player) was born to be used by everyone
if you don't have foreach then
you can use for(new i; i < GetPlayerPoolSize(); i++)
just like what I said earlier


Re: Array index out of bounds. - Jefff - 16.02.2017

if there is no players online random(0) returns huge numbers
change to
pawn Код:
return users[((pl < 2) ? 0 : random(pl))];