This chapter focuses on how to create C++ classes and structs that integrate well with the UE4 Blueprints editor. These classes are graduated versions of the regular C++ classes, and are called UCLASS
.
Using UCLASS
is a great practice. The UCLASS
macro, if configured correctly, can possibly make your UCLASS
Blueprintable. The advantage of making your UCLASS
Blueprintable is that it can enable your custom C++ objects to have Blueprints visually editable properties (UPROPERTY
) with handy UI widgets such as text fields, sliders, and model selection boxes. You can also have functions (UFUNCTION
) that are callable from within a Blueprints diagram. Both of these are shown in the following screenshots:
On the left, two UPROPERTY
decorated class members (a UTexture
reference and an FColor
) show up for editing in a C++ class's Blueprint. On the right, a C++ function GetName
marked as BlueprintCallable
UFUNCTION
shows up as callable from a Blueprints diagram.
The following are the topics that we will cover in this chapter:
UCLASS
– deriving from UObject
UPROPERTY
UPROPERTY
from BlueprintsUCLASS
as the type of a UPROPERTY
UCLASS
UObject
-derived classes (ConstructObject <>
and NewObject <>
)UObject
-derived classesUSTRUCT
UENUM()
UFUNCTION
You will notice that the sample objects we create in this class, even when Blueprintable, will not be placed in levels. That is because in order to be placed in levels, your C++ class must derive from the Actor
base class, or below it. See Chapter 4, Actors and Components for further details.
The UE4 code is, typically, very easy to write and manage once you know the patterns. The code we write to derive from another UCLASS
, or to create a UPROPERTY
or UFUNCTION,
is very consistent. This chapter provides recipes for common UE4 coding tasks revolving around basic UCLASS
derivation, property and reference declaration, construction, destruction, and general functionality.
3.15.226.120