
Creating a Blueprint from your custom UCLASS
Blueprinting is just the process of deriving a Blueprint class for your C++ object. Creating Blueprint-derived classes from your UE4 objects allows you to edit the custom UPROPERTY
visually inside the editor. This avoids hardcoding any resources into your C++ code. In addition, in order for your C++ class to be placeable within the level, it must be Blueprinted first. But this is only possible if the C++ class underlying the Blueprint is an Actor
class-derivative.
Note
There is a way to load resources (such as textures) using FStringAssetReferences
and StaticLoadObject
. These pathways to loading resources (by hardcoding path strings into your C++ code) are generally discouraged, however. Providing an editable value in a UPROPERTY()
, and loading from a proper concretely typed asset reference is a much better practice.
Getting ready
You need to have a constructed UCLASS
that you'd like to derive a Blueprint class from (see the Making a UCLASS – deriving from UObject section earlier in this chapter) in order to follow this recipe. You must have also marked your UCLASS
as Blueprintable
in the UCLASS
macro for Blueprinting to be possible inside the engine.
Tip
Any UObject
-derived class with the meta keyword Blueprintable
in the UCLASS
macro declaration will be Blueprintable
.
How to do it…
- To Blueprint your
UserProfile
class, first ensure thatUCLASS
has theBlueprintable
tag in theUCLASS
macro. This should look as follows:UCLASS( Blueprintable ) class CHAPTER2_API UUserProfile : public UObject
- Compile and run your code.
- Find the
UserProfile
C++ class in the Class Viewer (Window | Developer Tools | Class Viewer). Since the previously createdUCLASS
does not derive fromActor
, to find your customUCLASS
, you must turn off Filters | Actors Only in the Class Viewer (which is checked by default):Turn off the Actors Only check mark to display all the classes in the Class Viewer. If you don't do this, then your custom C++ class may not show!
Tip
Keep in mind that you can use the small search box inside the Class Viewer to easily find the
UserProfile
class by starting to type it in: - Find your
UserProfile
class in the Class Viewer, right-click on it, and create a Blueprint from it by selecting Create Blueprint… - Name your Blueprint. Some prefer to prefix the Blueprint class name with
BP_
. You may choose to follow this convention or not, just be sure to be consistent. - Double-click on your new Blueprint as it appears in the Content Browser, and take a look at it. You will be able to edit the Name and Email fields for each
UserProfile
Blueprint instance you create.
How it works…
Any C++ class you create that has the Blueprintable
tag in its UCLASS
macro can be Blueprinted within the UE4 editor. A Blueprint allows you to customize properties on the C++ class in the visual GUI interface of UE4.