Skip to content

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

UCLASS(Blueprintable)
* BlueprintType : be able to use as a variable from blueprint
UCLASS(BlueprintType)
* NotBlueprintType : can not use this variable from blueprint
UCLASS(NotBlueprintType)


2. 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;  
};
parameters can be passed to the UPROPERTY() macro, such as * EditAnywhere : the property can be edited every where obviously * EditDefaultsOnly : Property can be changed only for blueprint before runtime(change default value) * EditInstanceOnly : Property can be changed only for game-level instance of UCLASS object * BlueprintReadWirte : Property can be both readable & writable from blueprint diagram, notice that with BlueprintReadWrite, it must be a public member of class * BlueprintReadOnly : The Property must be set from C++ and cannot be changed from the blueprints * Category : It’s recommended to always specify a Category for UPROPERTY( ), it determines which submenu the UPROPERTY( ) will appear under in the property editor


## 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

ObjectType* object = ConstructObject<ObjectType>(UClassReference);
NewObject<> is simple, but need to pass a GetTransientPackage() with each call
ObjectType* object = NewObject<ObjectType>(GetTransientPackage(), UClassReference);


Destroying

simply call ConditionalBeginDestroy() to begin teardown

newobject->ConditionalBeginDestroy();
newobject = nullptr;

default garbage collection time can be found in BaseEngine.ini
we can trigger memory cleanup manually by calling

GetWorld()->FroceGarbageCollection(true)


4. Creating Structure

A blueprint editable property in UE editor that contains multiple member, docs is here

USTRUCT([Specifier, Specifier, ...])
struct FStructName
{
    GENERATED_BODY()
};


5. Creating Enum

similar to USTRUCT

UENUM([Specifier, Specifier, ...])
enum FEnumName
{
    enumMember1;
    enumMember2;
    enumMember3;
};

5. UFUNCTION()

UFUNCTION() is a c++ function that is also callable with BluePrint, any c++ function can be marked as a UFUNCTION()

docs