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

Converting enum elements


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
thEsp
BANNED
Join Date: Aug 2017
Old 06-08-2020 , 14:31   Converting enum elements
Reply With Quote #1

Hello. As the title says most of it, is there any way to convert enum elements to a certain string? I did search a lot before coming to ask here, even in other Pawn forums. In fact I've been hassling with this for more than 4 straight hours. Unfortunately I couldn't find anything helpful at all. After some time of researching I thought a pre-processor functionality from C (which I suppose is missing in Pawn) called "stringification" would help me, but that was something completely distinct and I realized it much later.

If you don't understand, here's what I mean:
Code:
enum MyEnumThingy // <- don't get confused by this name please, it's a totally random thing (example) {      Red,      Blue }; // ... new szEnumName[] = ENUM_NAME(Red /* Element value */, MyEnumThingy /* Enumerator */); // szEnumName = "Red"

That's not something natives can do so I'm counting on a hackish macro-function, if even possible...
thEsp is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 06-08-2020 , 16:18   Re: Converting enum elements
Reply With Quote #2

I might not fully understand what you're trying to do, but what about this?

PHP Code:
new szEnumName[MyEnumThingy];

num_to_str(szEnumName[Red]); 
The only other option i could really think of is just creating 2D array for it and "hardcode" color names. (Could be fixed by using a simple text file though).
__________________

Last edited by Napoleon_be; 06-08-2020 at 16:19.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-08-2020 , 16:27   Re: Converting enum elements
Reply With Quote #3

The enumerator members are only integer constants, the name of the variable cannot be converted to a string, to my knowledge at least. You'd need to size an array using the enumerator name and place the items in the array in the same order.
PHP Code:
enum Colors 
{
    
Red,
    
Green,
    
Blue,
    
Purple,
    
Yellow
}

new 
cColorStringsColors ][] = 
{
    
"Red",
    
"Green",
    
"Blue",
    
"Purple",
    
"Yellow"
};

server_print"The sun is %s" cColorStringsYellow ] ); 
__________________

Last edited by Bugsy; 06-08-2020 at 16:41.
Bugsy is offline
thEsp
BANNED
Join Date: Aug 2017
Old 06-08-2020 , 18:13   Re: Converting enum elements
Reply With Quote #4

Bugsy that's not what I need because that's hard-coded. The idea behind is to iterate through an enum's valid (0 - N) members and check if their name matches with a certain string. I heard Pawn for SA:MP has actually a stringification macro-function which could in theory come handy, but that's a custom feature. I just got my hands into the compiler we use and already figured out the part macro-definitions are handled. Because it's late mid-night now rest is to be seen & possibly modified tomorrow.

Last edited by thEsp; 06-08-2020 at 18:25.
thEsp is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-08-2020 , 18:53   Re: Converting enum elements
Reply With Quote #5

I had a feeling, being it was the most obvious solution. Though, I'm not sure it's worth the hassle to modify the compiler to handle this. Why does the string array not satisfy what you're trying to do? If the string matches the enum constant, how is it any different?
__________________
Bugsy 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 14:12.


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