Game Development Community

How to get to Enum String..

by Dusty Monk · in Torque 3D Professional · 08/24/2010 (8:37 pm) · 2 replies

In The New Order (1.1b2), is there a way to get to the string associated with an enum value from an ImplementEnumType declaration?

That is, given an Enum Type implementation as such:

ImplementEnumType(ActionContainers, "Action Containers" "@ingroup wsAvatar")
   {  containerInventoryPanel,       "InventoryContainer" },
   {  containerAbilityPanel,         "AbilityContainer"   },
   {  containerEquipmentPanel,       "EquipmentContainer" },
   {  containerWeaponPanel,          "WeaponContainer" },
   {  containerVehiclePanel,         "VehicleContainer" },
   {  containerGaugePanel,           "GaugeContainer" }
EndImplementEnumType;

In a console function, I need to return the string associated with the enum values. Previously, I could just index into the global table declarion and return the label, as such:

ConsoleMethod(wsAvatar, getWeaponPanelInfoAt, const char *, 3, 3, "(int index)")
{

   S32 nIndex = dAtoi(argv[2]);
   ActionReference ref;
   if (!object->getWeaponPanelInfo(nIndex, ref))
      return NULL;

   if (ref.mContainerID < 0)
      return NULL;

   char * ret = Con::getReturnBuffer(64);
   dSprintf(ret, 64, "%s %d", gwsActionContainerTable.table[ref.mContainerID].label, ref.mIndex);
   return ret;
}

But now that we build the enum associations through a rather convoluted series of nested templates, and I haven't been able yet to step through the macro unravelling to figure out exactly what's getting declared within, I have no idea how to get to this string.

It seems silly to declare a separate static string table just to be able to return the appropriate string (and to then have to update two locations if the string changes) when we're clearly making the constant to string association in the ImplementEnumType declation.

Thoughts?

About the author

Dusty Monk is founder and president of Windstorm Studios, an independant game studio. Formerly a sr. programmer at Ensemble Studios, Dusty has worked on AAA titles such as Age of Empires II & III, and Halo Wars.


#1
08/24/2010 (9:35 pm)
Use EngineMarshallData<YourEnumType>(type) to convert enum values to string.

This:
EngineMarshallData<ActionContainers>(containerAbilityPanel);
Or this:
EngineMarshallData<ActionContainers>( (ActionContainers)1 );
Returns this:
"AbilityContainer"
#2
08/24/2010 (9:46 pm)
Cool thanks I'll try that.