Converting string to enum

This may seem like a weird question, but i have those strings that have been created with EnumToString node to populate a ComboBox, now i would like to get those ComboBox strings back to their respective enum so i can use them, how can i do that ?

Well,

you could do a switch on string or many if-blocks to get your enum based on the string.

Another idea could be to have a struct with enum and string as its member. Now instead of filling your combobox with strings, you fill it with a custom widget (f.e. ComboBox Item), this widget will have your struct as a member is is now able te return the string OR the enum.

This worked for me:

5 Likes

Simple way - in your enum .h file put two functions:

  • GetStringForEnum(enum e) - switch on enum and return string
  • GetEnumForString(fstring s) - if/else on fstring and return enum

That way every file that includes the enum also includes the conversion functions.

In regard to the topmost answer, using a bunch of if’s or a switch in order to convert a string to a number and then back to an enum is a really bad and unoptimized way of doing it.



enum EMyEnum
{
    // ...
};

FString fsComboBoxValue = TEXT("1"); // Your combobox value

int32 iEnumValue = FCString::Atoi(*fsComboBoxValue);

EMyEnum myEnum = EMyEnum((uint8)iEnumValue);


If FCString is undefined you might need to include: Runtime/Core/Public/Misc/CString.h

2 Likes

Thanks to TheAxcell’s code above, I can improve the code to get enum value by name.
At first, create your own enum like this:
-------------------code start-------------------------------
UENUM(BlueprintType)
enum class EAnimState : uint8
{
Default,
Idle,
Jump,
};
-------------------code end-----------------------------------

Then, parse string to your enum type anywhere you want:

-------------------code start-------------------------------
const UEnum* AnimStateEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT(“EAnimState”), true);
if (AnimStateEnum) {
int32 Index = AnimStateEnum->GetIndexByName(AnimStateName);
EAnimState AnimState = EAnimState((uint8)Index);
return AnimState;
}
-------------------code end----------------------------------
Note:

  1. If Index is out of range, first element in EAnimState will be returned, so, keep your default enum value in the first place.
  2. This works in Unreal Engine version 4.17.X.
4 Likes

How I tackle this is with BP:
I make a map -> String - Enum.
If the string corresponds in the map we get the enum as return.

In my project I have objects that can reside in an area. But also in multiple areas. So i make a string like house/garage/garden.
I split the string by ‘/’. Each return is checked in the map which gives me the corresponding enum. The ‘Right S’ is the new string to search for. If null, we exit otherwise we loop.

2 Likes

I had exactly the same problem, exactly in the same context.

Thanks to @Wobbleyheadedbob for hinting Find Option Index. [ATTACH=JSON]{“alt”:“ComboBox: From string to Enum”,“data-align”:“none”,“data-size”:“full”,“data-tempid”:“temp_164599_1557942614516_64”,“title”:“checkpoints.PNG”,“caption”:“ComboBox: From string to Enum”}[/ATTACH]

At startup populate a String/Enum Map. Query the map with ‘Find’ using your string as input. It will return the enum. E_Settings is just the name of my enum, substitute your own. The node leading into the ADD which is converting enum to string is the ‘Enum to String’ node. Be sure NOT to use the ‘ToString’ node.

2 Likes

thank you great stuff
good Graphenes but still bad Graphene

Best solution for scalability. You can replace GetIndexByName with GetIndexByNameString if you have a FString.

1 Like

Blueprint maybe a bit brute force, but this should work for you. Unfortunately, there’s no break option for ForEach over enums.

5 Likes

This is what I have been using, hope it helps someone :grin:

// .h
UFUNCTION(BlueprintPure, Category = "BPFunctions")
static ERegionInfo GetRegionInfoFromString(FString RegionString);


// .cpp
ERegionInfo PROJ_BlueprintFunctions::GetRegionInfoFromString(FString RegionString)
{
	const FString ERegionInfoString{ TEXT("ERegionInfo") };
	const UEnum* RegionInfoEnum{ FindObject<UEnum>(ANY_PACKAGE, *ERegionInfoString, true) };
	
	if (RegionInfoEnum) 
	{
		int32 Index{ RegionInfoEnum->GetIndexByNameString(RegionString) };
		const ERegionInfo RegionInfoCheck{ ERegionInfo((uint8)Index) };
		
		// Check if the enum is valid
		if (UEnum::GetValueAsString(RegionInfoCheck) != TEXT("None"))
		{
			return RegionInfoCheck;
		}
	}

	// Fallback if nothing is found
	const ERegionInfo RegionInfo{ ERegionInfo::RE_NoSelection };
	return RegionInfo;
}

Also if this helps anyone, if you need to convert the enum to its non-display name as a string.

FString PROJ_BlueprintFunctions::GetRegionInfoNonDisplayString(ERegionInfo RegionInfo)
{
	FString ERegionInfoString{ UEnum::GetValueAsString(RegionInfo) };
	ERegionInfoString.RemoveFromStart(TEXT("ERegionInfo::"));

	return ERegionInfoString;
}

I’m on the same process for now … i don’t know if it’s optimal but it works.
Thanks for your post !

1 Like

thanks, this is clean and still works in ue 5.3

if (const UEnum* ECultureTypePtr = StaticEnum<ECultureType>())
{
    const int32 Index = ECultureTypePtr->GetIndexByNameString(CurrentCultureName);
    const ECultureType GetCultureType = static_cast<ECultureType>(Index);

}

1 Like