SA-MP Forums Archive
shorten a line like this? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: shorten a line like this? (/showthread.php?tid=241710)



shorten a line like this? - xir - 18.03.2011

pawn Код:
if(GetPlayerJobID(playerid) == 1 || GetPlayerJobID(playerid) == 2 || GetPlayerJobID(playerid) == 3 || GetPlayerJobID(playerid) == 4|| GetPlayerJobID(playerid) == 8 || GetPlayerJobID(playerid) == 9 || GetPlayerJobID(playerid) == 10 || GetPlayerJobID(playerid) == 11 || GetPlayerJobID(playerid) == 12 || GetPlayerJobID(playerid) == 13 || GetPlayerJobID(playerid) == 17 || GetPlayerJobID(playerid) == 18 || GetPlayerJobID(playerid) == 19 || GetPlayerJobID(playerid) == 20 || GetPlayerJobID(playerid) == 21) return 0;
how can I shorten this line, to a very good way?


Re: shorten a line like this? - Kyosaur - 18.03.2011

Use a switch statement, its very simple (scroll down to case, and it has EXACTLY what your looking for).

https://sampwiki.blast.hk/wiki/Control_Structures#switch_2


If you need further assistance, feel free to PM me and i'll help you more in-depth.


Re: shorten a line like this? - Calgon - 18.03.2011

pawn Код:
switch(GetPlayerJobID(playerid)) {
        case 1..21: {
            return 0;
        }
        default: return 1;
    }
Crap, beaten to it by Kyosaur.


Re: shorten a line like this? - xir - 18.03.2011

So I need to make case for each ID that cant use that cmd?


Re: shorten a line like this? - Norck - 18.03.2011

pawn Код:
switch(GetPlayerJobID(playerid))
{
    case 1..4, 8..13, 17..21: return 0;
}
Just like that.


Re: shorten a line like this? - Calgon - 18.03.2011

No, the two periods connect from the first number and go through to the last number, so

1..24

Anything in between 1 and 24 will be called for that case.

Follow the poster's solution above me ^ as you had IDs with gaps, his solution should work though.


Re: shorten a line like this? - legodude - 18.03.2011

pawn Код:
new Job=GetPlayerJobID(playerid);
if((Job >= 1 && Job <= 4) || (Job >= 8 && Job <= 13) || (Job >= 17 && Job <= 21))
maybe the case is faster. but i don't think so at this one...
someone test please?


Re: shorten a line like this? - Kyosaur - 18.03.2011

Quote:
Originally Posted by legodude
Посмотреть сообщение
pawn Код:
new Job=GetPlayerJobID(playerid);
if((Job >= 1 && Job <= 4) || (Job >= 8 && Job <= 13) || (Job >= 17 && Job <= 21))
maybe the case is faster. but i don't think so at this one...
someone test please?
Even if this was true the speed difference wouldnt affect anything at all, it would be VERY small of a difference. Also im pretty sure the switch is faster, as you have SIX variable accesses in the above code.


btw you know you can do this:

Код:
if((1 <= job <= 4) || (8 <= Job <= 13) || (17 <= Job <= 21))
this is a lot cleaner.


Re: shorten a line like this? - Calgon - 18.03.2011

Quote:
Originally Posted by legodude
Посмотреть сообщение
pawn Код:
new Job=GetPlayerJobID(playerid);
if((Job >= 1 && Job <= 4) || (Job >= 8 && Job <= 13) || (Job >= 17 && Job <= 21))
maybe the case is faster. but i don't think so at this one...
someone test please?
You're wasting memory by creating another variable there.


Re: shorten a line like this? - Norck - 18.03.2011

Quote:
Originally Posted by Calg00ne
Посмотреть сообщение
You're wasting memory by creating another variable there.
But he saves a CPU usage, so this function will be called only once.