How To use An enum as a string

Hello, I wanted to use datatable so I created UStruct and datatable. All of them is working well but there is just one issue. While using FindDatatableRow, I want to pass an enum to function as a string. What I mean, normally FindDatatableRow takes argument as an string to determine which row to find. Instead of writing a string, I want to pass an enum that stores some names matching the rows. Is it possible? My goal is creating basic Rock Paper Scissors game. Datatable will store all mesh for all character. I will edit its enum via editor and its automaticly change properties according to correspoding row.

table = data->FindRow<FRtable>(FName("abc"), FString("asd"));

ENUM()
enum choose{
	rock
	paper
	scissors

};
USTRUCT()
struct FRtable : public FTableRowBase {
	GENERATED_BODY(BlueprintType)
		UPROPERTY(EditAnywhere)
		UStaticMesh* rmesh; // it will be change according to chracter 
	UPROPERTY(EditAnywhere)
		FName pointp;if my chracter is rock this will be scissors
	UPROPERTY(EditAnywhere)
		FName pointS;  //if my chracter is rock this will be papaer
	UPROPERTY(EditAnywhere)
		FName pointEq; if my chracter is rock this will be rock

};```

I want to placed an enum to "abc"

I am no sure I understood your question good, but I’ll try to help nevertheless. There are two approaches to this.

  1. Use UEnum::GetValueAsString(your_enum_reference)

  2. Create translator method
    .h file

Use UEnum and Mind Unreal Engine Naming conventions. Also, please name it to something more logical than “Choose” :smiley:

UENUM()
enum EChoose {
	Rock,
	Paper,
	Scissors,
};

FString GetMyEnumValue(EChoose MyEnum) const;

Then

.cpp

FString YourClass::GetMyEnumValue(EChoose MyEnum) const
{
	switch (MyEnum)
	{
		case Rock: { return "Rock"; }
		case Paper: { return "Paper"; }
		case Scissors: { return "Scissors"; }
		default: { // handle error or return some default value }
	}
}
1 Like

Given that the Unreal Header Tool already generates the translation table in question, re-implementing it yourself in a header/CPP file seems like un-needed work :slight_smile:
You should just use the built-in function!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.