TExcelTAD
Home › Unreal Engine 5 with C++ › UCLASS, UPROPERTY & UFUNCTION

UCLASS, UPROPERTY & UFUNCTION

Standard C++ cannot list a class's members at runtime. Unreal needs to: the editor shows properties in the Details panel, Blueprints call functions by name, the garbage collector follows object pointers, and the network and save systems copy values. So Unreal adds a reflection system. You mark types and members with macros, and a code generator, UnrealHeaderTool (UHT), reads your headers before the compiler and writes the metadata into *.generated.h / *.gen.cpp files.

The shape of a reflected class

cpp
// ArenaWeapon.h
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ArenaWeapon.generated.h"          // must be the LAST include

UENUM(BlueprintType)
enum class EFireMode : uint8
{
    Single UMETA(DisplayName = "Single Shot"),
    Burst,
    Auto
};

USTRUCT(BlueprintType)
struct FWeaponStats
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (ClampMin = "0.0", Units = "cm"))
    float Range = 5000.f;

    UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (ClampMin = "1"))
    int32 MagazineSize = 12;
};

UCLASS(Blueprintable, Abstract)
class ARENA_API AArenaWeapon : public AActor
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "Weapon")
    bool TryFire();

    UFUNCTION(BlueprintPure, Category = "Weapon")
    int32 GetAmmo() const { return Ammo; }

protected:
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon")
    FWeaponStats Stats;

    UPROPERTY(EditAnywhere, Category = "Weapon")
    EFireMode FireMode = EFireMode::Single;

    UPROPERTY(EditDefaultsOnly, Category = "Weapon", meta = (EditCondition = "FireMode == EFireMode::Burst"))
    int32 BurstCount = 3;

    UPROPERTY(VisibleInstanceOnly, BlueprintReadOnly, Category = "Weapon")
    int32 Ammo = 0;

private:
    UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Weapon", meta = (AllowPrivateAccess = "true"))
    TObjectPtr<USoundBase> FireSound;

    float LastFireTime = -1.f;               // not reflected: invisible to editor, GC, saving, network
};

Rules UHT enforces: GENERATED_BODY() in every UCLASS/USTRUCT; the .generated.h include last; UENUMs used in Blueprints are enum class … : uint8; reflected types carry their prefixes; no reflected members inside #if blocks other than WITH_EDITORONLY_DATA.

UPROPERTY specifiers

Editing and visibility (pick one from each column at most):

Editor Blueprint
EditAnywhere: in the class defaults and on placed instances BlueprintReadWrite
EditDefaultsOnly: only in the class defaults (Blueprint subclass) BlueprintReadOnly
EditInstanceOnly: only on placed instances in a level (none): invisible to Blueprints
VisibleAnywhere / VisibleDefaultsOnly / VisibleInstanceOnly: shown read-only

Other common specifiers:

Specifier Effect
Category = "Weapon|Audio" grouping in Details and the Blueprint palette (| makes subcategories)
Replicated, ReplicatedUsing = OnRep_Ammo network replication (Networking section)
Transient not saved; zero-filled on load
SaveGame included when you serialize with the ArIsSaveGame flag
Config read from an .ini file (the class needs UCLASS(Config = Game))
Instanced the object is owned by this property and edited inline (with EditInlineNew classes)
meta = (ClampMin, ClampMax, UIMin, UIMax) number limits in the editor
meta = (EditCondition = "bCanBurst") greyed out unless the condition is true
meta = (AllowPrivateAccess = "true") allow Blueprint* access to a private member

ClampMin only applies to values typed into the editor. Your C++ can still assign anything, so validate in code where it matters.

UFUNCTION specifiers

Specifier Effect
BlueprintCallable a node with execution pins; can change state
BlueprintPure a node without execution pins; must not change state (often const)
BlueprintImplementableEvent declared in C++, implemented only in Blueprint (no C++ body)
BlueprintNativeEvent C++ default in Name_Implementation, Blueprint may override
Server / Client / NetMulticast + Reliable / Unreliable (+ WithValidation) RPCs
Exec callable from the console (in certain classes: PlayerController, Pawn, GameMode, CheatManager…)
CallInEditor a button in the Details panel, for editor utilities
meta = (DisplayName = "…", Keywords = "…") how the node is named and found

Functions bound to dynamic delegates (AddDynamic) and timers by name must be UFUNCTION() too, even with no specifiers.

A model: what ClampMin and EditCondition do

The editor reads the metadata strings and enforces them when someone types a value. This model stores the metadata in a table and applies it the same way.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <map>
#include <optional>
#include <string>

struct PropertyMeta {
    std::optional<double> ClampMin, ClampMax;
    std::string EditCondition;               // name of a bool property, empty = always editable
};

struct DetailsPanel {
    std::map<std::string, double> values;
    std::map<std::string, PropertyMeta> meta;

    void Type(const std::string& prop, double typed) {
        const PropertyMeta& m = meta[prop];
        if (!m.EditCondition.empty() && values[m.EditCondition] == 0) {
            std::cout << prop << ": greyed out (" << m.EditCondition << " is false)\n";
            return;
        }
        double v = typed;
        if (m.ClampMin) v = std::max(v, *m.ClampMin);
        if (m.ClampMax) v = std::min(v, *m.ClampMax);
        values[prop] = v;
        std::cout << prop << ": typed " << typed << ", stored " << v << "\n";
    }
};

int main() {
    DetailsPanel d;
    d.meta["Range"] = {0.0, std::nullopt, ""};
    d.meta["Spread"] = {0.0, 45.0, ""};
    d.meta["BurstCount"] = {1.0, 10.0, "bCanBurst"};
    d.values["bCanBurst"] = 0;

    d.Type("Range", -200);
    d.Type("Spread", 90);
    d.Type("BurstCount", 3);
    d.values["bCanBurst"] = 1;                // the designer ticks the checkbox
    d.Type("BurstCount", 30);
}
Range: typed -200, stored 0
Spread: typed 90, stored 45
BurstCount: greyed out (bCanBurst is false)
BurstCount: typed 30, stored 10

Test yourself

Which specifier lets a designer set a value only in the Blueprint class defaults, not per placed instance?

  1. EditAnywhere
  2. EditInstanceOnly
  3. EditDefaultsOnly
  4. VisibleAnywhere

A private UPROPERTY(BlueprintReadOnly) fails to compile. What fixes it?

  1. Make it Transient
  2. Add meta = (AllowPrivateAccess = "true")
  3. Remove GENERATED_BODY
  4. Use EditAnywhere

What is the difference between BlueprintImplementableEvent and BlueprintNativeEvent?

  1. None
  2. Implementable has no C++ body; Native has a C++ default in _Implementation that Blueprints may override
  3. Native events are networked
  4. Implementable events are pure