Adding ToString to a custom Enum?

I am trying to add a ToString function to my custom Enum in C++ because if I use Blueprints I can convert my custom enum to string. C++ the only way I found from other header files was like this (without the UENUM macro)

UENUM(BlueprintType)
namespace EKaoriGameUnit
{
	enum Type
	{
		Alive	UMETA(DisplayName = "Alive"),
		Dead	UMETA(DisplayName = "Dead")
	};

	inline const TCHAR* ToString(EKaoriGameUnit::Type State)
	{
		switch (State)
		{
		case Alive:
			return TEXT("Alive");
		case Dead:
			return TEXT("Dead");
		}
		return TEXT("Unknown");
	}
}

Since I want to use this or other Enums later in Blueprints I added the UENUM macro but now the UHT throws this error

KaoriGameTypes.h(10): error : In TemporaryUHTHeader_KaoriGameTypes: Missing ‘}’ in ‘Enum’

#Alternative / Skip a Step

If you are willing to try an alternative you can make a BlueprintFunctionLibrary for yourself that will do the same as your function does.

Keep in mind blueprint function libraries can be used anywhere! Even in anim blueprints! anywhere!

so it is not that much of an issue :slight_smile:

You’re just declaring the enum strings in a different but absolutely blueprint global context


I think of this as skipping a step because you’d need to write up some sort of BP interface for your proposed Enum function anyways. But now it is instantly BP global and you dont have to write it twice


You can also use the static blueprint functions in any .cpp file like any other static library functions

#Wiki

:slight_smile:

Rama

That is definitely something to consider at a later point thanks, but for now I need a way to get the Text representation for my enum. What I did in Blueprints was searching the level for camera actors (I tagged them with a specific text value) belonging to the team of the player.
I was trying the same in C++ but there is no function to convert an Enum value to string :frowning:

The UENUM parsing of a namespace gets rather cranky if you have something other than just the Enum in it. You can do this instead to get the same behavior in terms of the namespacing but with functional compiling!

UENUM(BlueprintType)
namespace EKaoriGameUnit
{
	enum Type
	{
		Alive	UMETA(DisplayName = "Alive"),
		Dead	UMETA(DisplayName = "Dead")
	};
}

namespace EKaoriGameUnit
{
	inline const TCHAR* ToString(EKaoriGameUnit::Type State)
	{
		switch (State)
		{
		case Alive:
			return TEXT("Alive");
		case Dead:
			return TEXT("Dead");
		}
		return TEXT("Unknown");
	}
}

To get your enum to string in a less switchy manner:

FindObject<UEnum>(ANY_PACKAGE, TEXT("EKaoriGameUnit"), true)->GetDisplayNameText(State);

Thank you. That works perfect :slight_smile:

#Thank You Marc!

wow that’s awesome Marc!

Thanks!

#Added to Wiki

I added your awesome get Enum as String function to the Wiki!

GetDisplayNameText function is only accessible when WITH_EDITOR is defined, so pay attention. I’ve corrected wiki page mentioned above.

What if the enum text (to be displayed) should be different than the enum values? For example:

enum Type
{
EType1,
EType2
}

If I want to print the text version of EType1 into “Alive” and EType2 into “Dead”, then your posting on Wiki won’t work. Do you have another Wiki which can do what I explain here?