Download

Need to dynamically create timers

I am implementing a “buff bar” system where I wish to display current status effects for the player with an overlaying decrementing counter. I’m trying to work out the pseudocode right now.

In my character’s blueprint, I can call a custom “Add Buff” event, passing in the duration, description and an icon. From there, I’m not sure. I could make a struct that contains the duration, description and icon and the “Add Buff” event could simply add a new entry with those values to an array.

Then, I could use a timer that calls a function every second. This function could loop through the array, decrement each duration by 1 and remove it if it’s 0.

The problem with this is the durations will essentially be decreased by up to 1 second for each buff. If the timer is in the middle of it’s wait cycle when a buff is added, it will decrement the duration on the new buff before a second has fully passed.

So I feel like I need a new timer for each buff. You can’t create a timer variable so the only thing I can think of is creating a new actor to represent the buff. Each actor would have its own timer and function independently. The character would just keep track of the actors. However, I feel like this may be over complicating things. Is there a simpler solution to this?

Using a timer to decrement the duration every x seconds is not a good approach in my opinion especially if we are talking about hundreds of buffs (as in multiplayer).

I would use timestamps for this… for example when you add a buff to a character you would simply store a buff id and buff timestamp (the time it got added) to either a pre-made variable (like buff1, buff2 etc.) or an array.
So then you can update the seconds remaining in your hud and/or widget like so: timestamp + durationOfBuff - currentTime = remaining duration.

That way it’s way more efficient since you are only storing a number (timestamp), no timers and especially no actors are needed for this.

Well a timestamp wont tell me how long the buff should last for, only the time it was applied so I will need that as well. But using a timestamp does allow me to get a more accurate duration to print to the HUD.

So as long as I pass the initial timestamp and duration to the HUD, I can use the HUD’s event tick to show the remaining time left on it. Ok, I think I can work with that, thanks!

You could add the duration time to the timestamp beforehand (meaning store the time that the buff ends rather than when it got added), it all depends on your game.
So then it would simply be timestamp - currentTime.

Oh … yeah, that makes sense. Sometimes I get so wrapped up in trying to do things literally that optimizations just don’t occur to me at all.

TK-Master, thank you for your replies on this, seems like a way smarter way to accomplish things. I had also wondered about this often