Set time interval
//.h file
FTimerHandle FireRateTimerHandle;
float FireRate = 5.f; // initialDelay (+ frequency when bLoop = true)
void TriggerFire();
// .cpp file
#include "TimerManager.h"
AExample::BeginPlay()
{
Super::BeginPlay();
bool bLoop = true; // run in a loop (FireRate specifies frequency)
GetWorldTimerManager().SetTimer(FireRateTimerHandle, this, &AExample::TriggerFire, FireRate, bLoop);
}
AExample::TriggerFire()
{
if(bCanFire) Fire();
}
Timer with a callback function and input parameter
FTimerDelegate
allows to create an object that can bind a function, see the code below:
FTimerDelegate InputDelegate = FTimerDelegate::CreateUObject(PlayerController, &AExample::TriggerSomething, Value);
GetWorldTimerManager().SetTimer(TimerHandleVariable, InputDelegate, StartDelayTime, bLoop);