Timers breaking in arrays

I have abilities in my game which are stored as structs and each ability has a cooldown, which I wanted to store as a timer. Each entity can have multiple abilities so I made an array to hold them. When I use an ability I create a timer either for an empty event or function with a duration of the cooldown of the ability and then I can later check the cooldown and see if it is greater than 0.

This works great with one ability, but as soon as I attempt to create a different timer and set the array element with the new timer, all other timers that have been set inside the array have both their elapsed duration and remaining duration immediately set to -1, but their timer handle remains intact and unique, and the item remains in the same position in the array. Their timer handle is not invalidated either. Notably, the timers that have not yet been set in the array remain untouched at duration of 0.
Also, the timer that has been most recently set still works as normal. It counts down as any timer would and its elapsed and remaining duration are both correct, it is only when a different timer in the array is updated does that timer get set to -1.

Here is a slightly more visual explanation using an array of 3 timers, with their remaining duration:

[Time 0s]: (0, 0, 0)

[Timer 1 set to 8 seconds]: (8, 0, 0)

[Time 1s]: (7, 0, 0)

[Timer 2 set to 5 seconds]: (-1, 5, 0)

[Time 2s]: (-1, 4, 0)

[Timer 1 set to 8s]: (8, -1, 0)

[Time 7s]: (3, -1, 0)

[Timer 3 set to 4 seconds]: (-1, -1, 4)

To fix this I have tried creating a map with keys as the ability struct and values as the timer handle, but this didn’t work. I even tried removing the old item in the array and adding the new one afterwards instead of replacing it, but this did not fix the issue either.
I also tried creating a new array for just timer handles and simply use the indexes to tie the arrays together, but the same issue occurs. I also tried clearing and invalidating the previous timer handle in that array position before setting the new one, but this didn’t fix it either.
I also tried moving the timers inside the struct and simply retrieve them that way, but the same issue occurred.

I use timers extensively throughout my game, but in each case I only ever save their handles to pre-created standalone variables. The only difference I can see is that I am now attempting to store timers in an array and not standalone variables. Thus I presume this is the issue. I for now replaced all the timers with gametime calculations, as I can’t think of any other ways to fix this.

Does anybody know why this is happening or a better way to fix it? Thanks in advance.

1 Like

If you use this setup, it should be ok

Using the map, I can always find the right handle given the timer name.

I’m guessing the ability struct might also work.

Yes, I can also find the corresponding timer handle for each item in the map. The issue is that in my program, its elapsed and remaining duration is incorrect despite the handle being correct, unique, and unchanged.

What I have encountered is that when adding or changing an item inside an array, if that item has a timer handle, then all other valid timers inside the array have their elapsed and remaining duration set to -1, basically becoming invalid.
The other array elements were not edited yet the timers broke with their handles intact. The struct had the same issue. I have not tested if this issue occurs while the timers are paused however.

I was wondering if this was simply a bug with the engine or if I am missing something.

1 Like

I did not have this problem, hence my answer

It could be possible timer associated with handles get reset in a struct, I haven’t tried.

Just to check: If you’re using ‘set timer by event’ or ‘set timer by function’ you still need a different event/function for each timer. You can’t have more than one timer started with exactly the same node :slight_smile:

2 Likes

Oh, I didn’t realize this. Yes that would explain my issue. Sorry, I’m still new to ue.

I’ll just stick with using gametime for now then. Thanks for your help.

1 Like

Try ‘set timer by function’ and keep giving it different names, you’ll be fine :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.