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.
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.
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.
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.
I’m sure there’s some template master out there that can transform the usage of FUNCSIG into a constexpr, but the enum name is extracted only once. Note that I’m using it for debugging only, high performance is not a top priority for me.