
Making a UCLASS – deriving from UObject
When coding with C++, you can have your own code that compiles and runs as native C++ code, with appropriate calls to new
and delete
to create and destroy your custom objects. Native C++ code is perfectly acceptable in your UE4 project as long as your new
and delete
calls are appropriately paired so that no leaks are present in your C++ code.
You can, however, also declare custom C++ classes, which behave like UE4 classes, by declaring your custom C++ objects as UCLASS
. UCLASS
use UE4's Smart Pointers and memory management routines for allocation and deallocation according to Smart Pointer rules, can be loaded and read by the UE4 Editor, and can optionally be accessed from Blueprints.
Tip
Note that when you use the UCLASS
macro, your UCLASS
object's creation and destruction must be completely managed by UE4: you must use ConstructObject
to create an instance of your object (not the C++ native keyword new
), and call UObject::ConditionalBeginDestroy()
to destroy the object (not the C++ native keyword delete
). How to create and destroy your UObject
-derivative classes is outlined in the Instantiating UObject-derived classes (ConstructObject <> and NewObject <>) and Destroying UObject-derived classes sections later in this chapter.
Getting ready
In this recipe, we will outline how to write a C++ class that uses the UCLASS
macro to enable managed memory allocation and deallocation as well as to permit access from the UE4 Editor and Blueprints. You need a UE4 project into which you can add new code to use this recipe.
How to do it...
To create your own UObject
derivative class, follow the steps below:
- From your running project, select File | Add C++ Class inside the UE4 Editor.
- In the Add C++ Class dialog that appears, go to the upper-right side of the window, and tick the Show All Classes checkbox:
- Create a
UCLASS
by choosing to derive from theObject
parent class.UObject
is the root of the UE4 hierarchy. You must tick the Show All Classes checkbox in the upper-right corner of this dialog for theObject
class to appear in the list view. - Select
Object
(top of the hierarchy) as the parent class to inherit from, and then click on Next.Tip
Note that although
Object
will be written in the dialog box, in your C++ code, the C++ class you will deriving from is actuallyUObject
with a leading uppercaseU
. This is the naming convention of UE4:UCLASS
deriving fromUObject
(on a branch other thanActor
) must be named with a leadingU
.UCLASS
deriving fromActor
must be named with a leadingA
(Chapter 4, Actors and Components).C++ classes (that are not
UCLASS
) deriving from nothing do not have a naming convention, but can be named with a leadingF
(for example,FAssetData
), if preferred.Direct derivatives of
UObject
will not be level placeable, even if they contain visual representation elements such asUStaticMeshes
. If you want to place your object inside a UE4 level, you must at least derive from theActor
class or beneath it in the inheritance hierarchy. See Chapter 4, Actors and Components for how to derive from theActor
class for a level-placeable object.This chapter's example code will not be placeable in the level, but you can create and use Blueprints based on the C++ classes that we write in this chapter in the UE4 Editor.
- Name your new
Object
derivative something appropriate for the object type that you are creating. I call mineUserProfile
. This comes off asUUserObject
in the naming of the class in the C++ file that UE4 generates to ensure that the UE4 conventions are followed (C++UCLASS
preceded with a leadingU
). - Go to Visual Studio, and ensure your class file has the following form:
#pragma once #include "Object.h" // For deriving from UObject #include "UserProfile.generated.h" // Generated code // UCLASS macro options sets this C++ class to be // Blueprintable within the UE4 Editor UCLASS( Blueprintable ) class CHAPTER2_API UUserProfile : public UObject { GENERATED_BODY() };
- Compile and run your project. You can now use your custom
UCLASS
object inside Visual Studio, and inside the UE4 Editor. See the following recipes for more details on what you can do with it.
How it works…
UE4 generates and manages a significant amount of code for your custom UCLASS
. This code is generated as a result of the use of the UE4 macros such as UPROPERTY
, UFUNCTION
, and the UCLASS
macro itself. The generated code is put into UserProfile.generated.h
. You must #include
the UCLASSNAME.generated.h
file with the UCLASSNAME.h
file for compilation to succeed. Without including the UCLASSNAME.generated.h
file, compilation would fail. The UCLASSNAME.generated.h
file must be included as the last #include
in the list of #include
in UCLASSNAME.h
:

The error that occurs when a UCLASSNAME.generated.h
file is not included last in a list of includes is as follows:
>> #include found after .generated.h file - the .generated.h file should always be the last #include in a header
There's more…
There are a bunch of keywords that we want to discuss here, which modify the way a UCLASS
behaves. A UCLASS
can be marked as follows:
Blueprintable
: This means that you want to be able to construct a Blueprint from the Class Viewer inside the UE4 Editor (when you right-click, Create Blueprint Class… becomes available). Without theBlueprintable
keyword, the Create Blueprint Class… option will not be available for yourUCLASS
, even if you can find it from within the Class Viewer and right-click on it:- The Create Blueprint Class… option is only available if you specify
Blueprintable
in yourUCLASS
macro definition. If you do not specifyBlueprintable
, then the resultantUCLASS
will not beBlueprintable
. BlueprintType
: Using this keyword implies that theUCLASS
is usable as a variable from another Blueprint. You can create Blueprint variables from the Variables group in the left-hand panel of any Blueprint's EventGraph. IfNotBlueprintType
is specified, then you cannot use this Blueprint variable type as a variable in a Blueprints diagram. Right-clicking theUCLASS
name in the Class Viewer will not show Create Blueprint Class… in its context menu:
Any UCLASS
that have BlueprintType
specified can be added as variables to your Blueprint class diagram's list of variables.
You may be unsure whether to declare your C++ class as a UCLASS
or not. It is really up to you. If you like Smart Pointers, you may find that UCLASS
not only make for safer code, but also make the entire code base more coherent and more consistent.
See also
- To add additional programmable
UPROPERTY
to the Blueprints diagrams, see the Creating a user-editable UPROPERTY section below. For details on referring to instances of yourUCLASS
using appropriate Smart Pointers, refer to Chapter 3, Memory Management and Smart Pointers.