SA-MP Forums Archive
Array index out of bonus - 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 bonus (/showthread.php?tid=646150)



Array index out of bonus - None1337 - 11.12.2017

Hello guys, i have a war system on my server and when i am not in a turf and i use /attack (to attack even i'm not in a turf) i get SERVER: Unknown command and in server_log this error:
Код:
[15:11:29] [debug] Run time error 4: "Array index out of bounds"
[15:11:29] [debug] AMX backtrace:
[15:11:29] [debug] #0 0016b1b8 in public IsPlayerInTurf (playerid=0, turfid=37) at D:\gamemode\gamemodes\exgaming.pwn:22304
[15:11:29] [debug] #1 001af6f4 in public cmd_attack (playerid=0, params[]=@01560ffc "") at D:\gamemode\gamemodes\exgaming.pwn:26674
[15:11:29] [debug] #2 native CallLocalFunction () from samp03svr
[15:11:29] [debug] #3 0003c25c in public OnPlayerCommandText (playerid=0, cmdtext[]=@01560fdc "/attack") at D:\gamemode\pawno\include\zcmd.inc:102
On my database I have a table with all the turfs starting from 1 to 36 (inclusive 36).

Variables:
Код:
new TurfInfo[37][zInfo],
	Turfs[37];
Line problem:

Код:
public IsPlayerInTurf(playerid, turfid)
{
	if(IsPlayerConnected(playerid))
	{
		if(turfid == -1)
		{
			return 0;
		}
		new Float:x, Float:y, Float:z;
		GetPlayerPos(playerid,x,y,z);
		if(x >= TurfInfo[turfid][zMinX] && x < TurfInfo[turfid][zMaxX] && y >= TurfInfo[turfid][zMinY] && y < TurfInfo[turfid][zMaxY]) //line 26674
		{
	 		return 1;
		}
	}
	return 0;
}



Re: Array index out of bonus - Kane - 11.12.2017

Код:
public IsPlayerInTurf (playerid=0, turfid=37)
Your array size is 37.


Re: Array index out of bonus - None1337 - 11.12.2017

Quote:
Originally Posted by Arthur Kane
Посмотреть сообщение
Код:
public IsPlayerInTurf (playerid=0, turfid=37)
Your array size is 37.
So, what should I do, to put 38?


Re: Array index out of bonus - Kane - 11.12.2017

Arrays start at 0. You've got something misunderstood. Set the size to 39 if you have 38.


Re: Array index out of bonus - None1337 - 11.12.2017

Quote:
Originally Posted by Arthur Kane
Посмотреть сообщение
Arrays start at 0. You've got something misunderstood. Set the size to 39 if you have 38.
I have
Код:
new TurfInfo[37][zInfo],
	Turfs[37];
And like this in database https://i.imgur.com/WUGkCjv.png

These are all my 36 turfs . So, what number should I replace with 37 to solve?


Re: Array index out of bonus - DRIFT_HUNTER - 11.12.2017

pawn Код:
public IsPlayerInTurf(playerid, turfid)
{
    if(!IsPlayerConnected(playerid))
        return 0;
    if(turfid == -1 || turfid >= 37)
        return 0;
   
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid,x,y,z);
    if(x >= TurfInfo[turfid][zMinX] && x < TurfInfo[turfid][zMaxX] && y >= TurfInfo[turfid][zMinY] && y < TurfInfo[turfid][zMaxY])
        return 1;
}
All i did was added check if turfid is greater or equal to 37 (that is if is out of bounds)
I wold recommend you to change these 37 with macro like MAX_TURFS or something similar


Re: Array index out of bonus - None1337 - 11.12.2017

Quote:
Originally Posted by DRIFT_HUNTER
Посмотреть сообщение
pawn Код:
public IsPlayerInTurf(playerid, turfid)
{
    if(!IsPlayerConnected(playerid))
        return 0;
    if(turfid == -1 || turfid >= 37)
        return 0;
   
    new Float:x, Float:y, Float:z;
    GetPlayerPos(playerid,x,y,z);
    if(x >= TurfInfo[turfid][zMinX] && x < TurfInfo[turfid][zMaxX] && y >= TurfInfo[turfid][zMinY] && y < TurfInfo[turfid][zMaxY])
        return 1;
}
All i did was added check if turfid is greater or equal to 37 (that is if is out of bounds)
I wold recommend you to change these 37 with macro like MAX_TURFS or something similar
Thanks, is working now!