C++ Timer, Call Function every seconds

Hi all,

I have check but I didn’t found what I want. I want to call a function every seconds for 5 seconds with a timer.

Is it possible ?

Thanks :slight_smile:

You can use Timersfor this sort of behavior, although you’d have to do some management to get it to repeat 5 times and then stop.
Or, you can keep track of elapsed time using some combination of UWorld::GetTimeSeconds, FApp::GetCurrentTime, and FApp::GetDeltaTime and fire the function manually in some tick function

I really do not advice the last method, though. Aside from loss of precision, you also have to manually manage your timer. While it might be doable for a single function, over time you can get swarmed by the amount of timers you’re managing. It greatly increases code complexity and decreases ease of understanding.

And your advice of choice is?

Well, using the first suggested timer manager of course :wink:
It saves you from handling all timers manually! In this example you’d still need a counter, but it’s better than needing a counter and keeping track of the delta time.

How could I have overlooked it…time for lunch!

I’m going to chime in and agree with Shammah about using Timers and not writing your own method. In general, if the choice is between writing your own code and using a system provided by the engine I’d recommend using the engine provided version. This way you’ll get the benefits of any future improvements to the system automatically when you upgrade to a new engine version.

Any advantage of using timers instead of just doing it in the tick (assuming we are talking about an Actor)?

If you do it in the Tick event, it will be dependent on how fast your system can run the game i.e. 30fps = 30 function calls. 120fps = 120 function calls. With a timer, it will call the function a set number of timer per second, independent of frame rate.

Sure, but for it to be perceivable by human standards, it would need the frame rate to drop to the point where it would be unplayable regardless.
I guess, I just find it weird that one would to need to constantly recreate timers of such small duration, and would probably only use timers for longer one-time events.

Timers are very useful that’s right. But not sure if I understand you right. You can add the delta time each tick until it’s >5 and then call your stuff and reset it to 0. Would like to test it how much difference in performance between timers and simple tick is, but the result is nearly the same since the delta time gives you the time since the last frame. So 120 frames have the same amount of function calls as a 30 frames pc. Hope I understood you right. But yeah in this case I would recommend a timer too I guess.

I will use the tick function and delta time and I call my function every seconds like that, thanks for answers :slight_smile:

Don’t underestimate this type of problem, though.

For instance, some friends and I went back to play Planetside (the first one) a couple of years after it had come out. The game’s code was never intended to be run on multicore systems.

We noticed this when one of our friends was pacing our Galaxy (flying dropship) on foot in heavy armor.

There seems to be a bit of confusion about timers in this thread. Hopefully this will clear things up.

The timer manager tick function gets called once per frame, just like other objects that need to update. It does not get called a fixed number of times per second. If your game is running at 60 FPS it will be called 60 times per second. If it’s running at 30 FPS it will be called 30 times per second.

Someone asked about reasons to use UE4’s timers instead of ticking individual objects and tracking them individually. I’ll cover a few of the reasons to use timers here.

Maintainability - I touched on this in my earlier reply in this thread. Having all of the code in one place instead of scattered in various classes makes it easier to update and fix. You also get the benefits of any future improvements to timers.

Efficiency - Lets pretend we have 10 actors that all need timers. You could have all ten tick every frame, check the time remaining, and do something if necessary. If you use timers you only need to tick the timer manager. It maintains a sorted list of timers so it usually only needs to check a few timers to see if they’ve expired. Ticking less objects and doing less comparisons is a good thing.

Accuracy - Imagine you have two objects that need timers and they should both be doing something on the current frame. If you use timers then the two timers will be run in the correct order. Whichever one would execute earlier based on the time remaining will be called first. If you’re ticking the objects and manually tracking the time remaining you have no control over which timer will expire first. They will be run in whatever order the objects receive their tick calls and not necessarily in the order of which one has less time remaining. If the order is important you should be using UE4 timers.

Fred, could you please provide one more inside regarding if and how there is a difference between the behaviour of timer and ticks when it comes to code that runs for longer than one frame?


I’m not really sure what you’re asking about. Which code is running longer than one frame in your question? Do you mean timers with a delay of longer than a single frame? That’s going to be the typical case. What about the behaviour are you interested in? Could you take a minute and clarify the question so I can give you a better answer?


I will try to :wink:

Given some program code that may run for longer than one frame or one timer circle.

Will it effect the frame-rate, player input, events, other timer…
…if it runs inside a ticking function?
…if it runs inside a timer?
…or is there no difference?

Hope it’s a little bit more clear.

It sounds like you’re asking about starting a long process (for example something that would take several hundred milliseconds to complete) in response to a timer or inside the tick function of some object. If this isn’t correct please let me know.

If you’re trying to do something slow, like a long computation or loading a file from disk, you should try to avoid doing it synchronously on the main thread. If you do this you’ll get dips in your framerate while everything waits for this to finish. There are ways to run code asynchronously but that’s getting off topic for this thread. If you want to know more about async loading or threading please start another thread.

Fred, never mind.

I can’t make my point, due to my bad English.

“Doing stuff in a tick is bad, if the stuff runs for a long time, because it slows down the frame rate”.

Right or wrong?

I’m making an RTS game that will rely on deterministic ticks on client side. I’d like my timers to tick every x amount of seconds, and be synchronized across clients in the game. Will timers be accurate in that sense? Can i rely on timers on different clients ticking at the same time?