Changing enum to string

//I created an enum:

UENUM(BlueprintType)
enum class EStaminaStatus : uint8
{
ESS_Normal UMETA(DisplayName = “Normal”),
ESS_BelowMinimum UMETA(DisplayName = “BelowMinimum”),
ESS_Exhausted UMETA(DisplayName = “Exhausted”),
ESS_ExhaustedRecovering UMETA(DisplayName= “Exhausted Recovering”),
ESS_MAX UMETA(DisplayName = “DefaultMax”)

};
//and i created the function to change the enum to string:

static FORCEINLINE FString GetEnumValueAsString(const FString& Name, TEnum Value)
{
const EStaminaStatus* enumPtr = FindObject(ANY_PACKAGE, *Name, true);
if (!enumPtr) return FString(“Invalid”);
return enumPtr->GetNameByValue((int64)Value).ToString();
}
//this is all in the header file
//now this is the outputlog that should print the current value of the enum in string in begin play function

UE_LOG(LogTemp, Warning, TEXT("GetStaminaStatus: %s "),GetEnumValueAsString< EStaminaStatus>(EStaminaStatus, StaminaStatus))

//i used this website: Enums For Both C++ and BP - UE4: Guidebook
//What did i do wrong???

2 Likes

UE_LOG(LogBlueprint, VeryVerbose, TEXT(“Your Enum: %s”), *UEnum::GetValueAsString(EStaminaStatus::ESS_Normal));

UEnum has several methods to get the direct string or the meta, translation etc.

2 Likes

I want to print out every time a different StaminaStatus. So i do the Log in the tick function and it should print every time the StaminaStatus it has instead of only one specific status

Then you can store it in a variable and print the variable:

// EStaminaStatus YourStaminaStatus = .....
UE_LOG(LogBlueprint, VeryVerbose, TEXT("Your Enum: %s"), *UEnum::GetValueAsString(YourStaminaStatus));
4 Likes
	UE_LOG(LogTemp, Warning, TEXT("My Enum: %s") ,*UEnum::GetValueAsString(GetNetMode()));

Why this doesn´t work?

If GetNetMode returns an enum, the enum must probably be a UEnum. Else there is no reason it wouldn’t work. If you have to work with an enum from engine source code which is not a UEnum you could also convert enum to byte or use (TEnumAsByte) to print the byte value in the logger.

So how exactly am i doing this?
UE_LOG(LogTemp, Warning, TEXT(“My Enum: %s”) ,*UEnum::TEnumAsByte(GetNetMode()));

Im just a beginner in c++ so i have it hard on multiplayer games

Can you try this, it would print numbers (enum indexes) if you can’t get a string the other way.

UE_LOG(LogTemp, Warning, TEXT("My Enum: %d"), static_cast<uint8>(GetNetMode()));

If there are any errors left I’d like to see the error.

1 Like

Thanks a lot!