As far as I understand the check is called every 2 seconds at all times. Is that efficient and if not is there a better way to do it?
If something runs every 2 seconds, it is not very often as frames usually tick at 16.7 ms (60 updates per second) or more. I’m usually only concerned about adding too much functionality to an actor or component tick since these typically run every frame, though you can configure them to run at any interval via the PrimaryActorTick or PrimaryComponentTick members in C++ or in blueprints.
The actor/component tick is actually implemented internally using the same functionality as the “Timer Manager” way, but runs typically at a higher interval than a custom timer you create. I know this because I had looked into whether using FTimerManager with a looping timer was fundamentally any different than overriding Tick and setting the tick interval to the desired loop rate.
This is the class “poll vs push” tradeoff. A timer solution is a “polling” solution because it runs continuously regardless if a condition is met. An alternative solution is a “push” or event based system. This is the classic “observer pattern” in CS. This is what is used in OnActorOverlap or OnComponent Overlap set of “delegate” functions. Instead of continuously checking for a condition, the object that satisfies the condition “fires” an event when the condition is true and then all interested parties subscribe to it via a “callback function” which replaces a timer. Delegates in Unreal are similar to delegates in other languages like C# or “event listeners” as they are sometimes called.
Event Dispatchers (Delegates) | Community tutorial
Delegates | Unreal Engine Documentation
They can be quite complex creating your own at the start if you are new to C++ or Unreal and a polling solution often is easier to reason about. The general rule of optimization is make your feature work first before worrying too much about optimization. Often the simplest solution works well enough and if it doesn’t it’s best to measure and Unreal provides a lot of tools here in their “stats system” to add performance timers and also profile your game session.
Thank you very much for the detailed answer this was exactly what I was looking for
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.