SA-MP Forums Archive
How to make if(pickupid == 1) compact? - 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: How to make if(pickupid == 1) compact? (/showthread.php?tid=498111)



How to make if(pickupid == 1) compact? - rangerxxll - 02.03.2014

I currently have 19 pickups which I'm looking to add. Upon entering the pickups, players will receive a random reward. (Based on another stock not relevant.) I don't want to continuously do this:

pawn Код:
if(pickupid == 1)
{

}
if(pickupid == 2)
{

}
if(pickupid == 3)
{

}
And so on. So I attempted to do this: (Which doesn't give random weapons upon entering it.)

pawn Код:
//random pickups
    if(pickupid == mpickup1 && mpickup2 && mpickup3 && mpickup4 && mpickup5 && mpickup6 && mpickup7 && mpickup8
     && mpickup9 && mpickup10 && mpickup11 && mpickup12 && mpickup13 && mpickup14 && mpickup15 && mpickup16 && mpickup17
     && mpickup18 && mpickup19)
    {
        GiveRandomWeapon(playerid);
    }
What's the best way to do this? Thank you. And if you need my stock, here you go.

pawn Код:
forward GiveRandomWeapon(playerid);
public GiveRandomWeapon(playerid)
{
    new rand = random(10);
    switch(rand)
    {
        case 0: GivePlayerWeapon(playerid, 4, 1) && SendClientMessage(playerid,COLOR_GREEN, "You've received a knife.");
        case 1: GivePlayerWeapon(playerid, 24, 10) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Desert Eagle");
        case 2: GivePlayerWeapon(playerid, 25, 10) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Shotgun");
        case 3: GivePlayerWeapon(playerid, 30, 25) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Ak47");
        case 4: GivePlayerWeapon(playerid, 33, 10) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Country Rifle");
        case 5: GivePlayerWeapon(playerid, 34, 10) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Sniper Rifle");
        case 6: GivePlayerWeapon(playerid, 23, 15) && SendClientMessage(playerid,COLOR_GREEN, "You've received a Silenced Pistol");
    }
}



Re: How to make if(pickupid == 1) compact? - Scenario - 02.03.2014

Use a switch().

pawn Код:
switch(some_number) // basically: if(some_number)
{
    case -1: // basically: if(some_number == -1)
    {
        // do something
    }
    case 0 .. 19: // 0 = the lowest value of some_number    19 = the highest value of some_number   .. means all of the numbers in between the two
    {
        // do something
    }
    default: // basically: "else"
    {
        // do something
    }
}



Re: How to make if(pickupid == 1) compact? - newbienoob - 02.03.2014

if(pickupid == mpickup1 && mpickup2 && mpickup3 && mpickup4 && mpickup5 && mpickup6 && mpickup7 && mpickup8
&& mpickup9 && mpickup10 && mpickup11 && mpickup12 && mpickup13 && mpickup14 && mpickup15 && mpickup16 && mpickup17
&& mpickup18 && mpickup19)

replace && with ||


Re: How to make if(pickupid == 1) compact? - rangerxxll - 02.03.2014

I've never fully understood the uses of switches and cases. How are cases faster than if statements and couldn't you do something similar in the if statements? if
pawn Код:
(pickupid == 0-19)
for example.

I'm eager to learn more. Thanks for the assistance.


Re: How to make if(pickupid == 1) compact? - Scenario - 02.03.2014

I'm not quite sure why they are faster, tbh. Although, I'm thinking it has something to do with only doing one "if" statement, and then kind of keeping a quick loop open or something rather than having to process dozens of if's.

As for using if(pickupid == 0-19), it simply won't work. You should avoid using if-statements wherever possible though.


Re: How to make if(pickupid == 1) compact? - rangerxxll - 02.03.2014

I'm still in the process of switching up the code, and it simply comes up with 19 warnings, saying mpickup's are not used. How could I make this proper?

pawn Код:
switch(pickupid) // basically: if(some_number)
    {
        case -1: // basically: if(some_number == -1)
        {
            // do something
        }
        case 0 .. 19: // 0 = the lowest value of some_number    19 = the highest value of some_number   .. means all of the numbers in between the two
        {
            GiveRandomWeapon(playerid);
        }
        default: // basically: "else"
        {
            // do something
        }
    }



Re: How to make if(pickupid == 1) compact? - Scenario - 02.03.2014

Ah yes, I forgot to mention that you'll need to make mpickup an array for this to work properly. Basically, when declaring the variable do this:

pawn Код:
new mpickup[MAX_VALUES+1]; // for this, MAX_VALUES+1 is 20
Then when you go and define what "mpickup#" is, make it "mpickup[#]", starting with 0 as the first one. Hopefully that makes sense.


Re: How to make if(pickupid == 1) compact? - newbienoob - 02.03.2014

Afaik, switch can only be used with integer.
pawn Код:
switch(reputation) //since "reputation" is an integer, you can use it with switch
{
    case 0: //it's same as if(reputation == 0)
}

//you can't do this
switch(pickupid)
{
    mypickup:
}
//since pickupid is not an integer.



Re: How to make if(pickupid == 1) compact? - Scenario - 02.03.2014

Quote:
Originally Posted by newbienoob
Посмотреть сообщение
since pickupid is not an integer.
Uh... yeah it is! The same goes for vehicle ID's, player ID's, checkpoint ID's... they're all numbers.

#e: @OP: Where you have this:

pawn Код:
case 0 .. 19:
When you replace the mpickup1 through mpickup19 with the array-style I mentioned, you'll need to replace 0 .. 19 with mpickup[0] .. mpickup[19].


Re: How to make if(pickupid == 1) compact? - rangerxxll - 02.03.2014

Yea, still not following. Would I need to define the maximum pickups?
pawn Код:
#define MAX_PICKUPS 50
new mpickups[1];
new mpickups[2]; //etc.