x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Conversion of Enum to String

Hello, So I have been trying to convert my Enumeration values to an FString.

I followed this post: https://wiki.unrealengine.com/Enums_For_Both_C%2B%2B_and_BP

and ended up with this:

 FString getBodyEnumAsString(EBodyPosition value){
     const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, TEXT("EBodyPosition"), true);
     if (!EnumPtr) return FString("Invalid");
 
     return EnumPtr->GetEnumName((int32)value);
 }

This is not working though, something about unresolved externals. I am sure I am making some mistake somewhere, and if someone could help me with it, that would be great.

Thanks,

Product Version: UE 4.9
Tags:
more ▼

asked Nov 08 '15 at 09:49 PM in C++ Programming

avatar image

joelComberiati
122 26 22 29

avatar image joelComberiati Nov 11 '15 at 06:34 AM

Still being an issue, any help?

avatar image erinacea Nov 11 '15 at 08:34 AM

I'm doing something similar for one of my enums and it works perfectly fine. Where do you declare the function and how does it get called?

avatar image joelComberiati Nov 13 '15 at 06:00 AM

I simply call it when i create my object, by just passing my Enum value through: getBodyEnumAsString(EBodyPosition::Waist);

avatar image Alnek Mar 14 '16 at 11:29 AM

something about unresolved externals

would you clarify, is it compilation or run-time issue?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

4 answers: sort voted first
 template<typename T>
 FString EnumToString(const FString& enumName, const T value, const FString& defaultValue) const
 {
     UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, *enumName, true);
     return pEnum
         ? ExpandEnumString(pEnum->GetNameByIndex(static_cast<uint8>(value)).ToString(), enumName)
         : defaultValue;
 }

 FString ExpandEnumString(const FString& name, const FString& enumName)
 {
     FString expanded(name);
     FString spaceLetter("");
     FString spaceNumber("");
     FString search("");
     expanded.ReplaceInline(*enumName, TEXT(""), ESearchCase::CaseSensitive);
     expanded.ReplaceInline(TEXT("::"), TEXT(""), ESearchCase::CaseSensitive);
     for (TCHAR letter = 'A'; letter <= 'Z'; ++letter)
     {
         search = FString::Printf(TEXT("%c"), letter);
         spaceLetter = FString::Printf(TEXT(" %c"), letter);
         expanded.ReplaceInline(*search, *spaceLetter, ESearchCase::CaseSensitive);
     }
     for (TCHAR number = '0'; number <= '9'; ++number)
     {
         search = FString::Printf(TEXT("%c"), number);
         spaceNumber = FString::Printf(TEXT(" %c"), number);
         expanded.ReplaceInline(*search, *spaceNumber, ESearchCase::CaseSensitive);
     }
     expanded.ReplaceInline(TEXT("_"), TEXT(" -"), ESearchCase::CaseSensitive);
     expanded = expanded.RightChop(1).Trim().TrimTrailing();
     return expanded;
 }

more ▼

answered Jun 01 '16 at 07:59 PM

avatar image

gehill
31 4 3

avatar image gehill Jun 01 '16 at 08:03 PM

This will work in editor and game modes.

avatar image Missing A Few Heads Sep 01 '17 at 05:36 AM

I think this is my first post... had to say... you are a freakin' wizard. Why is this not in the engine? Thanks!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

For the people that dislike the string parameter, I found a solution to this. I didn't like the string because it would present problems when refactoring (renaming) the enum.

 #define stringify( name ) # name
 
 UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT(stringify(YourEnumType)), true);

The stringify macro literally converts the characters of its input to chars. this works great and it will refactor the enum because it's now used as a type instead of a string. I hope this helps anyone.

more ▼

answered Dec 14 '17 at 12:11 AM

avatar image

92Rob
98 1 3 8

avatar image Sazonov Jul 10 '18 at 09:41 AM

Not working if enum is inside namespace or class.

avatar image Hyrex93 Aug 27 '18 at 05:47 AM

This is by far the most nice way to deal with Enum stringizing.

As for namespaces enum, you can try to declare using namespace within the implementation body before calling this macro. I've seen some usage like this.

 namespace SomeNamespace;
 {
   enum class EExample 
   {
     X,Y
   }
 }

within implementation body

 {
   using namespace SomeNamespace;
   stringify(EExample);
 }

In addition, one can always write double macro to make it better. This was after referring @MissingAFewHead templated function.

 #define stringify(name) #name
 #define EnumValStr(Enum, Val)  EnumToString( (stringify(Enum)) , Val)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Using the info from both of these posts I wrote the following function that works:

 template<typename T>
 static FString EnumToString(const FString& enumName, const T value)
 {
     UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, *enumName);
     return *(pEnum ? pEnum->GetNameStringByIndex(static_cast<uint8>(value)) : "null");
 }

And I just put it in a header file along with my enums. Thanks yall!

more ▼

answered Sep 01 '17 at 05:57 AM

avatar image

Missing A Few Heads
6 1 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

If you'd like to do this without creating a function to convert the enum, here's a simple way to use the method outlined above:

 void AMyGameWeapon::SetWeaponState(EWeaponState::Type NewState)
 {
      const UEnum* WeaponStateEnum = FindObject<UEnum>(ANY_PACKAGE, TEXT("EWeaponState"));
      UE_LOG(LogMyGameWeapon, Log, TEXT("SetWeaponState called on weapon %s with state %s (Role %s)")
             , *GetNameSafe(this)
             , *(WeaponStateEnum ? WeaponStateEnum->GetEnumName(NewState) : TEXT("<Invalid Enum>"))
             , *UEnum::GetValueAsString(TEXT("Engine.ENetRole"), Role));

The first line finds a pointer to the enum object from its namespace, and then in the UE_LOG call, we use that enum pointer to get a string value to output. Important to note is that the pointer isn't guaranteed to be valid, so use a ternary operator to ensure that you safely handle a case where the pointer doesn't exist.

This UE_LOG statement also supplies an example of grabbing a string from an enum that isn't enclosed in a namespace and whose path is known.

more ▼

answered Oct 07 '16 at 10:44 PM

avatar image

KevODoom
176 5 8 19

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question