In this lesson, you will learn how to use reflection macros to bridge the gap between your C++ code and the Unreal Engine Editor.
What is Reflection?
Normally, C++ is a "compiled" language that doesn't inherently know about its own structure while the game is running. Unreal Engine solves this with its Reflection System. By using special macros, you "register" your classes and variables with the engine, allowing the Editor to see them, save their state, and expose them to Blueprints.
Core Reflection Macros
There are two primary macros you will encounter in almost every header file:
1. UCLASS()
This macro is placed directly above your class declaration. It tells the Unreal Header Tool (UHT) that this class should be tracked by the engine. Without it, the engine treats your class as a standard C++ object, meaning you won't be able to spawn it in the Editor or assign it to a Blueprint.
2. UPROPERTY()
This macro is placed above member variables. It tells the engine to manage the variable's memory and make it visible in the Details panel. By adding specific "specifiers" inside the parentheses (like EditAnywhere or BlueprintReadOnly), you define exactly how the engine should interact with that data.
Example Implementation
Here is how you apply these to a custom actor:
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h" // Required for the reflection system
UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// This variable is now visible in the Editor and saved automatically
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Gameplay")
float Health;
};
Why They Matter
When you add these macros, the UnrealHeaderTool scans your code during the build process to generate extra "glue" code—seen in the .generated.h file. This system is the backbone of the engine; it powers the garbage collector, the serialization of saved games, and the ability to drag and drop your C++ objects into the world. If you forget these macros, your variables will effectively be invisible to the engine's tools.
Key Takeaway: Reflection macros act as a bridge that registers your C++ code with Unreal Engine, enabling features like serialization, garbage collection, and Editor integration.