Creating a timer and target system in C++

UE4 has timer system usable from both C++ and Blueprints :slight_smile:

On each time change you will need to reset the timer (use SetTimer using same handler), so read current time and then modify it and set timer again

EDIT: I just notice those docs are outdated, it missing timer handlers, you need to create FTimerHandle varable, just declaring it is enough, you need to create one for each timer in class (not each object, the new FTimerHandle will be created once you spawn the object) , you can also declarer it as local varable if you want to create fire and forget timer. If you lose handle, you will lose control over timer, keep that in mind.

Now function difference, before introduction of handles object and function was used as timer’s handle/id thats why all functions you see in this outdated docs use them, but know all the operational function use just handle as first argument insted of object/function combination and in SetTimer you input handle in first argument and then type in object and function so timer know what function to execute, so you only type in object and function in SetTimer. IF you confused just look on API refrence you should understand what i mean:

I’ve got the basis of a game I’d like to keep building but there are three things missing. One that people cannot seem to figure out and two others I hope there is a solution to via this question.

I’d like to add a countdown timer (start at, say, 2 minutes, and pause and allow the player to restart the game when it hits 0) and also a target system where you have a few designated meshes around the map and when the player shoots them, the number of targets left decrease by one. Could someone help me in how I’d go about implementing this in C++ or steer me in the right direction with some tips? Also it’s a first person shooter and was built using the FPS preset, don’t know if that’s relevant but I thought I’d include it all the same.

Thanks in advance.

Thanks for the in depth information. I couldn’t quite tell from your answer, but is it possible to set the timer to a certain number like 3 minutes? And is it possible to then give the player the chance to restart from the beginning when the timer hits 0?

Yes :slight_smile: timer use seconds so 3*60 = 120, it a time to which given function will be called, when timer hits 0 it will call a function and timer stops, whatever happens next depends on your code.

watch this