I want to get and set enums by their property name.
I think i can get a enum’s value by name with this code:
FProperty* property = Target->GetClass()->FindPropertyByName(Name);
EMyEnum* propertyAddress = property->ContainerPtrToValuePtr<EMyEnum>(Target);
EMyEnum = *propertyAddress;
However this method would only work for the enumType “EMyEnum”.
I am looking for a more generic method.
1 Like
.h---
/** Converts a UENUM to string */
static FString EnumToString(const TCHAR* EnumName, const int32 EnumValue);
/** Gets the index of a UENUM item given the enum item as a string. Essentially the reverse of EnumToString. */
static int32 StringToEnumIndex(const TCHAR* EnumName, const FString& InString);
.cpp---
FString FMyModule::EnumToString(const TCHAR* EnumName, const int32 EnumValue)
{
const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, EnumName, true);
if (!EnumPtr)
{
return NSLOCTEXT("Enum not found", "Enum not found", "Enum not found").ToString();
}
return EnumPtr->GetNameStringByIndex(EnumValue);
}
int32 FMyModule::StringToEnumIndex(const TCHAR* EnumName, const FString& InString)
{
const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, EnumName, true);
if (EnumPtr)
{
return EnumPtr->GetIndexByNameString(InString);
}
return INDEX_NONE;
}
I think you can use the UEnum* ptr to also set by index, but I don’t have the engine open right now to check.
1 Like