Game Development Community

Enumerating an Enum in Script

by William Todd Scott · in Torque Game Engine · 01/12/2007 (12:13 pm) · 4 replies

Hi,

I am working on a a script based object editor gui. The gui builds its controls in script based on the persistent fields of an object. So far, I am able to handle most data types. However, I would like to create a guiPopupMenu control for persitent fields that are TypeEnum.

Is there a way to list the possible values for an enumerated type in order to populate the guiPopupMenu with the available options.

I believe the guiInspector control does this, but it is taking me a bit to track down how it works. Can someone point me in the right direction?

Thanks
Todd

#1
01/12/2007 (1:01 pm)
I'm pretty sure you can't do that without writing support functions yourself, though I could be wrong.
#2
01/12/2007 (2:28 pm)
Hey Stefan,

I don't mind writing support functions (I have had to do that to get the field type and what not), but I don't want to have to write custom support for EACH enum.

So, do you have any idea how, (if I have the object, the field, and the field type) I would go about getting the enum values?

Thanks
Todd
#3
01/12/2007 (2:35 pm)
Actually, no. I'm sorry. I usually implement it manually in a function which checks what string was passed from script, and then assigns it the correct enum value. Pretty ugly, but I think that's what some of the GUI controls do when you assign position settings and whatnot.

Hopefully someone else has a more swift solution, I would gladly switch myself.
#4
01/13/2007 (1:14 am)
Hey,

So, I was actually sitting right on the answer and I just didn't realize it. What else is new? :)

Stefan, in case you, or someone else, is interested...

Among others, I added these console methods to SimObject in order to be able to retrieve a field's type and, if it is a TypeEnum, the potential enum values.

ConsoleMethod(SimObject, getFieldType, const char *, 3, 3, "obj.getFieldType(fieldName);")
{
	const char *fieldName = StringTable->insert( argv[2] );
	return object->getDataFieldType( fieldName );
}

ConsoleMethod(SimObject, getFieldEnumValue, const char *, 4, 4, "obj.getFieldEnumCount(fieldName, index);")
{
	const char *fieldName = StringTable->insert( argv[2] );
	U32 index = dAtoi(argv[3]);
	return object->getDataFieldEnumValue(fieldName, index);
}


The support function for getting the field's type is as follows:

const char *SimObject::getDataFieldType(StringTableEntry slotName)
{
	if(mFlags.test(ModStaticFields))
	{
		const AbstractClassRep::Field *fld = findField(slotName);
		if(fld)
		{
			ConsoleBaseType *cbt = ConsoleBaseType::getType(fld->type);
			return cbt->getTypeName();
		}
	}

	if(mFlags.test(ModDynamicFields))
	{
		if(!mFieldDictionary) return "";
		const AbstractClassRep::Field *fld = findField(slotName);
		if(fld)
		{
			ConsoleBaseType *cbt = ConsoleBaseType::getType(fld->type);
			return cbt->getTypeName();
		}
	}

	return "";
}


The support function for getting a field's enum values is as follows:

const char * SimObject::getDataFieldEnumValue(StringTableEntry slotName, U32 index)
{
	if(mFlags.test(ModStaticFields))
	{
		const AbstractClassRep::Field *fld = findField(slotName);
		if(fld)
		{
			if(fld->type == TypeEnum)
			{
				if(index >= fld->table->size) return "";
				return fld->table->table[index].label;
			}
		}
	}

	if(mFlags.test(ModDynamicFields))
	{
		if(!mFieldDictionary) return "";
		const AbstractClassRep::Field *fld = findField(slotName);
		if(fld)
		{
			if(fld->type == TypeEnum)
			{
				if(index >= fld->table->size) return "";
				return fld->table->table[index].label;
			}
		}
	}

	return "";
}

Todd