Using Unreal's CreateLambda function in C++
Certain delegate types have access to a static CreateLambda function that can be fun to use.

Game Engine
Unreal Engine 5.5.3
IDE
Rider 2024.3.6
Project Name
MyProject
OS
macOS Sequoia 15.3.1
Recently when working with timers I wanted a quick way to toggle an actor's boolean
value without having to write an additional function. I just wanted something quick and simple and using CreateLambda
was the solution.
Typically when creating a timer I would create something similar to the below snippet where we have an established FTimerHandle
(MyTimerHandle) that binds to call a member function (MyFunction).
GetWorldTimerManager().SetTimer(MyTimerHandle, this, &AMyActor::MyFunction, 1.f, false);
In my situation MyFunction
was only setting Tick
to false and I was wondering if there was another way to do this without having to create new function. The SetTimer
accepts an FTimerDelegate
as it's third parameter so we use the CreateLambda
method inherited from the delegate type. So, we can do FTimerDelegate::CreateLambda([]() { // code here })
. Below is how updated the above snippet to use CreateLambda
, in my scenario I'm setting Tick
to true
.
GetWorldTimerManager().SetTimer(MyTimerHandle,
FTimerDelegate::CreateLambda([this]()
{
SetActorTickEnabled(true);
}), 1.f, false);
Using lambdas can be great and I found that using CreateLambda
in a timer function was fun to use when doing very basic logic.