shorten a line like this?
#1

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?
Reply
#2

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.
Reply
#3

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

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

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

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.
Reply
#7

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?
Reply
#8

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.
Reply
#9

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.
Reply
#10

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)