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

PHP Code:
enum EXAMPLE
{
       
EX_1,
       
EX_2[3]
}
new 
myarray[EXAMPLE];
public 
OnFilterScriptInit()
{
    
myarray[EX_2][0] = 1;
    
myarray[EX_2][1] = 2;
    
myarray[EX_2][2] = 3;
    for(new 
i!= 3i++)
        
printf(#%d,myarray[EX_2][i]);

Self explanatory, i hope.
Reply
#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
#23

Quote:
Originally Posted by leonardo1434
View Post
PHP Code:
enum EXAMPLE
{
       
EX_1,
       
EX_2[3]
}
new 
myarray[EXAMPLE];
public 
OnFilterScriptInit()
{
    
myarray[EX_2][0] = 1;
    
myarray[EX_2][1] = 2;
    
myarray[EX_2][2] = 3;
    for(new 
i!= 3i++)
        
printf(#%d,myarray[EX_2][i]);

Self explanatory, i hope.
Guess you meant i < 3, not i != 3. Ok so this is the only way to add values? I thought I can add multiple values in one line like the example I posted with array:
Code:
new example[3] = {1,2,3};
Reply
#24

Quote:
Originally Posted by FalconCRO
View Post
Guess you meant i < 3, not i != 3
I think not.

With "i < 3 " the loop will be called as long as i is lower than 3. This means it will call at i = 0, 1 and 2.
With "i != 3" the loop will be called as long as i is not equal to 3. This means it will call at i = 0, 1 and 2.

Both operations will stop the loop when i reaches 3 - which is at the exact same values.
Reply
#25

Nicely explained and learned something from this tutorial.
Thanks.
Reply
#26

There's something I can't get to understand with enums. Observe the following code:

Code:
enum examples {
      ex_1,
      ex_2
}

new array[examples];
Now, if I try to access ex_1 cell, but let's say that in that specific part of the code it's more convenient to me to simply use array[0], instead of array[ex_1], I would get a tag mismatch warning. In order to get rid of the warning, I need to do this: array[examples:0], but why? I thought it has something to do with strong and weak tags, but I'm using a weak one and it still happens.
Reply
#27

Quote:
Originally Posted by FalconCRO
View Post
There's something I can't get to understand with enums. Observe the following code:

Code:
enum examples {
      ex_1,
      ex_2
}

new array[examples];
Now, if I try to access ex_1 cell, but let's say that in that specific part of the code it's more convenient to me to simply use array[0], instead of array[ex_1], I would get a tag mismatch warning. In order to get rid of the warning, I need to do this: array[examples:0], but why? I thought it has something to do with strong and weak tags, but I'm using a weak one and it still happens.
Think of it yourself -

array[0] <= script reads it as the first cell of the variable 'array'
array[examples:0] <= script reads it as first var of enum 'examples' which is stored in the variable 'array'

Not sure if the language is 100% right but I hope you understand what I'm saying.
Reply
#28

Quote:
Originally Posted by FalconCRO
View Post
There's something I can't get to understand with enums. Observe the following code:

Code:
enum examples {
      ex_1,
      ex_2
}

new array[examples];
Now, if I try to access ex_1 cell, but let's say that in that specific part of the code it's more convenient to me to simply use array[0], instead of array[ex_1], I would get a tag mismatch warning. In order to get rid of the warning, I need to do this: array[examples:0], but why? I thought it has something to do with strong and weak tags, but I'm using a weak one and it still happens.
It's most probably because you've declared the index of the array as the enum.

Edit: Forget what I said earlier (removed)

I wonder, however, why do you want to use an enum as your array index if you somehow find it easier/more convenient to use numbered indexes at times?
Reply
#29

Would this be possible, to set 0 as the default already in the enum?

pawn Code:
enum TruckLocation
{
    LocationName[60],
    Float:LocX,
    Float:LocY,
    Float:LocZ,
    bool:IsIsland,
    Float:FLocX = 0,
    Float:FLocY = 0,
    Float:FLocZ = 0,
    IslandCheckpoint,
}
And if I set it at the new:
pawn Code:
new TLocations[][TruckLocation] =
{
    {"Test Location", 1, 2, 3, false},
    {"Test Location with island.", 1, 2, 3, true, 3, 2, 1}
};
Would this actually work or not?
Reply
#30

Quote:
Originally Posted by SomebodyAndMe
View Post
Would this be possible, to set 0 as the default already in the enum?

pawn Code:
enum TruckLocation
{
    LocationName[60],
    Float:LocX,
    Float:LocY,
    Float:LocZ,
    bool:IsIsland,
    Float:FLocX = 0,
    Float:FLocY = 0,
    Float:FLocZ = 0,
    IslandCheckpoint,
}
And if I set it at the new:
pawn Code:
new TLocations[][TruckLocation] =
{
    {"Test Location", 1, 2, 3, false},
    {"Test Location with island.", 1, 2, 3, true, 3, 2, 1}
};
Would this actually work or not?
Float:FLocX = 0,
Float:FLocY = 0,
Float:FLocZ = 0,

means:

#define FLocX 0
#define FLocY 0
#define FLocZ 0

which makes:

TLocations[x][FLocX]
TLocations[x][FLocY]
TLocations[x][FLocZ]

look like:

TLocations[x][0]
TLocations[x][0]
TLocations[x][0]..

Accessing the same index for multiple values isn't a good idea?

Why don't you:
Float:FLoc[3],

So you can access it:
FLoc[0] for x
FLoc[1] for y
FLoc[2] for z

------

Other than all that, you haven't understood enums yet. It's never possible for "TruckLocation" to set a default value to your "TLocations".. simply, TruckLocation is only used for accessing indexes of TLocations.
Reply
#31

Nice
Reply
#32

Can i add here slots hslots for guests in house like for 3 people s[32] name slots
PHP Code:
enum hInfo
{
    
id,
    
Float:hEntrx,
    
Float:hEntry,
    
Float:hEntrz,
    
Float:hExitx,
    
Float:hExity,
    
Float:hExitz,
    
Float:hCarx,
    
Float:hCary,
    
Float:hCarz,
    
Float:hCarfa,
    
hOwner[32],
    
hDiscript[16],
    
hPrice,
    
hBuyPrice,
    
hInt,
    
hVirtual,
    
hLock,
    
hMIcon,
    
hPickup,
    
hPickupExit,
    
hOplata,
    
hHeal,
    
hslot,
    
bool:hRob
}
new 
HouseInfo[MAXHOUSE][hInfo]; // 

And use it like this: ?
PHP Code:
for(new idxidx TOTALHOUSEidx++)
    {
            
HouseInfo[idx]hslot[0],
            
HouseInfo[idx]hslot[1],
            
HouseInfo[idx]hslot[2]);
        } 
Reply
#33

Quote:
Originally Posted by Y_Less
View Post
Some other points for you:

pawn Code:
enum E_EG
{
    E_FIRST = 4,
    E_SECOND = 2
}

main()
{
    new data[E_EG];
    data[E_FIRST] = 7; // Compiler error - index out of bounds (E_EG = 3)
    data[E_EG:0] = 7; // Fine and no warnings.
    new E_EG:dat2 = E_FIRST; // Also no warnings despite using "E_FIRST".
}
"E_EG" is both a tag and a constant. You can also have anonymous enums and "strong" and "weak" enum tags.
Why is E_EG = 3 and not 2?
Reply
#34

Quote:
Originally Posted by cuemur
View Post
Can i add here slots hslots for guests in house like for 3 people s[32] name slots
PHP Code:
enum hInfo
{
    
id,
    
Float:hEntrx,
    
Float:hEntry,
    
Float:hEntrz,
    
Float:hExitx,
    
Float:hExity,
    
Float:hExitz,
    
Float:hCarx,
    
Float:hCary,
    
Float:hCarz,
    
Float:hCarfa,
    
hOwner[32],
    
hDiscript[16],
    
hPrice,
    
hBuyPrice,
    
hInt,
    
hVirtual,
    
hLock,
    
hMIcon,
    
hPickup,
    
hPickupExit,
    
hOplata,
    
hHeal,
    
hslot,
    
bool:hRob
}
new 
HouseInfo[MAXHOUSE][hInfo]; // 

And use it like this: ?
PHP Code:
for(new idxidx TOTALHOUSEidx++)
    {
            
HouseInfo[idx]hslot[0],
            
HouseInfo[idx]hslot[1],
            
HouseInfo[idx]hslot[2]);
        } 
Yaaaaay! You've successfully bumped a 10 month old thread. Congratulations!
Reply
#35

Quote:
Originally Posted by Binx
View Post
Yaaaaay! You've successfully bumped a 10 month old thread. Congratulations!
OFF-T: more like he is asking a question regarding the thread which seem to be related so it aint really a bump

ON-T:

Quote:
Originally Posted by cuemur
View Post
Can i add here slots hslots for guests in house like for 3 people s[32] name slots
[php]enum hInfo



And use it like this: ?
PHP Code:
for(new idxidx TOTALHOUSEidx++)
    {
            
HouseInfo[idx]hslot[0],
            
HouseInfo[idx]hslot[1],
            
HouseInfo[idx]hslot[2]);
        } 
yes you can just a little mistake the element name should be in square braces i.e. [ ]
e.g: HouseInfo[idx][ hslot[0] ] = something ; or whatever
here you are accessing element hslot and its inner array index 0.

hslot in enum should be like , hslot[3]; etc.
Reply
#36

nice one Thanks mate
Reply
#37

Very useful tutorial. Thanks for making it easy mate.
Reply
#38

Thanks a lot for this tutorial!
I used to do this mistake.
Quote:

enum E_VEHICLE_DATA{
VEHICLE_OWNER = INVALID_VEHICLE_ID
}

Reply
#39

Awesome tutorial, helped me out a few times thanks for that
Reply
#40

Thanks for this.. I really thought that the answer would be 0 | 9 | 5 | 56
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)