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 ?


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:

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

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-------------------------------
enum class EAnimState : uint8
-------------------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----------------------------------

  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.
  3. If this works for you or If there is any problem, please let me know:)

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.

1 Like

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.

1 Like