DizzasTeR-

[SqMod] Custom Timer Class

Jul 29th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. /*
  2.     Author: Doom_Kill3R
  3.     Plugin: SqMod by SLC
  4. */
  5.  
  6. CTimers <- [];
  7.  
  8. function onServerFrame(elapsedTime) {
  9.     foreach(t_index, t_inst in CTimers) {
  10.         t_inst.Process(time());
  11.     }
  12. }
  13. SqCore.On().ServerFrame.Connect(onServerFrame);
  14.  
  15. class Timer {
  16.     /* Main */
  17.     callback    = null;
  18.     interval    = null;
  19.     repeat      = null;
  20.     params      = [];
  21.    
  22.     /* Misc */
  23.     startTick   = null;
  24.    
  25.     constructor(callback, interval, repeat, ...) {
  26.         this.callback = callback;
  27.         this.interval = interval;
  28.         this.repeat = repeat == 0 ? -1 : repeat;
  29.         this.params = vargv;
  30.        
  31.         this.params.insert(0, this);
  32.         this.startTick = ::time();
  33.        
  34.         ::CTimers.push(this);
  35.         //::SqLog.Inf("Timer:: " + callback.getinfos().name + " - Interval: " + interval + " - repeat: " + repeat);
  36.     }
  37. }
  38.  
  39. function Timer::Destroy() {
  40.     //SqLog.Dbg("Timers::Destroy - " + this);
  41.     if(::CTimers.find(this) != null) {
  42.         ::CTimers.remove(::CTimers.find(this));
  43.     }
  44.     this = null;
  45. }
  46.  
  47. function Timer::Process(currentTime) {
  48.     if(currentTime-this.startTick >= this.interval) {
  49.         if(this.repeat == 0) {
  50.             this.Destroy();
  51.             return true;
  52.         } else {
  53.             this.startTick = currentTime;
  54.             this.Call();
  55.             if(this.repeat == -1) {
  56.                 return true;
  57.             }
  58.         }
  59.         this.repeat--;
  60.         if(this.repeat == 0) {
  61.             this.Destroy();
  62.         }
  63.     }
  64. }
  65.  
  66. function Timer::Call() {
  67.     //SqLog.Dbg("Timers::Call - " + this);
  68.     this.callback.acall(this.params);
  69. }
  70.  
  71. function isTimer(inst) {
  72.     if(typeof(inst) == "instance") {
  73.         if(CTimers.find(inst) != null) {
  74.             return true;
  75.         } else {
  76.             return false;
  77.         }
  78.     } else {
  79.         return false;
  80.     }
  81. }
Add Comment
Please, Sign In to add comment