In this lesson, you will learn how to use the UPROPERTY macro to bridge the gap between your C++ code and the Unreal Engine Editor.
Why Do We Need UPROPERTY?
By default, C++ variables are invisible to the engine. If you declare a variable like float MovementSpeed; inside your class, the engine doesn’t know it exists, meaning it won’t appear in the Details panel for designers to edit, and—more importantly—the Garbage Collector won't track it.
The UPROPERTY macro is the "handshake" that registers your variable with the Reflection System. It tells Unreal: "Please track this memory, save its value to disk, and display it in the editor if I ask."
Implementing the Macro
To expose a variable, you place the UPROPERTY macro directly above the variable declaration in your header file. You must also include the appropriate specifiers inside the parentheses to dictate how the engine treats that data.
UCLASS()
class AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// This makes the variable visible in the Details panel and editable
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Stats")
float MovementSpeed = 600.0f;
// This protects the variable from the Garbage Collector
UPROPERTY()
UStaticMeshComponent* MeshComponent;
};
Understanding Key Specifiers
EditAnywhere: Allows you to change the value in both the Blueprint editor and on individual instances placed in the level.BlueprintReadWrite: Enables you to Get or Set this variable within the Blueprint visual scripting graph.Category: Organizes your variables into folders in the Details panel, making your class much easier to navigate as your project grows.- Memory Safety: If your variable is a pointer to a
UObject(like aUStaticMeshComponent), you must useUPROPERTY(). Without it, the Garbage Collector won't know the object is still in use and may delete it while your game is running, causing a crash.
By using these macros, you transform private code variables into powerful, managed assets that the engine can serialize, save, and manipulate safely.
Key Takeaway: The UPROPERTY macro is essential for both exposing data to the Editor and ensuring the engine’s Garbage Collector keeps your referenced objects alive.