Game Development Community

use Implement Enum Type in console method

by Nathan Bowhay - ESAL · in Torque 3D Professional · 03/22/2011 (1:21 pm) · 3 replies

Is there a way to use a enum in a console method?
So I can for instance have:
ImplementEnumType( TextEditUnitType,
   "nn" )
	{	GuiTextEditSliderCtrl::UnitTypeRadians,	"Radians"		},
	{	GuiTextEditSliderCtrl::UnitTypeDegrees,	"Degrees"		},
	{	GuiTextEditSliderCtrl::UnitTypeInches,	"Inches"		},
	{	GuiTextEditSliderCtrl::UnitTypeFeet,	"Feet"			},
	{	GuiTextEditSliderCtrl::UnitTypeMiles,	"Miles"			},
	{	GuiTextEditSliderCtrl::UnitTypeMM,		"Millimeters"	},
	{	GuiTextEditSliderCtrl::UnitTypeCM,		"Centimeters"	},
	{	GuiTextEditSliderCtrl::UnitTypeM,		"Meters"		},
	{	GuiTextEditSliderCtrl::UnitTypeKM,		"Kilometers"	},
	{	GuiTextEditSliderCtrl::UnitTypeNone,	"None"			}
EndImplementEnumType;

and then have a console method like this:
DefineConsoleMethod( GuiTextEditSliderCtrl, convert, F32, ( TextEditUnitType inType,  TextEditUnitType outType, F32 value),,
   "Converts a value from one type to another.n"
   "@param inType Type of the passed in value.n"
   "@param outType Type of to convert to.n"
   "@return Converted value or same value if it can't be converted." )
{
	return object->convert(inType, outType, value);
}

and call it in script like this:
myGui.convert("Degrees", "Radians", 90);

#1
03/22/2011 (2:18 pm)
As far as I know, you need to do it like this:

DefineConsoleMethod( GuiTextEditSliderCtrl, convert, F32, ( const char* inType,  const char* outType, F32 value),,
   "Converts a value from one type to another.n"
   "@param inType Type of the passed in value.n"
   "@param outType Type of to convert to.n"
   "@return Converted value or same value if it can't be converted." )
{
	return object->convert( EngineUnmarshallData< TextEditUnitType >()(inType), EngineUnmarshallData< TextEditUnitType >()(outType), value);
}
#2
03/22/2011 (7:38 pm)
Huh I tried my solution above and I guess it just works. At least it is so far lol. Feel sort of funny for asking it when it was the solution, just thought there would be something special you would have to do.

Guess it will be helpful to others.
#3
03/23/2011 (8:04 am)
Yeah, the added parenthesis do make it look strange. Anyway, EngineUnmarshallData is what the new console method/function system uses internally to convert from strings into native types.