Course contentsShow
← Unreal Engine Game Development
Lesson 30 of 8801. Foundations and Editor WorkflowFree lesson

Exposing Variables with UPROPERTY Macros

Use UPROPERTY specifiers to allow the engine to manage variable memory, enable editor editing, and ensure proper serialization during save operations.

In this lesson, you will learn how to bridge the gap between your C++ variables and the Unreal Engine editor using the UPROPERTY macro.

Why use UPROPERTY?

In standard C++, a variable inside a class is private to the code. The Unreal Engine, however, needs to be "aware" of your variables to manage them effectively. By placing the UPROPERTY() macro above your variable declaration, you invoke the Unreal Reflection System. This does three vital things:

  1. Memory Management: Unreal’s Garbage Collector keeps track of your pointers. If you don't use UPROPERTY, the engine might accidentally delete an object while you're still using it.
  2. Editor Exposure: It allows you to edit the variable directly inside the Details Panel of the Unreal Editor, rather than recompiling code every time you want to tweak a value.
  3. Serialization: It ensures that when you save your level or package your game, the current value of that variable is written to disk and restored correctly when the game starts.

Implementation

To expose a variable, you simply place the macro directly above it in your header (.h) file. You can pass "specifiers" inside the parentheses to define how the engine treats the variable:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay Settings")
float MovementSpeed = 600.0f;
  • EditAnywhere: Allows you to change this value both in the Details Panel of a placed Actor and within the Blueprint Class editor.
  • BlueprintReadWrite: Permits your Blueprints to read or change this value during gameplay.
  • Category: Organizes your variables into clean, collapsible sections in the editor’s interface.

Without this macro, the variable remains a "hidden" piece of code that the Engine ignores. By adding it, you turn a static piece of data into a dynamic tool that designers can balance and test in real-time without touching the C++ source files again.

Key Takeaway: The UPROPERTY macro is the essential bridge that allows Unreal Engine to track, save, and expose your C++ variables to the Editor’s visual interface and the memory management system.