C# How to notify other classes with a single timer?

285 Views Asked by At

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?

1

There are 1 best solutions below

9
On BEST ANSWER

I can point out 2 options:

  1. Create an interface like TimerControlled (all names can be changed) With a method TimerTick(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 your TimerControlled object to an array (of TimerControlled) which will allow you to cycle through that array and call TimerTick methods of those object with 2 lines of code.

Interface:

interface TimerControlled
{
   void TimerTick();
}

Implement it in each of your classes:

public class YourClass: TimerControlled{
   ....
   public void TimerTick(){
      advanceCooldown();
      advanceBuffTimers();
   }
}

finally add your classes to a list of TimerControlled:

class YourLogicClass{
   List<YourClass> characters= new List<YourClass>();
   private timer;
   List<TimerControlled> timerControlledObjects = new List<TimerControlled>();
   ...
   public void Initialize(){
      ... //your code, character creation and such
      foreach(YourClass character in characters){ //do the same with all objects that have TimerControlled interface implemented
         timerControlledObjects.add(character);
      }
      timer = new Timer();
      timer.Tick += new EventHandler(timerTick)
      timer.Start();

   } 

   public void timerTick(Object sender, EventArgs e){
      foreach(TimerControlled timerControlledObject in timerControlObjects){
         timerControlledObject.TimerTick();
      }
   }

}
  1. (not a very good option in the long run) Static timer in a static class, like 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:

public static class Global{
//I usually create such class for global settings
   public static Timer timer= new Timer();
}



class YourLogicClass{
   public void Initialize(){
       ... 
       Global.timer.Start();
   }
}

class YourClass{

   public YourClass(){
      Global.timer.tick += new EventHandler(timerTick);
   }


   private void timerTick(Object sender,EventArgs e){
      advanceCooldowns();
      advanceBuffTimers();
   }
}

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.