Can i use lambda with timers?

It just that i have to write a long line and then declare a function in the header and implement it in the cpp file only to make a simple delay.
In javascript for example you can do something like that:


But when i tried to use lambda within the timer function it says it couldn’t convert what he needs to lambda.
Is it really have to be such a mess only to make a simple delay ?

FTimerDelegate TimerCallback;
	// callback;

FTimerHandle Handle;
GetWorld()->GetTimerManager().SetTimer(Handle, TimerCallback, 5.0f, false);

Is this what you’re looking for?

Yep, thanks :slight_smile:

I’d like to emphasize the need of being extra careful when using that method. I’ve used the following:

Callback.BindLambda(&] {
IsHit = false;

Looks quite harmless, right? Well it’s not because when loading a new level with ‘OpenLevel’ it ‘sometimes’ (randomly) happens to have that ‘&]{}’ reference to the ‘this’ (this being an anim instance) object that was destroyed, and the timer manager attempts to call the ‘this->IsHit = false’ causing a crash, since not binding it to a certain objects the timer still lives even after the object was destroyed.

If you use that method and require a reference to an object that might get destroyed, make sure to clear the timer when loading a new level or to clear the timer when fit.

If you want an inline callback, you can do the following:

GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateLambda([] { /* callback; */ }), 5.0f, false);