DWQA QuestionsHow to use delays in C++ in Unreal Engine
admin Staff asked 5 months ago

Using FTimerDelegate

FTimerDelegate TimerDelegate;
TimerDelegate.BindLambda([&]
{
UE_LOG(LogTemp, Warning, TEXT("This text will appear in the console 3 seconds after execution"))
});

FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 3, false);

 

 

Using FTimerHandle only

FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, [&]()
{
UE_LOG(LogTemp, Warning, TEXT("This text will appear in the console 3 seconds after execution"))
}, 3, false);

 

Separate functions
The second way is to use a delay to execute another function.
Without input parameters

FTimerHandle TimerHandle;
GetWorld()->GetTimerManager().SetTimer(TimerHandle, this, &UObject::MethodWithDelay, 3, false);

 

With input parameters

int32 ParameterToPass = 100; // You can use any supported variable type

FTimerHandle TimerHandle;
FTimerDelegate TimerDelegate = FTimerDelegate::CreateUObject(this, &UObject::MethodWithDelay, ParameterToPass);
GetWorld()->GetTimerManager().SetTimer(TimerHandle, TimerDelegate, 3, false);