AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Converting enum elements (https://forums.alliedmods.net/showthread.php?t=325117)

thEsp 06-08-2020 14:31

Converting enum elements
 
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...

Napoleon_be 06-08-2020 16:18

Re: Converting enum elements
 
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).

Bugsy 06-08-2020 16:27

Re: Converting enum elements
 
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 ] ); 


thEsp 06-08-2020 18:13

Re: Converting enum elements
 
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.

Bugsy 06-08-2020 18:53

Re: Converting enum elements
 
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?


All times are GMT -4. The time now is 17:07.

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