Delegates and Lambas

Overview

The Unreal Engine provides different ways to call functions.

Delegate

A delegate is a pointer to a C++ function.

A delegate is bound to a function with a certain signature.

There are two kind of delegates: single-cast and multi-cast delegates.

Single-Cast Delegate

A single-cast delegate is bound to only one function.

To declare a single-cast delegate, use the following macros:

DECLARE_DELEGATE(Delegate)
DECLARE_DELEGATE_OneParam(IntDelegate, int32)

Internally, a single-cast delegate is represented by TScriptDelegate.

Multi-Cast Delegate

A multi-cast delegate can be bound to multiple functions and execute them all at once.

Multi-cast delegate are not allowed to use a return value.

To declare a multi-cast delegate, use the following macros:

DECLARE_MULTICAST_DELEGATE(MultiDelegate)
DECLARE_MULTICAST_DELEGATE_OneParam(IntMultiDelegate, int32)

Internally, a single-cast delegate is represented by TMulticastScriptDelegate.

Binding

Single-Cast Delegate

  1. To bind a function to a delegate, call BindUFunction.
    Delegate MyDelegate;
    MyDelegate.BindUFunction(this, FName("MyFunction"));
    
    IntDelegate MyIntDelegate;
    MyIntDelegate.BindUFunction(this, FName("MyIntFunction"));
  1. To check if a delegate is safe to execute, call IsBound.
  1. To call the function bound to the delegate, call Execute.
    Delegate.Execute();
    
    MyIntDelegate.Execute(42);

Multi-Cast Delegate

  1. To bind functions to a delegate, call AddUFunction for each function.
    MultiDelegate MyMultiDelegate;
    MyMultiDelegate.AddUFunction(this, FName("MyFunction1"));
    MyMultiDelegate.AddUFunction(this, FName("MyFunction2"));
    
    IntMultiDelegate MyIntMultiDelegate;
    MyIntMultiDelegate.AddUFunction(this, FName("MyIntFunction1"));
    MyIntMultiDelegate.AddUFunction(this, FName("MyIntFunction2"));
    MyIntMultiDelegate.AddUFunction(this, FName("MyIntFunction3"));
  1. To check if a delegate is safe to execute, call IsBound.
  1. To call the functions bound to the delegate, call Broadcast.
    ⚠️
    The execution order of the functions is not defined.
    MultiDelegate.Broadcast();
    
    MyIntMultiDelegate.Broadcast(42);

TFunction

A TFunction represents a copy to something callable.

template <typename FuncType>
class TFunction

The FuncType template parameter represents a function type.

When declaring a TFunctionRef, the parameter names for FuncType are optional.

For example, a function taking a string and float and returning an int32.

TFunctionRef<int32 (const FString&, float)>
TFunctionRef<int32 (const FString& Name, float Scale)>

TFunction is similar to std::function.

TFunctionRef

A TFunctionRef represents a reference to something callable.

template <typename FuncType>
class TFunctionRef

TFunctionRef is different from TFunction and std::function.

However, TFunctionRef is more efficient than TFunction.

TFunctionRef is useful to parameterize a function with some caller-defined code without making it a template.