[Tutorial] Enumerators (enums). What they actually are?
#22

You may also add that you can define the increment and decrement of the enum values, like Slice did in his Tips & Tricks thread where he used Enums to create 32 flags into one single variable:

Quote:
Originally Posted by Slice
View Post
Bit-flags in enums
Did you know that you can store 32 true/false values in one single variable? Not only do you save space, but you also get less clutter in your code.

You don't have to understand how the binary numeral system works; however, I recommend it. You can read more about it in this topic, if you're interested.

If you have, say, 100 true/false (bool) per-player variables you would use 195 KB of space. However, if you were to use 4 arrays with bit flags, only 8 KB of space would be used. The outcome would be exactly the same, but you would save 187 KB of space!

Here's an example also containing macros to simplify the usage.
pawn Code:
// Usage for all macros: BitFlag_X(variable, flag)
#define BitFlag_Get(%0,%1)            ((%0) & (%1))   // Returns zero (false) if the flag isn't set.
#define BitFlag_On(%0,%1)             ((%0) |= (%1))  // Turn on a flag.
#define BitFlag_Off(%0,%1)            ((%0) &= ~(%1)) // Turn off a flag.
#define BitFlag_Toggle(%0,%1)         ((%0) ^= (%1))  // Toggle a flag (swap true/false).

enum PlayerFlags:(<<= 1) {
    // It's important that you don't forget to put "= 1" on the first flag. If you don't, all flags will be 0.
   
    PLAYER_IS_LOGGED_IN = 1,   // 0b00000000000000000000000000000001
    PLAYER_HAS_GANG,           // 0b00000000000000000000000000000010
    PLAYER_CAN_BUY_PROPERTIES, // 0b00000000000000000000000000000100
    PLAYER_BLABLA_1,           // 0b00000000000000000000000000001000
    PLAYER_BLABLA_2,           // 0b00000000000000000000000000010000
    PLAYER_BLABLA_3,           // 0b00000000000000000000000000100000
    PLAYER_BLABLA_4,           // 0b00000000000000000000000001000000
    PLAYER_BLABLA_5,           // 0b00000000000000000000000010000000
    PLAYER_BLABLA_6,           // 0b00000000000000000000000100000000
    PLAYER_BLABLA_7,           // 0b00000000000000000000001000000000
    PLAYER_BLABLA_8,           // 0b00000000000000000000010000000000
    PLAYER_BLABLA_9,           // 0b00000000000000000000100000000000
    PLAYER_BLABLA_10,          // 0b00000000000000000001000000000000
    PLAYER_BLABLA_11,          // 0b00000000000000000010000000000000
    PLAYER_BLABLA_12,          // 0b00000000000000000100000000000000
    PLAYER_BLABLA_13,          // 0b00000000000000001000000000000000
    PLAYER_BLABLA_14,          // 0b00000000000000010000000000000000
    PLAYER_BLABLA_15,          // 0b00000000000000100000000000000000
    PLAYER_BLABLA_16,          // 0b00000000000001000000000000000000
    PLAYER_BLABLA_17,          // 0b00000000000010000000000000000000
    PLAYER_BLABLA_18,          // 0b00000000000100000000000000000000
    PLAYER_BLABLA_19,          // 0b00000000001000000000000000000000
    PLAYER_BLABLA_20,          // 0b00000000010000000000000000000000
    PLAYER_BLABLA_21,          // 0b00000000100000000000000000000000
    PLAYER_BLABLA_22           // 0b00000001000000000000000000000000
};
Notice that in the PlayerFlags enum, he has this tiny piece of code after the name of it:
pawn Code:
:(<<= 1)
This will tell the enum to set the next enum-data to the number equal to the previous enum-data shifted to the left.

Standard enums would have had this piece of code attached to it
pawn Code:
:(++)
//or possibly :(+= 1)
But remember, as Slice mentioned in the quote above: The first value will be set to 0 unless you specify otherwise. So if you try using multiplication or division without specifying the value of the first enum-data, you will just get lots of 0's - which you don't want.

EDIT: Nice tutorial btw - +1
Reply


Messages In This Thread
Enumerators (enums). What they actually are? - by iPLEOMAX - 14.02.2012, 17:00
Re: Enumerators (enums). What they actually are? - by T0pAz - 14.02.2012, 17:05
Re: Enumerators (enums). What they actually are? - by ServerScripter - 14.02.2012, 17:10
Re: Enumerators (enums). What they actually are? - by iPLEOMAX - 14.02.2012, 18:31
Re: Enumerators (enums). What they actually are? - by -CaRRoT - 14.02.2012, 18:53
Re: Enumerators (enums). What they actually are? - by iPLEOMAX - 15.02.2012, 09:06
Re: Enumerators (enums). What they actually are? - by Calgon - 15.02.2012, 09:07
Re: Enumerators (enums). What they actually are? - by iPLEOMAX - 15.02.2012, 09:24
Re: Enumerators (enums). What they actually are? - by Vince - 15.02.2012, 10:19
Re: Enumerators (enums). What they actually are? - by iPLEOMAX - 15.02.2012, 10:33
Re: Enumerators (enums). What they actually are? - by KingHual - 15.02.2012, 11:32
Re: Enumerators (enums). What they actually are? - by Max_Coldheart - 15.02.2012, 13:08
Re: Enumerators (enums). What they actually are? - by Zarky - 15.02.2012, 13:33
Re: Enumerators (enums). What they actually are? - by kizla - 15.02.2012, 19:01
Re: Enumerators (enums). What they actually are? - by -ExG-VirusKiller - 16.02.2012, 04:33
Re: Enumerators (enums). What they actually are? - by Nonameman - 19.02.2012, 00:17
Re: Enumerators (enums). What they actually are? - by Minion - 17.07.2012, 11:53
Re: Enumerators (enums). What they actually are? - by Sniper Kitty - 06.09.2012, 06:52
Re: Enumerators (enums). What they actually are? - by Slice - 06.09.2012, 06:57
Re: Enumerators (enums). What they actually are? - by FalconCRO - 11.01.2013, 20:29
Re: Enumerators (enums). What they actually are? - by leonardo1434 - 11.01.2013, 21:46
Re: Enumerators (enums). What they actually are? - by LarzI - 11.01.2013, 21:55
Re: Enumerators (enums). What they actually are? - by FalconCRO - 11.01.2013, 22:19
Re: Enumerators (enums). What they actually are? - by LarzI - 11.01.2013, 22:23
Re: Enumerators (enums). What they actually are? - by gDarius - 14.01.2013, 14:02
Re: Enumerators (enums). What they actually are? - by FalconCRO - 13.02.2013, 11:06
Re: Enumerators (enums). What they actually are? - by CreativityLacker - 13.02.2013, 11:38
Re: Enumerators (enums). What they actually are? - by LarzI - 13.02.2013, 13:12
Re: Enumerators (enums). What they actually are? - by SomebodyAndMe - 09.05.2013, 15:29
Re: Enumerators (enums). What they actually are? - by iPLEOMAX - 09.05.2013, 19:13
Re: Enumerators (enums). What they actually are? - by Frede - 11.05.2013, 08:56
Re: Enumerators (enums). What they actually are? - by cuemur - 17.03.2014, 09:19
Re: Enumerators (enums). What they actually are? - by Baltazar - 08.04.2014, 01:44
Re: Enumerators (enums). What they actually are? - by Binx - 08.04.2014, 01:55
Re: Enumerators (enums). What they actually are? - by Niko_boy - 08.04.2014, 04:20
Re: Enumerators (enums). What they actually are? - by khRamin78 - 24.06.2015, 06:35
Re: Enumerators (enums). What they actually are? - by Konverse - 24.06.2015, 07:02
Re: Enumerators (enums). What they actually are? - by andyandyy8 - 24.06.2015, 09:28
AW: Enumerators (enums). What they actually are? - by Br3ad - 06.07.2015, 14:49
Re: Enumerators (enums). What they actually are? - by TheRaGeLord - 07.07.2015, 10:36

Forum Jump:


Users browsing this thread: 1 Guest(s)