Enums are commonly used in C++ as flags or inputs to switch statements. However, what if you want to pass an enum
value to or from C++ from a Blueprint? Alternatively, if you want to use a switch
statement in Blueprint that uses an enum
from C++, how do you let the Blueprint editor know that your enum
should be accessible within the editor? This recipe shows you how to make enums visible in Blueprint.
StaticMeshActor
class called Tree
using the editor.UENUM(BlueprintType) enum TreeType { Tree_Poplar, Tree_Spruce, Tree_Eucalyptus, Tree_Redwood };
UPROPERTY
in the Tree
class:UPROPERTY(BlueprintReadWrite) TEnumAsByte<TreeType> Type;
Tree
constructor:auto MeshAsset = ConstructorHelpers::FObjectFinder<UStaticMesh>(TEXT("StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'")); if (MeshAsset.Object != nullptr) { GetStaticMeshComponent()->SetStaticMesh(MeshAsset.Object); GetStaticMeshComponent()->bGenerateOverlapEvents = true; } GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);
MyTree
, based on Tree
.MyTree
, click on the Construction Script tab.treetype
. There is a Get number of entries in TreeType node.ToByte
node.ToByte
node to the input of the SET Type node. You'll see an extra conversion node automatically appear.Print String
node after the Event BeginPlay event node.Format Text
node, and connect its output to the input of the Print String
node. A conversion node will be added for you.Format Text
node, add My Type is {0
}! to the textbox.Type
from the variables section of the Blueprint into the graph selecting Get from the menu.Type
output pin.Format Text
labelled 0
.StaticMeshActor
as the base class for our Actor
so that we can easily give it a visual representation in the level.UENUM
macro.enum
as Blueprint-available using the BlueprintType
specifier.enum
declaration is just the same as we would use in any other context.Tree
requires a TreeType
. Because tree has tree-type is the relationship we want to embody, we include an instance of TreeType
in our Tree
class.UPROPERTY()
to make the member variable accessible to the reflection system.BlueprintReadWrite
specifier to mark the property as having both get and set support within Blueprint.TEnumAsByte
template when used in UPROPERTY
, so we declare an instance of TEnumAsByte<TreeType>
as the Tree's Type
variable.Tree
are simply the standard load and initialize our static mesh component preamble used in other recipes.Tree
class so that we can demonstrate the Blueprint-accessibility of the TreeType enum
.TreeType enum
.TreeType enum
type to retrieve a value to store as our Type
.ToByte
node, which can then be implicitly converted by Blueprint into an enum
value.BeginPlay
event within the Event Graph tab.Print String
node.Format Text
node.Format Text
node takes terms enclosed in curly braces, and allows you to substitute other values for those terms returning the final string.Type
into the Format Text
node, we need to convert our variable stores from the enum
value into the actual name of the value.Type
variable, then using the Enum to Name
node.Name
, or FNames
in native code, are a type of variable that can be converted to strings by Blueprint, so we can connect our Name
to the input on the Format Text
node.3.149.214.32