Enum to String in Unreal Engine: How I Learned to Stop Hardcoding and Love Type Safety

biq
3 min readOct 19, 2020
Just imagine she is searching for the right strings.

Intro

There are things, that come up time and time again. One of those things is creating a string version of Enum values in Unreal Engine. For this and other cases, we started to condense some of our utility and helper functions into a module. This module can then be used as part of a plugin, or as a standalone game module. In this article, I want to highlight our approach for to stringify enum values.

Setup

API Example

Imagine we want to log the value of an enum type called EWlanNotificationMSM. The API would look like this.

Let’s make this happen.

Type safety

One of the goals I had in mind, when designing BIQStringify was the ability to work with a type safe call signature. Unreal Engine already provides a handy function for retrieving type information, called FindObject.

It expects you to specify the meta type (UEnum in this case) as template argument, the package root from which to search, the declaration name of the type and if the type should be matched exactly. Even if we abstract this now, passing the type as string is not really the type safe approach I had in mind. So next step is fetching type information.

Function call context

Luckily, most compilers define a context macro, allowing you to access the name of the current function. I abstracted the differences for compiling on Linux, Mac and Windows in a macro called CurrentFunctionName.

--

--