Enum GetDisplayNameText - in non editor build

Hi,

How do you manage to show enum value as string in UI in “non editor builds”?

GetDisplayNameText is not valid in non editor build due to #WITH_EDITOR around it.

Should I implement a GetString with a switch inside my Enum?

Thanks,

The short of it is: you don’t. There is a good reason that function only exists in editor builds: the associated metadata storing the display string also only exists in editor builds.

If you want to present text to the user, you should always use FText and you should strive to use the LOCTEXT macros. The reason for this is FText is localized while everything else is not. Even if you don’t plan to localize, it’s a good habit to get into for minimal effort. I’ve shipped projects where localization was left by the wayside until the last minute and spent days revisiting every single string in the game to make it localized. It’s not fun.

Whenever I have enums that will have a displayed equivalent, I just create an uproperty array of FTexts matching that enum wherever I need to use them:


	/** Localized names for submenus. */
	UPROPERTY( Category=PlayerMenu, EditAnywhere )
	FText SubMenuNames EPlayerMenu::EPlayerMenu_MAX ];

That notation has the little known advantage of making your array show in property editors with enum values instead of array indices. Basically, instead of:

[1] [My text value for EPlayerMenu::One]
[2] [My text value for EPlayerMenu::Two]

you get a friendlier:

[One] [My text value for EPlayerMenu::One]
[Two] [My text value for EPlayerMenu::Two]

Thanks for the answer.

FText part: I’m already put any UI string in FText. I should have put the method name as GetText and not GetString. My goal is to have localized string and GetDisplayNameText was handle it.

Solution part:
Where are you putting the FText property that you shared, in the UI Class? inside the Enum Namespace ? in a Struct?
My goal is to be able to reuse the text display anywhere I want without redefining it. This is why the Metadata things was good. Now I have the reason behind the fact that I can’t use GetDisplayNameText.

In that exemple, since that text is only used for that player menu UMG widget, I’ve stored it in that widget. I try to keep localized text on the object that “owns” it. For instance, pickups store their own name, as do characters and some widgets. For truly decentralized text, I have a string manager stored in my game singleton, but those cases have been few and far between.

Thanks for your feedback

In case someone has a need for this in the future, I’ve written an article about this problem. We open sourced a subset of our common module. Feel free to check it out on gitlab.