Chapter 2 : Creating UE Properties¶
1. Creating UCLASS¶
It’s perfectally accaptable using naive C++ Object Class and new , delete operaters in UE Classes,
but using UCLASS macro can utilize UE’s build in smart pointer and garbage collection functions.
* UE4 such as UPROPERTY, UFUNCTION and UCLASS will be generated after include the
< UCLASSNAME >.generate.h files
* It’s important to noted that < UCLASSNAME >.genetate.h file must be included
as the last #include in the list of #include in UCLASS.h
Here’s a correct example
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
//CORRECT, generated file is the last file included
#include "UserProfile.generated.h"
UCLASS()
class XDD_API UUserProfile : public UObject
{
GENERATED_BODY()
};
Several keywords can modify the way of UCALSS behaves * Blueprintable : be able to construct a blueprint inside UE editor
* BlueprintType : be able to use as a variable from blueprint * NotBlueprintType : can not use this variable from blueprint2. Creating UPROPERTY¶
A compelet document can be found in here
in UCLASS, Each UPROPERTY represent it can be a visual editable or blueprint-accessable
data member of the UCLASS
Examples such as EditAnywhere, BlueprintReadWrite can be added to each UPROPERTY
UCLASS(Blueprintable)
class example_API : public UObject
{
GENERATE_BODY()
public:
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = Stats)
float Armor;
UPROPERTY(EditAnyWhere, BlueprintReadWrite, Category = Stats)
float HpMax;
};
## 3. Allocation details can be found here As reference before, we should not use new and delete for opject creation, instead, we use ConstructObject to instantiate object
UCLASS should be considered as resources to the C++ code, and their names should never hardcoded into the code base.
Therefore, we can declare an empty UCLASS reference in C++ code, then select it from blueprint dialog in UE editor.
// in UCLASS code section
//Display any UCLASSes deriving from UObject in a dropdown menu in Blueprints.
UPROPERTY(Editanywhere, BlueprintReadWrite, Category = Unit)
TSubclassOf<UObject> UClassOfPlayer;
//Displays string name of UCLASSes that derive from the GameMode C++ base class
UPROPERTY(EditAnywhere, meta = (MetaClass = "GameMode"), Category = Unit)
FstringClassReference UClassGameMode;
- TSubclassOf<> : specify a UClass name using drop-down menu inside UE editor with related member.
- FStringClassReference :the MetaClass refer to the base C++ class, which limit the drop-down menu’s content to only the blueprints derived from that C++ class.
Instantiating¶
details can be found here
Instantiating new instances of UObject typically use ConstructObject<> or NewObject<>,
it will need at least two info to work properly.
* UClass reference to which type you want to initiate
* Original C++ base class
for example, initiate the object using ConstructObject< > will looks like
Destroying¶
simply call ConditionalBeginDestroy() to begin teardown
default garbage collection time can be found in BaseEngine.ini
we can trigger memory cleanup manually by calling
4. Creating Structure¶
A blueprint editable property in UE editor that contains multiple member, docs is here
5. Creating Enum¶
similar to USTRUCT
¶
5. UFUNCTION()¶
UFUNCTION() is a c++ function that is also callable with BluePrint, any c++ function can be marked as a UFUNCTION()