Switch & case question
#1

As I am developing a gamemode I want to make it faster and create as much as case's except if and else statements. Although there is a problem occuring, and I'm not sure how to fix it, I'm also not sure if I can use this in a case.
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
    {
        if (newkeys & KEY_NO)
         {
                   switch(LootObjects[MAX_LOOTS])
                  {
                case 0:
                {
                    if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
                    {
                        AddItem(playerid,"Dead Deer", 1);
                        SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                        DestroyObject(LootObjects[0]);
                    }
                }
                }
        return 0;
    }
This error seems to show up:
Код:
E:\SAMP\RPG all\RPG\gamemodes\rc-apo.pwn(511) : error 032: array index out of bounds (variable "LootObjects")
If somebody could help that would be great, as this is a great way to speed up my slow script.
The script looks a bit messed up although it looks good in pawno itself.
Reply
#2

Err dat intendation doe...
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if (newkeys & KEY_NO)
    {
        switch(LootObjects[MAX_LOOTS])
        {
            case 0:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[0]);
                }
            }
        }
    }
    return 0;
}
Lol you missed a bracket always keep your code neat so you wont have any errors like this.
edit: I noticed you used my tutorial for loot !
Reply
#3

EDIT: Nvm, I noticed Voxel gave you a solution :P
Reply
#4

Quote:
Originally Posted by Voxel
Посмотреть сообщение
Err dat intendation doe...
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if (newkeys & KEY_NO)
    {
        switch(LootObjects[MAX_LOOTS])
        {
            case 0:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[0]);
                }
            }
        }
    }
    return 0;
}
Lol you missed a bracket always keep your code neat so you wont have any errors like this.
edit: I noticed you used my tutorial for loot !
I used yours and modified it a lot

Код:
switch(LootObjects[500])
Changed it to this, and added a bracket although the error still appears.
Код:
		if (newkeys & KEY_NO)
		{
			switch(LootObjects[500])
			{
				case 0:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
					{
						AddItem(playerid,"Dead Deer", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
						DestroyObject(LootObjects[0]);
					}
				}
				case 1:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,242.75516,4239.82813,8.84726))
					{
						AddItem(playerid,"Dead Deer", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
						DestroyObject(LootObjects[1]);
					}
				}
				case 2:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,225.52122, 4161.61670, 8.79168))
					{
						AddItem(playerid,"Bandage", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_SMALLMEDKIT);
						DestroyObject(LootObjects[2]);
					}
				}
				case 3:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,644.18268, 4754.36621, 8.81872))
					{
						AddItem(playerid,"Large Water Bottle - Full", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_WATERBOTTLE);
						DestroyObject(LootObjects[3]);
					}
				}
			}
		}
Reply
#5

Quote:
Originally Posted by [WA]iRonan
Посмотреть сообщение
I used yours and modified it a lot

Код:
switch(LootObjects[500])
Changed it to this, and added a bracket although the error still appears.
Код:
		if (newkeys & KEY_NO)
		{
			switch(LootObjects[500])
			{
				case 0:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
					{
						AddItem(playerid,"Dead Deer", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
						DestroyObject(LootObjects[0]);
					}
				}
				case 1:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,242.75516,4239.82813,8.84726))
					{
						AddItem(playerid,"Dead Deer", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
						DestroyObject(LootObjects[1]);
					}
				}
				case 2:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,225.52122, 4161.61670, 8.79168))
					{
						AddItem(playerid,"Bandage", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_SMALLMEDKIT);
						DestroyObject(LootObjects[2]);
					}
				}
				case 3:
				{
					if(IsPlayerInRangeOfPoint(playerid,2.0,644.18268, 4754.36621, 8.81872))
					{
						AddItem(playerid,"Large Water Bottle - Full", 1);
						SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_WATERBOTTLE);
						DestroyObject(LootObjects[3]);
					}
				}
			}
		}
In that case, try switch(LootObjects[499]) :P If MAX_LOOT is 500, you can't use [500]. It's range would be 0 - 499.
Reply
#6

Quote:
Originally Posted by Jstylezzz
Посмотреть сообщение
In that case, try switch(LootObjects[499]) :P If MAX_LOOT is 500, you can't use [500]. It's range would be 0 - 499.
Thank you, worked. I realized that 0 is actually a dummy at many scripts, so I will use it as a dummy too.
Reply
#7

pawn Код:
if (newkeys & KEY_NO)
{
    for(new i = 0; i != MAX_LOOTS; i++)
    {
        switch(i)
        {
            case 0:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[0]);
                }
            }
            case 1:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,242.75516,4239.82813,8.84726))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[1]);
                }
            }
            case 2:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,225.52122, 4161.61670, 8.79168))
                {
                    AddItem(playerid,"Bandage", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_SMALLMEDKIT);
                    DestroyObject(LootObjects[2]);
                }
            }
            case 3:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,644.18268, 4754.36621, 8.81872))
                {
                    AddItem(playerid,"Large Water Bottle - Full", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_WATERBOTTLE);
                    DestroyObject(LootObjects[3]);
                }
            }
        }
    }
}
Reply
#8

Quote:
Originally Posted by Riddick94
Посмотреть сообщение
pawn Код:
if (newkeys & KEY_NO)
{
    for(new i = 0; i != MAX_LOOTS; i++)
    {
        switch(i)
        {
            case 0:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,254.02530, 4084.52393, 0.42032))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[0]);
                }
            }
            case 1:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,242.75516,4239.82813,8.84726))
                {
                    AddItem(playerid,"Dead Deer", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_DEAD);
                    DestroyObject(LootObjects[1]);
                }
            }
            case 2:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,225.52122, 4161.61670, 8.79168))
                {
                    AddItem(playerid,"Bandage", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_SMALLMEDKIT);
                    DestroyObject(LootObjects[2]);
                }
            }
            case 3:
            {
                if(IsPlayerInRangeOfPoint(playerid,2.0,644.18268, 4754.36621, 8.81872))
                {
                    AddItem(playerid,"Large Water Bottle - Full", 1);
                    SendClientMessage(playerid, COLOR_GREEN, TEXT_INV_WATERBOTTLE);
                    DestroyObject(LootObjects[3]);
                }
            }
        }
    }
}
Too advanced, it's solved already!
Reply
#9

Wait a sec.. I think Riddick is right. Pretty sure actually. What you do now, is when the player presses the 'Y' button, you check slot 499 of LootObjects. With Riddick's code, you'll loop trough all the objects, to see if that's the object you're currently trying to loot.
Reply
#10

Quote:
Originally Posted by Jstylezzz
Посмотреть сообщение
Wait a sec.. I think Riddick is right. Pretty sure actually. What you do now, is when the player presses the 'Y' button, you check slot 499 of LootObjects. With Riddick's code, you'll loop trough all the objects, to see if that's the object you're currently trying to loot.
Wait he is, thanks Riddick, it works now!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)