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.