I am trying to make a global timer where everything that needs to be notified after certain time has passed.
For instance, in a game, there would be buffs and attack cool down timers and item cool down and much more.
Managing them separately is fine, but how would I make them all to run on same timer?
I was tried using SortedList with a float as key and a delegate as value to be simply invoked when time is up, but I cant seem to manage it. Tried delegate with Generic parameter but I can't put that into a sorted list.
Can anyone point me in the right direction?
I can point out 2 options:
TimerControlled
(all names can be changed) With a methodTimerTick(whatever arguments you need)
(and others if needed) , which implements your timer tick logic for that class. Implement the interface the on each class that uses timer dependent mechanics. Finally on your base (logic) class add all of yourTimerControlled
object to an array (ofTimerControlled
) which will allow you to cycle through that array and call TimerTick methods of those object with 2 lines of code.Interface:
Implement it in each of your classes:
finally add your classes to a list of
TimerControlled
:Global.timer
, which will mean only 1 instance of that timer will exist. Then attach an event handler to timer from each relevant class to handle timer ticks.Code:
Keep in mind that I've written the code off the top of my head, so some syntax errors might be there, but the logic is right.
If you have further questions regarding the answer ask away.