Delegates

Delegates, Dynamic Delegates & Multicast Delegates

Updated about 2 years ago Edit Page Revisions

Overview

This article shows the core code needed to implement a variety of delegates in UE4.

A delegate is an event that you can define and call and respond to.

Every time the event is fired, anyone listening for this event will receive it and be able to take appropriate action.

In the case of multicast delegates, any number of entities within your code base can respond to the same event and receive the inputs and use them.

In the case of dynamic delegates, the delegate can be saved/loaded within a Blueprint graph (they're called Events/Event Dispatcher in BP).

In this example we will be using exclusively DYNAMIC_MULTICAST which is the type that is most useful in Blueprints.

Steps

Signature

You create the signature of the delegate, which declares what inputs any receiving functions should specify.

//RamaMeleeWeapon class .h

DECLARE_DYNAMIC_MULTICAST_DELEGATE_SixParams( FRamaMeleeHitSignature, class AActor*, HitActor, class UPrimitiveComponent*, HitComponent, const FVector&, ImpactPoint, const FVector&, ImpactNormal, FName, HitBoneName, const struct FHitResult&, HitResult );

Notice the macro declares that we will be adding 6 parameters, there are similar macros for other quantities of parameters.

 DECLARE_DYNAMIC_MULTICAST_DELEGATE_SixParams

Calling the Delegate

You call the delegate within the class structure where it was defined, making sure to only execute it if it is currently bound, meaning at least 1 entity is listening for this delegate / event.

.h
//.h
//RamaMeleeWeapon class .h

//This should be in the class which calls the delegate, and where the signature was defined
//This is an instance of the signature that was defined above!
FRamaMeleeHitSignature RamaMeleeWeapon_OnHit;
.cpp
//.cpp
//Only the code that is supposed to initiate the event calls Broadcast()
if(RamaMeleeWeapon_OnHit.IsBound()) // Add Event and see your new delegate\! So nice\!

This is an additional benefit of using DYNAMIC_MULTICAST delegates: Multi-cast implies binding multiple various object instances to the delegate and then firing off the event to everyone from a single .Broadcast, which can include your Level Blueprint as a recipient/listener.

Video Example

Here is a video on how a C++ delegate created in an actor component in C++ looks and is called in Blueprints.

The code in this wiki and this video are from my Melee Weapon Plugin

Further Reading

Epic Documentation: Multi-cast Delegates

DYNAMIC_MULTICAST And Other Types

There are other delegate types besides DYNAMIC_MULTICAST that are not quite as versatile when it comes to Blueprints.

Check out the source code of Delegate.h for a detailed explanation.

 Runtime/Core/Public/Delegates/Delegate.h

Sample from this file:

**
 *  C++ DELEGATES
 *  -----------------------------------------------------------------------------------------------
 *
 *  This system allows you to call member functions on C++ objects in a generic, yet type-safe way.
 *  Using delegates, you can dynamically bind to a member function of an arbitrary object,
 *  then call functions on the object, even if the caller doesn't know the object's type.
 *
 *  The system predefines various combinations of generic function signatures with which you can
 *  declare a delegate type from, filling in the type names for return value and parameters with
 *  whichever types you need.
 *
 *  Both single-cast and multi-cast delegates are supported, as well as "dynamic" delegates which
 *  can be safely serialized to disk.  Additionally, delegates may define "payload" data which
 *  will be stored and passed directly to bound functions.

Conclusion

Enjoy using delegates in UE4 so that any part of your code base can respond to an event triggered by one section of your code.

Also enjoy exposing delegates via C++ for the rest of your team to use in Blueprints.

Original Author: Rama