Setting and Clearing Timers
SetTimer functions of
FTimerManager will set a timer to call a function or delegate after a delay, and can be set to repeat that function call indefinitely. These functions will fill out a Timer Handle (type
FTimerHandle), which can be used to pause (and resume) the countdown, query or change the amount of time remaining, or even cancel the Timer altogether. It is safe to set Timers within a function called by a Timer, even including reuse of the Timer Handle that was used to call the function. One use for this might be to delay initialization of one Actor if it depends on another Actor that hasn't spawned yet, but is expected to spawn soon; the dependent Actor's initialization function could set a Timer to call itself again after a fixed length of time, e.g. one second. Alternately, the initialization function could be called by a looping Timer that clears itself upon success.
Timers can also be set to run on the next frame, rather than with a timed interval. This is accomplished by calling
SetTimerForNextTick, but note that this function does not fill out a Timer Handle.
To clear a Timer, pass the
FTimerHandle that was filled out during the
SetTimer call into the
FTimerManager function called
ClearTimer. The Timer Handle will become invalid at this point, and can be reused to manage a new Timer. Calling
SetTimer with an existing Timer Handle will clear the Timer referenced by that Timer Handle and replace it with a new one.
Finally, all Timers associated with a specific object can be cleared by calling
// Call RepeatingFunction once per second, starting two seconds from now.
GetWorldTimerManager().SetTimer(MemberTimerHandle, this, &AMyActor::RepeatingFunction, 1.0f, true, 2.0f);
// Once we've called this function enough times, clear the Timer.
if (--RepeatingCallsRemaining <= 0)
// MemberTimerHandle can now be reused for any other Timer.
// Do something here...
SetTimer with a rate less than or equal to zero is identical to calling