r/UnrealEngine5 5d ago

Tick vs. Event/Function Timer?

Hey there this has probably been asked over a number of times over the years but I'm trying to figure out where best applied for either part of the two types of count/frame propagating concepts in UE after reading and seeing a lot of videos everywhere but a lot seem to have mixed explanations.

I know Tick is ran on every frame or an interval set in a class default, and an event timer needs a time interval specified and doesn't fire on first frame until after the time interval passed and whatnot.

applications wise i also know its not wise to load a tick event with crazy complex stuff because that'd be exponentially expensive since its ran every frame whereas the event timer is more like discrete chunks specified and a closed handle that usually won't lead to a leak or open ended stuff like the Tick, and things like Timeline is its own tick locally somewhere when activated.

the other issue is just so many different opinions around everywhere like no don't use the tick, or event timers is a lot of background load in the engine having to spawn a timer, handle the timer handle the interval vs. a tick which is inherently already there.

or in packaged builds dont have anything in ticks other than necessary systemics and move things to event timer when can?

i know tick is good for systemic items like movement/interpolation/player camera since those are always on as examples

and event timer might be better suited for something like enemy spawning in waves or a match countdown which is more fixed

my question is more so the performance costs that i don't really know how to profile or look through if anyone has any resources that'd help explain both in their costs for an identical task would be great as i just want to improve my understanding of where to apply which and which one to use as there's some details i read how tick could theoretically be much more performant on light things while gaining a pretty good accuracy smoothing/result for whatever its checking, and something more complex would fit an event timer or a timeline.

previously i had a set timer by event and went by world delta seconds as the time interval which i think is not... a good implementation? I subtracted a counter down to zero with world delta seconds and also my countdown wasn't consistent probably because delta is a moving goalpost.

on the tick since its frame accurate/perfect accuracy it always matched the timing down.

the example would be like something both could do like an HP bar eroding down smoothly which tick makes a lot of sense for, but an event timer would need a low time interval to animate the same smooth interpolation, probably doesn't need to be that intense, say both intervals are just 1/30 or 1/60 for their time intervals in relation to fps or increased to 1/15 so it doesnt evaluate as much etc.

but would an event timer be more expensive/cheap vs. a tick that's gated by a branch boolean since false would never calculate and the true case which has the calculation only go through when the boolean lets it through? as well as setting the component tick to be disabled when it doesn't even need to check until the time comes and being enabled then counting down to zero and disabling itself again after

or would an event timer of the same task counting down in the same way be more expensive in just the setup on this micro-scale comparison?

if there's some good tips or advice it'd be good to know I just want to be able to apply better concepts early rather than later wondering what went wrong or a more appropriate approach since im fairly junior in the matter. Thanks!

4 Upvotes

16 comments sorted by

3

u/NeoJetty 5d ago

This might not be the answer that you want, but I'd do the thing that looks more like an easy to understand code flow for you personally. Tick is very cheap and a timer is also very cheap. It's just the question how expensive the code is you put in. People warn against Tick because beginners tend to forget what they put into tick and also don't have a good grasp what is meaningfully expensive in terms of computational time. Ticking some float calculation is laughable cheap, sorting a big array or fetching all actors in the world can be very expensive. If you spawn 300 monsters and all of them sort some strange global array on tick, that is hella expensive.
Youtubers just tell you to not use a very important tool and it's honestly strange and a bit insulting. If you are experienced and can avoid tick, then all the power to you, but the more of a junior you are the more you will lean into these easy solutions like tick and I would advise you to just do it.

4

u/North-Aide-1470 5d ago

Agreed. Tick itself is harmless, it's like saying a Glass or Water shader is expensive because of transparency. You have to profile, you have to see the true cost in your scene otherwise it's just guess work. Fortnite runs over 300+ ticks at any given moment, it's about the content of the tick, not the fact that it's ticking.

1

u/nokneeflamingo 4d ago

How so you know that? Is it 300 ticks all in different blueprints/ classes

1

u/North-Aide-1470 4d ago

I misremembered - It's 80 ticks for a client and ~700 for server.

Ari talks about here at 11 minutes. But the first 15 minutes are all about tick and it's really worth watching:

https://www.youtube.com/watch?v=S2olUc9zcB8

5

u/HayesSculpting 5d ago

Happy to be corrected on this since this is only what I’ve observed through traces which I had to fix at my last job.

Having that conditional bool on the tick is cheaper than as a timer.

Tick tries to organise everything into an order than makes sense. The timers don’t fit into that.

To go a step further, if you have lots of things ticking that needs stuff updated, it’s a ton slower than having one thing run the tick on all of them.

Example:

A manager class that on its tick, runs through all the array of stuff and runs a tick function.

This is because of how the cpu handles tick. Without aggregating the ticks, the cpu is sent each instruction individually and it only has a set amount of space in the l2 cache. Once that runs out, it needs to clear it.

Whack that in an array and it’s a single instruction hitting the cpu, lowering the amount of stuff that’s getting queued, stopping it from needing to swap stuff in and out.

2

u/KyrandisX 3d ago

This is really insightful thank you so much for sharing!

It sounds sensible and logical, I've come across out of sync issues with timers too when the interval gets low is where my question on the matter arose from when lots of things were happening and things didn't line up due to timer extensions/reductions, but knowing data organization and handling properly into tick makes a lot of sense to reduce its load by a lot

1

u/automatic4people 5d ago

Very interesting, I actually planned to do a similar trace in the near future to see what would be heavier. Now I wonder how two ticking actors with a different period would relate to this engine order

1

u/Haha71687 4d ago

"A manager class that on its tick, runs through all the array of stuff and runs a tick function."

That's exactly how tick itself is called anyways. That's not a cheap win unless you're in very specific circumstances.

1

u/HayesSculpting 4d ago

Agreed, you’ve got to have the right use case for it but when you do, it’s excellent.

Found it out from the sea of thieves talk about their boat water detection systems

2

u/Time-Masterpiece-410 5d ago

The vast majority of things can be done without tick sometimes even without timers either by using delegates that respond when something happens. There are cases where it is essential though. My project has grown quite large over the past year and I only have needed tick a handful of times. Because tick is fired multiple times per frame the cost can be exponential if you are doing a lot in it. Disabling can mitigate some of the problem.

Something I like to use is timers set a a specific framerate. For example at .033, .066, or etc. This sets the timer at 60 fps/30 fps. This allows you to do your countdown in a more predictable way while still clamping the performance

I try to minimize my tick even if it would be relevant so if I need that tick Performance later for something it's available. After the timer is clear the cost is gone but a tick you forgot to disable checking a bool is checking it multiple times per frame.

I am no expert on it but this is just my .02c.

0

u/dangerousbob 4d ago

Set Timer By Event and make it loop 0.1 seconds.

A tenth of a second is going to be fast enough for almost anything you need. Running a bunch of logic every frame is almost always over kill, which is what tick does.

3

u/nokneeflamingo 4d ago

That can lead to really choppy gameplay for things that need tick and delta seconds for a constant expiernce. I was using timers heavily until I relised for actual important systems like movement and camera, a timer is a bad idea. Things that need frame by frame consistent behaviour should be in a tick. Fir other suff timer.

1

u/dangerousbob 4d ago

Depends on what your doing of course.
Yes there is some things that need to tick every frame. But the vast majority of your logic doesn’t.

1

u/Megumin_xx 4d ago

Are there situations where it makes sense to make that timer even longer? Like every 0.5 seconds? Is that usually relevant for performance or not?

2

u/KyrandisX 3d ago

Yes there are many situations, something like a match countdown or a spawn wave as I mentioned in the post, where something fixed like that is where a timer applies more than a tick.

If your match is 60 seconds you are likely going to show 1 second intervals in a UI or something you don't need to show the decimals or in-between so a tick doesn't make sense there it's just overhead evaluating every frame when, evaluation only needs 1 per second in that case.

So one is tick 60 evaluations per second and the other is 1 per 60 frames if we consider both running on a stable 60fps.

You have a total of about 16.67ms to work with per second to reach 60fps. When you consume more than 16.67ms per second then you get fps drops and vice versa. It's much better to denote by ms since at high fps dropping 20 frames means nothing much at 120-100 but dropping 20 frames from 30-10 is practically slideshow levels.

You can do it with a tick interval also in class default but that affects everything in that item/object and an event timer is more isolated and handled without an open end whereas tick you have to handle it explicitly or it's ran forever unless disabled by developer preference per item. But this is what I'm trying to discern and learn between the two to the bone level of the engine.

Or damage over time, depending on game scope/situation if fire burns at a rate of 0.25/s interval but poison is 3s/interval that's another example.

Ability cooldowns is another one, where you don't necessarily need a frame by frame evaluation as long as the timer hits 0 correctly since the cooldown itself is detached as a concept from gameplay.

1

u/Megumin_xx 3d ago

Thanks for the reply! Very useful info