Array index out of bounds.
#1

I'm getting "Array index out of bounds" in crashdetect due to this command:

Код:
CMD:getgift(playerid, params[])
{
    if(playerVariables[playerid][pPlayingHours] >= 3)
    {
        if(boxVariables[1][boxStatus] == 1)
        {
            if(IsPlayerInRangeOfPoint(playerid, 5.0, boxVariables[1][boxX], boxVariables[1][boxY], boxVariables[1][boxZ]))
            {
                format(szMessage, 256, "Poti folosi /getgift peste %d ore.", playerVariables[playerid][pGiftHours]);
                /*if(playerVariables[playerid][pGiftHours] == 0)
                {*/
					new string[256];
                    new gift = random(30);
					new count, boxes[50];
					if(gift >= 0 && gift < 2 && boxVariables[1][boxVehicle] == 0) gift = random(30);
					if(boxVariables[1][boxVehicle] == 1) { boxes[count] = 1; count++; }	
					else if(boxVariables[1][boxPP] == 1) { boxes[count] = 4; count += 3; }	
					else if(boxVariables[1][boxHidden] == 1) { boxes[count] = 8; count += 4; }
					else if(boxVariables[1][boxLic] == 1) { boxes[count] = 12; count += 4; }					
					else if(boxVariables[1][boxMoney] == 1) { boxes[count] = 18; count += 6; }	
					else if(boxVariables[1][boxFireWorks] == 1) { boxes[count] = 24; count += 6; }	
					else if(boxVariables[1][boxClearWarns] == 1 && playerVariables[playerid][pWarns] > 0) { boxes[count] = 27; count += 3; }	
					else if(boxVariables[1][boxMats] == 1) { boxes[count] = 33; count += 6; }	
					switch(boxes[random(count)])
					{
						case 0, 1:
						{
							format(string, sizeof(string), "GiftBox: %s a castigat un %s din giftbox. Felicitari!", GetName(playerid), VehicleNames[boxVariables[1][boxVehicle] - 400]);
							SendClientMessageToAll(COLOR_LIGHT, string);
						}
						case 2, 3, 4:
						{
							new pp = 10 + random(50);
							format(string, sizeof(string), "GiftBox: %s a castigat %d puncte premium din giftbox. Felicitari!", GetName(playerid), pp);
							SendClientMessageToAll(COLOR_LIGHT, string);
						}						
						case 5, 6, 7, 8:
						{
							SendClientMessage(playerid, COLOR_YELLOW, "* Ai castigat o culoare hidden din giftbox!");
						}
						case 9, 10, 11, 12:
						{
							SendClientMessage(playerid, COLOR_YELLOW, "* Ai castigat taote licentele +200h la giftbox!");
						}
						case 13, 14, 15, 16, 17, 18:
						{
							new money = 200000 + random(1000000);
							format(string, sizeof(string), "* Ai castigat $%s din giftbox.", NumberFormat(money));
							SendClientMessage(playerid, COLOR_YELLOW, string);
						}			
						case 19, 20, 21, 22, 23, 24:
						{
							new fireworks = 10 + random(30);
							format(string, sizeof(string), "* Ai castigat %s artificii din giftbox.", NumberFormat(fireworks));
							SendClientMessage(playerid, COLOR_YELLOW, string);
						}
						case 25, 26, 27:
						{
							SendClientMessage(playerid, COLOR_YELLOW, "* Ai castigat clear warns la giftbox!");
						}
						case 28, 29, 30, 31, 32, 33:
						{
							new mats = 5000 + random(30000);
							format(string, sizeof(string), "* Ai castigat %s materiale din giftbox.", NumberFormat(mats));
							SendClientMessage(playerid, COLOR_YELLOW, string);
						}
					}
					printf("%d", gift);			
					playerVariables[playerid][pGiftHours] = 3;
              /* }
                else SCM(playerid,COLOR_YELLOW, szMessage);*/
            }
            else
            {
                SCM(playerid,COLOR_WHITE, "You are not at giftbox location. Go to checkpoint.");
                SetPlayerCheckpoint(playerid, 1129.31531, -1448.38330, 15.43760, 5.0);
            }
        }
        else SCM(playerid,COLOR_YELLOW,"The gift is not active.");
    }
    else SCM(playerid,COLOR_WHITE, "Ai nevoie de 3 ore jucate pentru a folosi comanda /getgift.");
    return 1;
}
How can I fix this?
I assume it's the variable boxes which is causing this.

EDIT:

I've compiled it with the -d3 function, and this is what I got.
Код:
[16:32:28] [debug] Run time error 4: "Array index out of bounds"
[16:32:28] [debug]  Accessing element at index 106304000 past array upper bound 49
[16:32:28] [debug] AMX backtrace:
[16:32:28] [debug] #0 00227990 in public cmd_getgift (playerid=0, params[]=@014ae214 "") at ../withal/w_giftbox.inc:276
[16:32:28] [debug] #1 native CallLocalFunction () from samp-server.exe
[16:32:28] [debug] #2 00008080 in public OnPlayerCommandText (playerid=0, cmdtext[]=@014ae1f0
So it has to do with this line:
Код:
switch(boxes[random(count)])
Reply
#2

If you don't accept help (possible solutions), then why do you even bother creating multiple threads?
Reply
#3

would help to know which line the error is on so we dont have to look at all this code -_-
Reply
#4

pawn Код:
VehicleNames[boxVariables[1][boxVehicle] - 400]
here's the issue, boxVariables[1][boxVehicle] is not a valid vehicle model ID.
Reply
#5

Quote:
Originally Posted by jamesbond007
Посмотреть сообщение
would help to know which line the error is on so we dont have to look at all this code -_-
I don't know which line it is.. As I said, I'm getting this error with crashdetect.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
pawn Код:
VehicleNames[boxVariables[1][boxVehicle] - 400]
here's the issue, boxVariables[1][boxVehicle] is not a valid vehicle model ID.
No, that's not the issue :/
Reply
#7

Quote:
Originally Posted by danielpalade
Посмотреть сообщение
I don't know which line it is.. As I said, I'm getting this error with crashdetect.
well its pretty obvious what the issue is after u did the crash detect..

random(count) is above 49 because count is probably a large number so its out of bounds

when u do new count u forgot to initialize it to 0.. or maybe its using some other count
u can also print count and see what the value is
Reply
#8

random(0) generates a value that big.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)