Jump to content

any way to get enum name from value?


photo

Recommended Posts

Posted

say I have a

enum

{

DUMMY1 = 1,

DUMMY2 = 2,

};

 

int v = 2;

how can I get "DUMMY2" string from the var v?

Posted

say I have a

enum

{

DUMMY1 = 1,

DUMMY2 = 2,

};

 

int v = 2;

how can I get "DUMMY2" string from the var v?

 

With a dedicated function (as symbolic names will be resolved to int values on compile)

 

String getEnumString( int v )
{
 if     ( v == DUMMY1 )  return "DUMMY1";
 else if( v == DUMMY2 )  return "DUMMY2";
 else                    return "INVALID";
}

Posted

Or you can use associative array if you have more enumeration elements:

 

enum
{
DUMMY1 = 1,
DUMMY2 = 2,
};

string map[];

map[DUMMY1] = "DUMMY1";
map[DUMMY2] = "DUMMY2";

log.message("map[DUMMY1] " + map[DUMMY1] + "\n");
log.message("map[DUMMY2] " + map[DUMMY2] + "\n");

Posted

thank you all guys for the reply, they worked, but i don't want write these manully, acctually i want something simlar runtime reflection, as engine known the enum constant value and name, i think there a way to get the enum name from value, for example

 

namespace Example{

enum

{

DUMMY1 = 1,

DUMMY2 = 2,

};

}

 

int v = 2;

String name = Example.getConstantName(v);

Posted

acctually i want something simlar runtime reflection

 

sorry, not available. You have to do a manual string lookup as described.

×
×
  • Create New...