How to bind lambda to an Unreal Engine dynamic delegate

Unreal Engine delegates can be of several types:

One of the key differences between dynamic and non-dynamic delegate types is that dynamic delegates can be used in blueprints.

You can bind a function or a lambda to a non-dynamic delegate using one of the Bind*, Create* or Add* functions: CreateLambda, CreateUFunction, CreateUObject, BindLambda, BindUFunction, BindUObject, AddLambda, AddUFunction, AddUObject and so on.

You can bind a function of an UObject to a dynamic delegate using AddDynamic or AddUniqueDynamic. Notice how there is no method provided to bind a lambda to a dynamic delegate. This is because of the way dynamic delegate manages its invocation list internally: it relies on Unreal Engine reflection system in runtime.

However, it would be possible if we proxied calls to our lambda through some UObject. This is exactly (plus necessary inner workings) what Andrew Derkach’s Dynamic Lambda does. This project is in the development stage, but it already can serve as proof of concept and inspire future improvements on this front.

DynamicLambda provides a simple syntax for binding a lambda to a dynamic delegate:

// Short subscription form is preferred
Test->SimpleTestDelegate += [&]{ DoSomeStuff(); };

// To bind 'weak' lambda use 'tuple' syntax
Test->SimpleTestDelegate += (MyObjectPtr, [&]{ DoSomeStuff(); });

Try it out!