Using C++ Timers in Unreal Engine 4

With a recent change in the C++ API, the FTimerHandle was introduced to gameplay timers. At the time of writing the documentation for this is not up-to-date, so I thought I should share some information on how to work with timers in C++ with this mini tutorial.

For practical examples on timers check out my C++ Survival project, freely available on GitHub.

Set Timer

You set timers through the global timer manager which is available through GetWorld()->GetTimerManager(). There are a couple of overloads (function variations) available to pass the function to execute, interval between ticks (if looped) and flag to set looping.

void ASBombActor::OnUsed(APawn* InstigatorPawn)
{
Super::OnUsed(InstigatorPawn);
if (!bIsFuzeActive)
{
/* This will trigger the ActivateFuze() on the clients */
bIsFuzeActive = true;
/* Repnotify does not trigger on the server, so call the function here directly. */
SimulateFuzeFX();
/* Activate the fuze to explode the bomb after several seconds */
GetWorld()->GetTimerManager().SetTimer(FuzeTimerHandle, this, &ASBombActor::OnExplode, MaxFuzeTime, false);
}
}

The handle in the header file. Although you are not required to keep a reference to the handle, it’s advisable to do put this in your header to properly clear or pause your timer instance.

	/* Handle to manage the timer */
FTimerHandle FuzeTimerHandle;

Clear Timer(s)

When destroying or deactivating objects, make sure you clear any active timers. There are two ways of dealing with timer removal.

void ASBombActor::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
// Ensure the fuze timer is cleared
GetWorld()->GetTimerManager().ClearTimer(FuzeTimerHandle);
// Alternatively you can clear ALL timers.
/*GetWorld()->GetTimerManager().ClearAllTimersForObject(this);*/
}

Timers are great for triggering delayed events and handling other time-based events that you may be inclined to put in your Tick() function instead.

Check out the API for FTimerManager for more useful functions on timers. Including time remaining, finding the tick rate, active timers, pause/continue etc.

If you wish to learn more on C++, check out my C++ Survival Project that is open-source and covers many gameplay concepts for C++.

11 Comments

    • That sounds highly unlikely mate. When in doubt, remove and recompile and see what happens. There really isn’t much going on in this small piece of code that can touch or corrupt your project, especially unlikely to make it impossible to recover.

      What else did you change? Can you recompile the project successfully and re-open the editor? More importantly, what line of code does the editor crash on specifically, with more information I can try to help.

      – Tom

      • This might sound crazy but it happened to me too. No matter what UE4 crashes on launch. To fix it I had to open Visual Studio, remove the setTimer code, and then rebuild the project to get UE4 to launch without crashing.

        • What engine version are you guys using? Without specific information like the exact error and line it’s failing on there isn’t much I can help you guys with.

          – Tom

    • There is a problem in World.H, at GEngine->GetWorld()->GetTimerManager(), in that when it’s being called, when the editor is loading and the call is being made from a static library for blueprints, i.e. a plugin. Yeah, it blows up with a C5, null pointer in some fashion. Haven’t had the time to debug yet. Not really for sure why it’s upset, there is very little code in the function. It is just checking for GameInstance. This is on 4.10.4

  1. Exactly, which is what I did, just did a check for a nullpointer. But I’m curious as well as to what point in the init process, that a plugin, can start assuming that the system is indeed loaded loaded enough to make calls.

    I mean if GEngine is nullptr then we know nothing is going to be happy! lol

  2. For those of you saying the code crashes. Make sure the function you’re binding to is an UFUNCTION(), I’m not sure if it’s explicitly said in any tutorial or official documentation itself, but I found out it was crashing if it’s not and UFUNCTION().

    Best way to discover that kind of crashes is to launch the editor from VS (DebugGame Editor & Win64, compile and Play), so when editor crashes it throws a breakpoint in VS and you can sometimes figure out what’s going on based on call stack and some keywords.

    Hope it helps.

    Good tutorial btw, not much info about that topic.

Leave a Reply