Making this code shorter
#1

Hi, how would it be possible to make this code shorter?
stock PlayerHasHeavyWeapon(playerid)
{
Код:
if (PlayerHasWeapon(playerid, 25) || PlayerHasWeapon(playerid, 27) || PlayerHasWeapon(playerid, 29) || PlayerHasWeapon(playerid, 30) || PlayerHasWeapon(playerid, 31) || PlayerHasWeapon(playerid, 33) || PlayerHasWeapon(playerid, 34))
Reply
#2

Hello!

Like this?
PHP код:
//global on the top of the script:
#define PHW(%0,%1) PlayerHasWeapon(%0,%1)
//the shorter code:
if(PHW(playerid,25) || PHW(playerid,27) || PHW(playerid,29) || PHW(playerid,30) || PHW(playerid,31) || PHW(playerid,33) || PHW(playerid,34)) 
Reply
#3

Like this:
Код:
if ((22 <= weaponid <= 38)
Reply
#4

Quote:
Originally Posted by Mencent
Посмотреть сообщение
Hello!

Like this?
PHP код:
//global on the top of the script:
#define PHW(%0,%1) PlayerHasWeapon(%0,%1)
//the shorter code:
if(PHW(playerid,25) || PHW(playerid,27) || PHW(playerid,29) || PHW(playerid,30) || PHW(playerid,31) || PHW(playerid,33) || PHW(playerid,34)) 
Or how to make the code totally unreadable. It bothers me to no end when I see people do that. Typing "SCM" or "SPD" because it's apparently too hard to type it out in full.

@OP: the code isn't pretty but there's only so much you can do. Any way I look at it will make it slower.
Reply
#5

Rewrite your PlayerHasWeapon function to accept multiply weaponids
Reply
#6

how you write PlayerHasWeapon
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
It bothers me to no end when I see people do that. Typing "SCM" or "SPD" because it's apparently too hard to type it out in full.
He want the code shorter, and it's shorter. If the code is readable or not is another question.
Well I did what he want.
Reply
#8

Maybe this?

pawn Код:
if (PlayerHasWeapon(playerid, 25)
|| PlayerHasWeapon(playerid, 27)
|| PlayerHasWeapon(playerid, 29)
|| PlayerHasWeapon(playerid, 30)
|| PlayerHasWeapon(playerid, 31)
|| PlayerHasWeapon(playerid, 33)
|| PlayerHasWeapon(playerid, 34))
Reply
#9

Try something like this:

PHP код:
new weapon[6][] = {{25},{27},{29},{30},{31},{33},{34}};
CheckWeaps(playerid) {
    for(new 
07i++);  {
        if(
PlayerHasWeapon(playeridweapon[i])) return true;
    }
    return 
false;
}
if(
CheckWeaps(playerid)) {
    
// your code here

This will make it easier if you wish to add more weapons along the way.
Reply
#10

I'm guessing your function loops through the player's weapons or something like that.
You shouldn't do that, because if you call it multiple times, you're looping multiple times. It's a waste.

Either make the function suggested by Nero_3D, or just loop once and check, without using a special function for that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)