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

Enum instead of #define ?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 05-30-2018 , 09:47   Enum instead of #define ?
Reply With Quote #1

Hello,

I have several #define right now :
PHP Code:
#define AS_REGISTER_SERVER        0
#define AS_HOOK_CHAT            1
#define AS_UNHOOK_CHAT            2
#define AS_SIMPLE                3
#define AS_TRADEOFFER_SUCCESS    4
#define AS_TRADEOFFER_DECLINED    5
#define AS_SCAN_INVENTORY        6
#define AS_CREATE_TRADEOFFER    7
#define AS_NOT_FRIENDS            8
#define AS_TRADE_TOKEN            9
#define AS_FRIEND_INVITE        10
#define AS_REPORT_PLAYER        11
#define AS_INVITE_GROUP            12
#define AS_SCAN_INVENTORY_IMG   13
#define AS_EXECUTE_CMD            14
#define AS_DISCONNECT            15
#define AS_SG_ANNOUCEMENT        16 
And I will probably have a lot more.

This is my forward :
PHP Code:
/** 
 * Fire when ever you get a message from ASteambot.
 * 
 * @return          Nothing.
 */ 
forward int ASteambot_Message(int MessageTypechar[] message, const int messageSize); 

My question is, should I use enum ? Would it be better for my forward so I would have something like this :
PHP Code:
/** 
 * Fire when ever you get a message from ASteambot.
 * 
 * @return          Nothing.
 */ 
forward int ASteambot_Message(MyCustomType MessageTypechar[] message, const int messageSize); 
__________________
Want to check my plugins ?

Last edited by Arkarr; 05-30-2018 at 09:47.
Arkarr is offline
ThatKidWhoGames
Veteran Member
Join Date: Jun 2013
Location: IsValidClient()
Old 05-30-2018 , 10:10   Re: Enum instead of #define ?
Reply With Quote #2

I think it honestly comes down to preference. Personally, I believe that an enum would be better for this.
ThatKidWhoGames is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 05-30-2018 , 10:24   Re: Enum instead of #define ?
Reply With Quote #3

Quote:
Originally Posted by ThatKidWhoGames View Post
I think it honestly comes down to preference. Personally, I believe that an enum would be better for this.
Okay... Still weird to me why we have enum then.
__________________
Want to check my plugins ?
Arkarr is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 05-30-2018 , 10:39   Re: Enum instead of #define ?
Reply With Quote #4

Honestly I would always choose an enum for this type of situation. Even if you don't want the "custom type" of the enum, you can omit the name and it will basically just become an int.

It also makes your list a little more manageable. For example if for any reason you needed to add a field in the middle you wouldn't have to update the numbers for all the fields that go after that one manually.

Here's what your example would look like as an enum without having to change anything in your forward (can stay as an int instead of MyCustomType):
PHP Code:
enum
{
    
AS_RegisterServer 0,
    
AS_HookChat,
    
AS_UnhookChat,
    
AS_Simple,
    ...
}; 

Last edited by hmmmmm; 05-30-2018 at 10:41.
hmmmmm is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 05-30-2018 , 10:55   Re: Enum instead of #define ?
Reply With Quote #5

Quote:
Originally Posted by hmmmmm View Post
Honestly I would always choose an enum for this type of situation. Even if you don't want the "custom type" of the enum, you can omit the name and it will basically just become an int.

It also makes your list a little more manageable. For example if for any reason you needed to add a field in the middle you wouldn't have to update the numbers for all the fields that go after that one manually.

Here's what your example would look like as an enum without having to change anything in your forward (can stay as an int instead of MyCustomType):
PHP Code:
enum
{
    
AS_RegisterServer 0,
    
AS_HookChat,
    
AS_UnhookChat,
    
AS_Simple,
    ...
}; 
So... just as in a real programming language ? Cool.
That would also work, right ?
PHP Code:
enum MyType
{
    
AS_RegisterServer 0,
    
AS_HookChat,
    
AS_UnhookChat,
    
AS_Simple,
    ...
};

MyType value1 AS_RegisterServer;
MyType value2 AS_Simple;

MyType result value1 value2;

PrintToServer(">>> %i"result); //Excepted : ">>> 3" 
EDIT: I would have tested it by myself/searched if I could.
__________________
Want to check my plugins ?

Last edited by Arkarr; 05-30-2018 at 10:57.
Arkarr is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 05-30-2018 , 11:17   Re: Enum instead of #define ?
Reply With Quote #6

Yes, compiles and runs fine in that context. But as soon as you try to assign the enum type to an int type you'll get tag mismatch warnings. If you don't need to assign to ints at all then that should work fine but if you do I suggest removing the enum name so you dont get into the view_as hell.

Also SourcePawn is a real language
hmmmmm is offline
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
Old 05-30-2018 , 11:37   Re: Enum instead of #define ?
Reply With Quote #7

Quote:
Originally Posted by hmmmmm View Post
Yes, compiles and runs fine in that context. But as soon as you try to assign the enum type to an int type you'll get tag mismatch warnings. If you don't need to assign to ints at all then that should work fine but if you do I suggest removing the enum name so you dont get into the view_as hell.

Also SourcePawn is a real language
Alright, thank you !
Yeah, you are right ! I don't know, for a second I thought it was interpreted...
__________________
Want to check my plugins ?
Arkarr is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 05-31-2018 , 05:50   Re: Enum instead of #define ?
Reply With Quote #8

Quote:
Originally Posted by hmmmmm View Post
Yes, compiles and runs fine in that context. But as soon as you try to assign the enum type to an int type you'll get tag mismatch warnings. If you don't need to assign to ints at all then that should work fine but if you do I suggest removing the enum name so you dont get into the view_as hell.
If the enum values should implicitly convert to int, make the first character of the name lowercase, if they should not, make it uppercase. That is how Pawn controls strict/weak type conversion. See: bool vs Float.
__________________

Last edited by asherkin; 05-31-2018 at 05:51.
asherkin 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 07:19.


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