Enums For Both C++ and BP
This wiki article was written by Rama.
This article is still in the process of being cleaned up, but it has been captured for preservation.
Overview
Dear Community,
Here's how you can create your own Enums that can be used with C++ and BP graphs!
Enums basically give you ability to define a series of related types with long human-readible names, using a low-cost data type.
These could be AI states, object types, ammo types, weapon types, tree types, or anything really :)
BP Graphs: Switch on Enum
For BP Graphs, one of the most wonderful things about ENUMS is the ability to use Switch on Enum() instead of having to do a series of branches and testing one value many times
C++ .h File
You need to add the UENUM definition above your class and then actually create a member variable in your class that you want to have be an instance of this enum.
If you want an enum to be used in many different classes (instances of this enum in many classes) you can define the enum in some class that holds all your other important definitions like USTRUCTS().
Testing the Value in the C++
Get Name of Enum as String
Templatized Version
{ return FString("Invalid"); }
}
// Example usage GetEnumValueAsString<EVictoryEnum>("EVictoryEnum", VictoryEnum))); </syntaxhighlight>
Also, if you want to avoid retyping the enum class name as a string on every call to GetEnumValueAsString, you can also define a c++ macro in the .h file where the function is defined.
For example, if you have defined GetEnumValueAsString in a class UTextUtil in TextUtil.h, you would have this macro
<syntaxhighlight lang="cpp">
define EnumToString(EnumClassName, ValueOfEnum) UTextUtil::GetEnumValueAsString<EnumClassName>(FString(TEXT(#EnumClassName)), (ValueOfEnum))
</syntaxhighlight>
This way in any other file where you want a FString from an enum value, you would do:
GetEnumFromString
If you want to retrieve an Enum value after storing the Enum as a string, here is how!
Summary
Now you know how to make enums that are project specific, that can be used in both C++ and Blueprints!
Enjoy!
Rama
Last updated