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