Enum to String in Unreal Engine: How I Learned to Stop Hardcoding and Love Type Safety
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.
Imagine we want to log the value of an enum type called EWlanNotificationMSM. The API would look like this.
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.