In this lesson, you will learn how to use the UFUNCTION macro to bridge the gap between your C++ logic and Unreal Engine’s reflection system, making your code visible to Blueprints and other internal engine tools.
The Role of Reflection
When you write a standard C++ function, it is invisible to the Unreal Editor; the engine doesn't know it exists once the game is compiled. To make a function "discoverable," we must register it with the Unreal Header Tool (UHT) using the UFUNCTION macro. This process, called reflection, allows the engine to inspect your function at runtime, determine its inputs and outputs, and trigger it via external systems like the Blueprint graph or network replication.
Implementing UFUNCTION
To expose a function, place the UFUNCTION macro directly above your function declaration in the class header file (.h). You must also include the specific specifier that tells the engine how to use it. The most common specifier for Blueprint interaction is BlueprintCallable.
UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// This function is now visible to Blueprints!
UFUNCTION(BlueprintCallable, Category = "MyGameplay")
void TriggerSpecialEffect();
};
Why Specifiers Matter
The UFUNCTION macro acts as a gateway. Without parameters inside the parentheses, the function is registered but remains hidden. By adding BlueprintCallable, you tell the engine: "Please create a node for this function in the Blueprint Editor." You can also add Category to keep your function organized in the Blueprint palette, making it easier for designers to find.
Beyond Blueprints, this reflection system is what allows the engine to safely handle events. Because the engine "owns" the reflection data, it can track when a function is called, validate the object's lifetime, and even replicate the function call across a network if you use the appropriate networking specifiers (which we will explore in future lessons).
By marking your functions with UFUNCTION, you turn static code into a dynamic system that communicates seamlessly with the rest of the Unreal environment.
Key Takeaway: The UFUNCTION macro registers your C++ function with the Unreal Reflection System, enabling the engine to invoke, organize, and expose your code to the Blueprint visual scripting environment.