array index out of bounds
#1

Hi, on my thread I was suggested to use an enum instead. So I've tried this,
Код:
enum prices
{
	colt = 150,
	deagle = 300,
	pumpshotgun = 350,
	combatshotgun = 500,
	mp5 = 420,
	tec9 = 400,
	ak47 = 700,
	m4 = 700,
	sniper = 1000,
	countryrifle = 850,
	armour = 500,
	parachute = 210
}

new WPrices[prices];
Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    new buylist1[100],buylist2[100],buylist3[100],buylist4[100],buylist5[100],buylist6[100];
    format(buylist1,sizeof(buylist1),"Colt-45 {4ECC47}[$%i]\nDesert Eagle {4ECC47}[$%i]",colt,deagle);
    format(buylist2,sizeof(buylist2),"Pump Shotgun {4ECC47}[$%i]\nCombat Shotgun {4ECC47}[$%i]",pumpshotgun,combatshotgun);
    format(buylist3,sizeof(buylist3),"MP5 {4ECC47}[$%i]\nTec-9 {4ECC47}[$%i]",mp5,tec9);
    format(buylist4,sizeof(buylist4),"AK47 {4ECC47}[$%i]\nM4 {4ECC47}[$%i]",ak47,m4);
    format(buylist5,sizeof(buylist5),"Sniper Rifle {4ECC47}[$%i]\nCountry Rifle {4ECC47}[$%i]",sniper,countryrifle);
    format(buylist6,sizeof(buylist6),"Armour {4ECC47}[$%i]\nParachute {4ECC47}[$%i]",armour,parachute);
	if(dialogid == 0)
	{
	    if(response)
	    {
     		switch(listitem)
            {
                case 0: ShowPlayerDialog(playerid, 1, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Pistols",buylist1,"Buy","");
                case 1: ShowPlayerDialog(playerid, 2, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Shotguns",buylist2,"Buy","");
                case 2: ShowPlayerDialog(playerid, 3, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Submachine gun",buylist3,"Buy","");
                case 3: ShowPlayerDialog(playerid, 4, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Assault",buylist4,"Buy","");
                case 4: ShowPlayerDialog(playerid, 5, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Rifles",buylist5,"Buy","");
                case 5: ShowPlayerDialog(playerid, 6, DIALOG_STYLE_LIST, "{008CFF}Weapon Shop - Extras",buylist6,"Buy","");
            }
	    }
	}
	if(dialogid == 1)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[colt])
					{
				        GivePlayerMoney(playerid, -WPrices[colt]);
						GivePlayerWeapon(playerid, WEAPON_COLT45, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[deagle])
					{
				        GivePlayerMoney(playerid, -WPrices[deagle]);
						GivePlayerWeapon(playerid, WEAPON_DEAGLE, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	if(dialogid == 2)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[pumpshotgun])
					{
				        GivePlayerMoney(playerid, -WPrices[pumpshotgun]);
						GivePlayerWeapon(playerid, WEAPON_SHOTGUN, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[combatshotgun])
					{
				        GivePlayerMoney(playerid, -WPrices[combatshotgun]);
						GivePlayerWeapon(playerid, WEAPON_SHOTGSPA, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	if(dialogid == 3)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[mp5])
					{
						GivePlayerMoney(playerid, -WPrices[mp5]);
						GivePlayerWeapon(playerid, WEAPON_MP5, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[tec9])
					{
						GivePlayerMoney(playerid, -WPrices[tec9]);
						GivePlayerWeapon(playerid, WEAPON_TEC9, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	if(dialogid == 4)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[ak47])
					{
						GivePlayerMoney(playerid, -WPrices[ak47]);
						GivePlayerWeapon(playerid, WEAPON_AK47, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[m4])
					{
						GivePlayerMoney(playerid, -WPrices[m4]);
						GivePlayerWeapon(playerid, WEAPON_M4, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	if(dialogid == 5)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[sniper])
					{
						GivePlayerMoney(playerid, -WPrices[sniper]);
						GivePlayerWeapon(playerid, WEAPON_SNIPER, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[countryrifle])
					{
						GivePlayerMoney(playerid, -WPrices[countryrifle]);
						GivePlayerWeapon(playerid, WEAPON_RIFLE, 99999);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	if(dialogid == 6)
	{
    	if(response)
	    {
     		switch(listitem)
			{
			    case 0:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[armour])
					{
						GivePlayerMoney(playerid, -WPrices[armour]);
						SetPlayerArmour(playerid, 100);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			    case 1:
			    {
					if(GetPlayerMoney(playerid) >= WPrices[parachute])
					{
						GivePlayerMoney(playerid, -WPrices[parachute]);
						GivePlayerWeapon(playerid, WEAPON_PARACHUTE, 1);
					}else SendClientMessage(playerid, -1, "{FFFF00}[Info] {808080}You do not have enough money.");
			    }
			}
		}
	}
	return 1;
}
I'm getting the error
Quote:

-- : error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")--: error 032: array index out of bounds (variable "WPrices")-- : error 032: array index out of bounds (variable "WPrices")
--(45 : error 032: array index out of bounds (variable "WPrices")

I don't reguarly use enums so I'm not quite sure what I'm doing wrong here. Any help appreciated.
Reply
#2

I need help as this is stumpting the development of my script. I will +rep anyone who can sort this.
Reply
#3

pawn Код:
enum prices
{
    colt = 150,
    deagle = 300,
    pumpshotgun = 350,
    combatshotgun = 500,
    mp5 = 420,
    tec9 = 400,
    ak47 = 700,
    m4 = 700,
    sniper = 1000,
    countryrifle = 850,
    armour = 500,
    parachute = 210
}

new WPrices[prices];
rewrite to:
pawn Код:
enum prices
{
    coltm
    deaglem
    pumpshotgunm
    combatshotgunm
    mp5m
    tec9m
    ak47m
    m4m
    sniperm
    countryriflem
    armourm
    parachute
}

new WPrices[prices] = {
    150,
    300,
    350,
    500,
    420,
    400,
    700,
    700,
    1000,
    850,
    500,
    210
};
and this
pawn Код:
format(buylist1,sizeof(buylist1),"Colt-45 {4ECC47}[$%i]\nDesert Eagle {4ECC47}[$%i]",colt,deagle);
rewrite to:
pawn Код:
format(buylist1,sizeof(buylist1),"Colt-45 {4ECC47}[$%i]\nDesert Eagle {4ECC47}[$%i]",WPrices[colt],WPrices[deagle]);
etc...
Reply
#4

Quote:
Originally Posted by ATomas
Посмотреть сообщение
pawn Код:
enum prices
{
    colt = 150,
    deagle = 300,
    pumpshotgun = 350,
    combatshotgun = 500,
    mp5 = 420,
    tec9 = 400,
    ak47 = 700,
    m4 = 700,
    sniper = 1000,
    countryrifle = 850,
    armour = 500,
    parachute = 210
}

new WPrices[prices];
rewrite to:
pawn Код:
enum prices
{
    coltm
    deaglem
    pumpshotgunm
    combatshotgunm
    mp5m
    tec9m
    ak47m
    m4m
    sniperm
    countryriflem
    armourm
    parachute
}

new WPrices[prices] = {
    150,
    300,
    350,
    500,
    420,
    400,
    700,
    700,
    1000,
    850,
    500,
    210
};
and this
pawn Код:
format(buylist1,sizeof(buylist1),"Colt-45 {4ECC47}[$%i]\nDesert Eagle {4ECC47}[$%i]",colt,deagle);
rewrite to:
pawn Код:
format(buylist1,sizeof(buylist1),"Colt-45 {4ECC47}[$%i]\nDesert Eagle {4ECC47}[$%i]",WPrices[colt],WPrices[deagle]);
etc...
Can you please explain why I need to make 2 sections, I'm trying to learn how I can combat the error in the future.
Reply
#5

I've had this topic open for about half an hour now. I am still trying to figure out why someone advised you to use an enum for this. Constants take up less code and are a lot easier to write out. Enums on the other hand need writing and assigning in two different pieces of code and another variable so it clogs up your code.

I would go against this advice to use an enum and just resort to constants so that your code is easier to read and maintain.
Reply
#6

You maybe help this: https://sampwiki.blast.hk/wiki/Enum
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)