In this lesson, you will learn how to use dynamic delegates to bridge the gap between C++ logic and Blueprints, enabling your systems to remain flexible and serialized.
Why Dynamic Delegates?
Standard C++ delegates (like single-cast or multi-cast) are extremely fast, but they have one major limitation: they don't exist in the eyes of the reflection system. This means you cannot bind them to functions inside a Blueprint graph.
Dynamic delegates are special because they are registered with the Unreal reflection system. This allows them to be serialized, meaning their state—specifically, which function they are pointing to—can be saved and restored. More importantly, they allow you to expose an "event" in C++ that a user can hook into directly from the Blueprint Editor.
Implementing the Dynamic Delegate
To create one, you must use a specific macro: DECLARE_DYNAMIC_MULTICAST_DELEGATE.
// In your header file
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChanged, float, NewHealth);
UCLASS()
class AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// This variable can now be seen and bound in the Blueprint Editor
UPROPERTY(BlueprintAssignable, Category = "Events")
FOnHealthChanged OnHealthChanged;
};
How it Works
- Declaration: The macro creates a class (the delegate) that acts as a container for bound functions.
BlueprintAssignable: By marking the instance with thisUPROPERTYspecifier, you tell the editor to expose a "Plus" button in the Details panel of your Blueprint. You can then drag off this delegate to bind custom Blueprint logic (like playing a sound or updating a UI widget) whenever the C++ code callsBroadcast().- Serialization: Because it uses the reflection system, Unreal can "remember" the name of the function bound to this delegate. When you save a project, the editor serializes the binding information, ensuring your Blueprint connections persist after you close and reopen the engine.
Dynamic delegates are the primary tool for creating "Event-Driven" architecture where your C++ gameplay systems talk to your Blueprint-driven UI and visual effects.
Key Takeaway: Use dynamic delegates with the BlueprintAssignable specifier to create serializable, cross-boundary events that allow Blueprints to react to C++ state changes.