Raised This Month: $51 Target: $400
 12% 

What is enum?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 10-28-2013 , 09:55   What is enum?
Reply With Quote #1

I found in one code this part:

PHP Code:
enum
{
    
CLASS_SMOKER 1,
    
CLASS_BOOMER 2,
    
CLASS_HUNTER 3,
    
CLASS_SPITTER 4,
    
CLASS_JOCKEY 5,
    
CLASS_CHARGER 6,
    
CLASS_WITCH 7,
    
CLASS_TANK 8
}; 
What is possible to do with it? What is that enum anyway?
iGANGNAM is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-28-2013 , 10:47   Re: What is enum?
Reply With Quote #2

An enum is just a group of related values.

enums usually come in two types each with two subtypes:
  1. Named. These are considered a tag (or type).
    • Normal numbered. Example: TFTeam enum:
      PHP Code:
      enum TFTeam
      {
          
      TFTeam_Unassigned 0,
          
      TFTeam_Spectator 1,
          
      TFTeam_Red 2,
          
      TFTeam_Blue 3    
      }; 
    • Flags that can be ORed ( | ) together. Example: MenuAction enum:
      PHP Code:
      enum MenuAction
      {
          
      MenuAction_Start = (1<<0),        /**< A menu has been started (nothing passed) */
          
      MenuAction_Display = (1<<1),    /**< A menu is about to be displayed (param1=client, param2=MenuPanel Handle) */
          
      MenuAction_Select = (1<<2),        /**< An item was selected (param1=client, param2=item) */
          
      MenuAction_Cancel = (1<<3),        /**< The menu was cancelled (param1=client, param2=reason) */
          
      MenuAction_End = (1<<4),        /**< A menu display has fully ended.
                                               param1 is the MenuEnd reason, and if it's MenuEnd_Cancelled, then 
                                               param2 is the MenuCancel reason from MenuAction_Cancel.
                                               */
          
      MenuAction_VoteEnd = (1<<5),    /**< (VOTE ONLY): A vote sequence has succeeded (param1=chosen item) 
                                               This is not called if SetVoteResultCallback has been used on the menu. */
          
      MenuAction_VoteStart = (1<<6),     /**< (VOTE ONLY): A vote sequence has started (nothing passed) */
          
      MenuAction_VoteCancel = (1<<7),    /**< (VOTE ONLY): A vote sequence has been cancelled (param1=reason) */
          
      MenuAction_DrawItem = (1<<8),    /**< An item is being drawn; return the new style (param1=client, param2=item) */
          
      MenuAction_DisplayItem = (1<<9),/**< Item text is being drawn to the display (param1=client, param2=item)
                                                To change the text, use RedrawMenuItem().
                                                If you do so, return its return value.  Otherwise, return 0.
                                                */
      }; 
  2. Not-named. Essentially, these are a group of related numbered constants. Example: The enum you posted, although they normally don't specify every value manually and are instead auto-numbered.

I would have probably have done that enum differently, like this:
PHP Code:
enum 
{
    
InfectedClass_Unknown,
    
InfectedClass_Smoker
    
InfectedClass_Boomer
    
InfectedClass_Hunter
    
InfectedClass_Spitter
    
InfectedClass_Jockey
    
InfectedClass_Charger
    
InfectedClass_Witch
    
InfectedClass_Tank 
}; 
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-28-2013 at 10:51.
Powerlord is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 10-28-2013 , 11:35   Re: What is enum?
Reply With Quote #3

Or just abuse the preprocessor and push these silly applications to the wastebasket.
Not to say there re not valid reasons to use enums, types can be handy.. But also stupid and annoying like for teams :/
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
iGANGNAM
AlliedModders Donor
Join Date: Sep 2012
Location: Lithuania
Old 10-28-2013 , 12:50   Re: What is enum?
Reply With Quote #4

So it's possible to do something like this?

Code:
enum EnumZombieClass {
	Hunter = 0,
	Boomer = 1,
	
}

enum EnumZombieProp {
	Name = 0,
	HP = 1,
	Speed = 2,
	
}

new const String:Zombie_Classes[EnumZombieClass][EnumZombieProp][] =
{
	{
	"Hunter",
	1000,
	250
	},

	{
	"Boomer",
	2000,
	220
	}
}
And later use it in code like this: Zombie_Classes[Hunter][HP] or so

Last edited by iGANGNAM; 10-28-2013 at 12:54.
iGANGNAM is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-28-2013 , 12:54   Re: What is enum?
Reply With Quote #5

Quote:
Originally Posted by iGANGNAM View Post
So it's possible to do something like this?

Code:
enum EnumZombieClass {
	Hunter = 0,
	Boomer = 1,
	Smoker = 2,
	Chainsaw = 3,
	Tank = 4
	
}

new const String:Zombie_Class_Names[EnumZombieClass][] =
{
	"Hunter",
	"Boomer",
	"Smoker",
	"Chainsaw",
	"Tank" //Tank class name
}
And later use it in code like this: Zombie_Class_Names[Hunter] or so
I haven't tried it, but in theory it should work. Although I'm not exactly sure if the const keyword works on variable definition (although it should).
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
mcpan313
Senior Member
Join Date: Mar 2010
Old 10-28-2013 , 13:54   Re: What is enum?
Reply With Quote #6

Quote:
Originally Posted by iGANGNAM View Post
So it's possible to do something like this? ...
PHP Code:
enum EnumZombieClass
{
    
Hunter 0,
    
Boomer 1,
}

#define NAME_LENGTH 32
enum EnumZombieProp
{
    
String:Name[NAME_LENGTH],
    
HP,
    
Speed
}

new 
Zombie_Classes[EnumZombieClass][EnumZombieProp] =
{
    {
"Hunter"1000250},
    {
"Boomer"2000220}
};

public 
OnPluginStart()
{
    
strcopy(Zombie_Classes[Hunter][Name], NAME_LENGTH"SuperHunter");
    
Zombie_Classes[Hunter][HP] = 2000;
    
// ...

__________________
sorry, for my poor english.
mcpan313 is offline
Send a message via MSN to mcpan313
RedSword
SourceMod Plugin Approver
Join Date: Mar 2006
Location: Quebec, Canada
Old 10-28-2013 , 14:41   Re: What is enum?
Reply With Quote #7

Quote:
Originally Posted by iGANGNAM View Post
So it's possible to do something like this?

Code:
enum EnumZombieClass {
	Hunter = 0,
	Boomer = 1,
	
}

enum EnumZombieProp {
	Name = 0,
	HP = 1,
	Speed = 2,
	
}

new const String:Zombie_Classes[EnumZombieClass][EnumZombieProp][] =
{
	{
	"Hunter",
	1000,
	250
	},

	{
	"Boomer",
	2000,
	220
	}
}
And later use it in code like this: Zombie_Classes[Hunter][HP] or so
You can fake struct yes, but mixing "string" cell type (8 bits) with cell type (32 bits) is a bad idea (saw a problem a long time ago; since this is more a hack using enums than an official feature). It can lead to incomprehensive errors.

I believe that if you do so you have the put the string at the end to avoid that problem (AFAICR, really not sure since it's not something I used really; just read about it).

And I'm not sure if your code in itself is ok (I answered with the intent of your post; not the actual code).
__________________
My plugins :
Red Maze
Afk Bomb
RAWR (per player/rounds Awp Restrict.)
Kill Assist
Be Medic

You can also Donate if you appreciate my work

Last edited by RedSword; 10-28-2013 at 14:42.
RedSword is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 10-28-2013 , 15:09   Re: What is enum?
Reply With Quote #8

Quote:
Originally Posted by RedSword View Post
You can fake struct yes, but mixing "string" cell type (8 bits) with cell type (32 bits) is a bad idea (saw a problem a long time ago; since this is more a hack using enums than an official feature). It can lead to incomprehensive errors.

I believe that if you do so you have the put the string at the end to avoid that problem (AFAICR, really not sure since it's not something I used really; just read about it).

And I'm not sure if your code in itself is ok (I answered with the intent of your post; not the actual code).
This "bad idea" is used in the adminmenu plugin (in scripting/adminmenu/dynamicmenu.sp specifically). And if you're rejecting plugins because someone uses it this way you will be called out on it.

Most of the problems you'll have with it involve inserting it into adt_tries and adt_arrays (Edit: and Natives). The ones I've seen are fixable by adding [0] to the end of the variable you're using... such as
Code:
SetTrieArray(counterTrie, time, soundData[0], sizeof(soundData));
which is from the current version of MapChooser Extended Sounds. This works because soundData[0] is the start of the array and I'm telling it the size is the size of the entire array. This is documented in this thread.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 10-28-2013 at 15:16.
Powerlord is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 11:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode