Announcement

Collapse
No announcement yet.

Converting string to enum

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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 ?

    #2
    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.
    Live long and prosper.

    Comment


      #3
      This worked for me:
      Click image for larger version

Name:	Enum-from-ComboBox.png
Views:	4674
Size:	239.2 KB
ID:	1491316
      Attached Files

      Comment


        #4
        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.

        Comment


          #5
          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.

          Code:
          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

          Comment


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

            Comment


              #7
              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.

              Comment


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

                Thanks to Wobbleyheadedbob for hinting Find Option Index.
                ComboBox: From string to Enum
                ComboBox: From string to Enum
                Last edited by joel.m3c; 05-15-2019, 01:53 PM.

                Comment


                  #9
                  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.

                  Comment

                  Working...
                  X