SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 11/01/2013, 09:46 PM   #21
leonardo1434
High-roller
 
leonardo1434's Avatar
 
Join Date: Mar 2012
Posts: 1,667
Reputation: 179
Default Re: Enumerators (enums). What they actually are?

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.
__________________
c++
leonardo1434 is offline   Reply With Quote
Old 11/01/2013, 09:55 PM   #22
LarzI
High-roller
 
LarzI's Avatar
 
Join Date: Apr 2007
Location: Norway
Posts: 2,628
Reputation: 136
Default Re: Enumerators (enums). What they actually are?

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
__________________


NEW: [FS] Simple Cellphones

if( strfind( title, "REP", true ) != -1 )
{________________________________________
Sigh();__________________________
}________________________________________


LarzI is offline   Reply With Quote
Old 11/01/2013, 10:19 PM   #23
FalconCRO
Little Clucker
 
Join Date: Feb 2011
Posts: 15
Reputation: 0
Default Re: Enumerators (enums). What they actually are?

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};
FalconCRO is offline   Reply With Quote
Old 11/01/2013, 10:23 PM   #24
LarzI
High-roller
 
LarzI's Avatar
 
Join Date: Apr 2007
Location: Norway
Posts: 2,628
Reputation: 136
Default Re: Enumerators (enums). What they actually are?

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.
__________________


NEW: [FS] Simple Cellphones

if( strfind( title, "REP", true ) != -1 )
{________________________________________
Sigh();__________________________
}________________________________________


LarzI is offline   Reply With Quote
Old 14/01/2013, 02:02 PM   #25
gDarius
Big Clucker
 
gDarius's Avatar
 
Join Date: Jan 2013
Posts: 59
Reputation: -1
Default Re: Enumerators (enums). What they actually are?

Nicely explained and learned something from this tutorial.
Thanks.
gDarius is offline   Reply With Quote
Old 13/02/2013, 11:06 AM   #26
FalconCRO
Little Clucker
 
Join Date: Feb 2011
Posts: 15
Reputation: 0
Default Re: Enumerators (enums). What they actually are?

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.
FalconCRO is offline   Reply With Quote
Old 13/02/2013, 11:38 AM   #27
CreativityLacker
Big Clucker
 
CreativityLacker's Avatar
 
Join Date: Oct 2012
Location: The chocolate factory
Posts: 134
Reputation: 25
Default Re: Enumerators (enums). What they actually are?

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.
__________________
Scripter at Resurrection
91.121.153.202:7778 or resur.cod5samp.com:7778
CreativityLacker is offline   Reply With Quote
Old 13/02/2013, 01:12 PM   #28
LarzI
High-roller
 
LarzI's Avatar
 
Join Date: Apr 2007
Location: Norway
Posts: 2,628
Reputation: 136
Default Re: Enumerators (enums). What they actually are?

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?
__________________


NEW: [FS] Simple Cellphones

if( strfind( title, "REP", true ) != -1 )
{________________________________________
Sigh();__________________________
}________________________________________


LarzI is offline   Reply With Quote
Old 09/05/2013, 03:29 PM   #29
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 471
Reputation: 21
Default Re: Enumerators (enums). What they actually are?

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?
SomebodyAndMe is offline   Reply With Quote
Old 09/05/2013, 07:13 PM   #30
iPLEOMAX
High-roller
 
iPLEOMAX's Avatar
 
Join Date: Mar 2011
Location: AE
Posts: 1,250
Reputation: 746
Default Re: Enumerators (enums). What they actually are?

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.
__________________
Goodbye SA-MP.
iPLEOMAX is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums zxc1 Scripting Help 1 02/09/2011 02:31 PM
Component IDs from file aren't loading into the enums, but save from the enums. Jack_Leslie Help Archive 7 19/08/2011 02:00 PM
Help using enums [L3th4l] Help Archive 4 27/02/2011 05:10 AM
Enums xir Help Archive 2 02/01/2011 05:28 PM


All times are GMT. The time now is 09:24 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.