SA-MP Forums Archive
Making this code shorter - 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: Making this code shorter (/showthread.php?tid=611851)



Making this code shorter - GoldenLion - 11.07.2016

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))



Re: Making this code shorter - Mencent - 11.07.2016

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)) 



Re: Making this code shorter - GoldenLion - 11.07.2016

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



Re: Making this code shorter - Vince - 11.07.2016

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.


Re: Making this code shorter - Nero_3D - 11.07.2016

Rewrite your PlayerHasWeapon function to accept multiply weaponids


Re: Making this code shorter - OneDay - 11.07.2016

how you write PlayerHasWeapon


Re: Making this code shorter - Mencent - 11.07.2016

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.


Re: Making this code shorter - MayaEU - 11.07.2016

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))



Re: Making this code shorter - DarkSkull - 11.07.2016

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.


Re: Making this code shorter - Stinged - 11.07.2016

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.