Author Topic: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013  (Read 8121 times)  Share 

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
  • Dualnames worked on a game that was nominated for an AGS Award!Dualnames worked on a game that won an AGS Award!
Re: MODULE: Timer v2.01 - Updated 14 Feb. 2007
« Reply #20 on: 24 Oct 2011, 05:37 »
When all else fails, dualnames wins and eats your heart out.

http://www.mediafire.com/?trszlvdho9715s7
No more military army stuff. I'm alive and back.

Alarconte

  • Kneel in front of ZOD, Jor-El Son! xDD
    • I can help with play testing
    •  
    • I can help with story design
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: MODULE: Timer v2.01 - Updated 14 Feb. 2007
« Reply #21 on: 25 Oct 2011, 11:38 »
thank you guys, so servicial!
"Tiny pixelated boobies are the heart and soul of Castlevania"

Galactic Battlefare Capital Choice Part 1 , finished, releasing soon
GBF CC Part 2, WIP

monkey0506

  • AGS Project Tracker Admins
  • Tasting the banhammer. Strangely, tastes like ham.
Re: MODULE: Timer v3.0 - Now OO! Updated 09 August 2013
« Reply #22 on: 09 Aug 2013, 18:13 »
Six year bump baby!

Update in the first post. Download link
« Last Edit: 10 Aug 2013, 03:17 by monkey_05_06 »
User was banned for this post.

Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #23 on: 07 Dec 2015, 14:15 »
Two and half year bump baby!

I don't suppose anyone has a working link for 3.01 by any chance?
Calico Reverie - Independent Game Development, Pixel Art & Other Stuff
Games: Mi - Starlit Grave - IAMJASON - Aractaur - blind to siberia - Wrong Channel - Memoriae - Point Of No Return

IHarvestBrains

  • Get me a 100 brains and then we can talk.
    • I can help with backgrounds
    •  
    • I can help with characters
    •  
    • I can help with play testing
    •  
    • I can help with translating
    •  
    • I can help with web design
    •  
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #24 on: 10 Aug 2017, 11:59 »
I would like a link for the v3.01 as well, please :)

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #25 on: 10 Aug 2017, 15:27 »
Oh, what a coincidence, I was recently thinking that I would use such module in the game I am working on too :).

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #26 on: 13 Aug 2017, 00:15 »
Meanwhile I decided to write my own timer implementation. I can release it as an alternative module later, if people find it usable.

Since AGS 3.4.0 supports custom managed structs, I made timer a pointer type. That adds bit complexity, but hopefully not too much to not use it (you are using pointers to Characters and others too, so...). And this will let you pass Timer objects around your script too, assigning variable to variable, as function parameter or return value.

Here is the code to the header and script body:

EDIT: updated the code

Timer.ash
Code: Adventure Game Studio
  1.  
  2. // Maximal number of timers, change at will (not related to built-in AGS limit).
  3. #define MAX_TIMERS 20
  4.  
  5. managed struct Timer
  6. {
  7.   /// Start the timer, giving timeout in game ticks.
  8.   import static Timer *Start(int timeout, RepeatStyle repeat = eOnce);
  9.   /// Start the timer, giving timeout in real time (seconds).
  10.   /// Remember that timer can be only as precise as your GameSpeed (40 checks per
  11.   /// second, or 0.025s by default).
  12.   import static Timer *StartRT(float timeout_s, RepeatStyle repeat = eOnce);
  13.   /// Stop the timer
  14.   import void Stop();
  15.  
  16.   /// Tells whether timer is currently active (counting down).
  17.   import readonly attribute bool  IsActive;
  18.   /// Signal property telling that the timer has expired. This flag will remain set
  19.   /// for one game tick only and self-reset afterwards.
  20.   import readonly attribute bool  EvtExpired;
  21.  
  22.   /// Tells whether timer has just expired. Safe to pass null-pointer.
  23.   import static bool   HasExpired(Timer *t);
  24.   /// Stops the running timer. Safe to pass null-pointer.
  25.   import static void   StopIt(Timer *t);
  26.  
  27.   protected int   _id; // internal ID of the timer
  28.   protected bool  _realtime; // is timeout in seconds (otherwise in game ticks)
  29.   protected float _timeout; // timeout (ticks or ms)
  30.   protected bool  _repeat; // should auto-repeat or not
  31.   protected float _remains; // time remaining (ticks or seconds)
  32.   protected bool  _evt; // expired event flag
  33. };
  34.  

Timer.asc
Code: Adventure Game Studio
  1. // Check against this value instead of 0.0 to reduce potential floating-point mistakes
  2. #define TINY_FLOAT 0.00001
  3.  
  4. // Internal timer references, need them to actually count down
  5. Timer *Timers[MAX_TIMERS];
  6. // Number of seconds in a game tick (updated)
  7. float GameTickTime;
  8.  
  9.  
  10. // Find free timer slot, returns internal ID, or -1 if timers limit reached
  11. int FindFreeSlot()
  12. {
  13.   int i;
  14.   for (i = 0; i < MAX_TIMERS; i++)
  15.   {
  16.     if (Timers[i] == null)
  17.       return i;
  18.   }
  19.   return -1;
  20. }
  21. // Remove timer reference
  22. void RemoveRef(this Timer*)
  23. {
  24.   if (this._id >= 0)
  25.   {
  26.     Timers[this._id] = null;
  27.     this._id = -1;
  28.   }
  29. }
  30. // Stop the timer
  31. void Timer::Stop()
  32. {
  33.   this.RemoveRef();
  34.   this._evt = false;
  35. }
  36. // Init timer parameters
  37. void Init(this Timer*, int id, bool realtime, float timeout, RepeatStyle repeat)
  38. {
  39.   this._id = id;
  40.   this._realtime = realtime;
  41.   this._timeout = timeout;
  42.   this._repeat = repeat;
  43.   this._remains = timeout;
  44.   this._evt = false;
  45. }
  46. // Start the timer with the given parameters
  47. Timer *StartTimer(bool realtime, float timeout, RepeatStyle repeat)
  48. {
  49.   int id = FindFreeSlot();
  50.   if (id == -1)
  51.   {
  52.     Display("Timer.asc: timers limit reached, cannot start another timer before any of the active ones has stopped.");
  53.     return null;
  54.   }
  55.   Timer *timer = new Timer;
  56.   timer.Init(id, realtime, timeout, repeat);
  57.   Timers[id] = timer;
  58.   return timer;
  59. }
  60. // Start the timer, giving timeout in game ticks.
  61. static Timer *Timer::Start(int timeout, RepeatStyle repeat)
  62. {
  63.   return StartTimer(false, IntToFloat(timeout), repeat);
  64. }
  65. // Start the timer, giving timeout in real time (seconds).
  66. // Remember that timer can be only as precise as your GameSpeed (40 checks per
  67. // second, or 0.025s by default).
  68. static Timer *Timer::StartRT(float timeout_s, RepeatStyle repeat)
  69. {
  70.   return StartTimer(true, timeout_s, repeat);
  71. }
  72. // Tells whether timer is currently active (counting down).
  73. bool get_IsActive(this Timer*)
  74. {
  75.   return this._id >= 0;
  76. }
  77. // Check to know if timer has expired. This property will only return TRUE once,
  78. // and will reset afterwards until timer restarts (automatically or manually).
  79. bool get_EvtExpired(this Timer*)
  80. {
  81.   return this._evt;
  82. }
  83. // Tells whether timer has just expired. Safe to pass null-pointer.
  84. static bool Timer::HasExpired(Timer *t)
  85. {
  86.   return t != null && t.get_EvtExpired();
  87. }
  88. // Stops the running timer. Safe to pass null-pointer.
  89. static void Timer::StopIt(Timer *t)
  90. {
  91.   if (t != null)
  92.     t.Stop();
  93. }
  94. // Countdown once and check if timeout was reached
  95. bool Countdown(this Timer*)
  96. {
  97.   // Otherwise, counting down
  98.   if (this._realtime)
  99.     this._remains -= GameTickTime;
  100.   else
  101.     this._remains -= 1.0;
  102.   // If timer just ran out, set event flag
  103.   if (this._remains <= TINY_FLOAT)
  104.   {
  105.     this._evt = true;
  106.     if (this._repeat)
  107.       this._remains = this._timeout;
  108.     return this._repeat; // keep the timer only if it is repeating one
  109.   }
  110.   this._evt = false;
  111.   return true;
  112. }
  113.  
  114. // Repeat each game tick, even if paused or during blocking action
  115. function repeatedly_execute_always()
  116. {
  117.   // We have to update value of GameTickTime each time, unfortunately, in case game speed changed
  118.   GameTickTime = 1.0 / IntToFloat(GetGameSpeed());
  119.  
  120.   int i;
  121.   for (i = 0; i < MAX_TIMERS; i++)
  122.   {
  123.     Timer *timer = Timers[i];
  124.     if (timer != null)
  125.     {
  126.       if (!timer.Countdown())
  127.       {
  128.         // If timer has stopped, remove its reference from the array
  129.         // (but keep event flag to let other scripts check it)
  130.         timer.RemoveRef();
  131.       }
  132.     }
  133.   }
  134. }
  135.  

Use example:
Code: Adventure Game Studio
  1. Timer *tSay;
  2. function room_AfterFadeIn()
  3. {
  4.   tSay = Timer.StartRT(3.0, eRepeat);
  5. }
  6. function room_RepExec()
  7. {
  8.   if (Timer.HasExpired(tSay))
  9.   {
  10.     cBman.SayBackground("hello!");
  11.   }
  12. }
  13.  
« Last Edit: 18 Aug 2017, 22:18 by Crimson Wizard »

Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #27 on: 10 Sep 2017, 15:01 »
Hey, just wanted to shout that I REALLY LIKE this module and can't live without it now. Thanks for making it! (nod)

Dave Gilbert

  • Mittens Vassal
  • AGS Baker
  • Hi. Our names are FRIGGING ADORABLE.
    • Lifetime Achievement Award Winner
    •  
  • Dave Gilbert worked on a game that was nominated for an AGS Award!Dave Gilbert worked on a game that won an AGS Award!
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #28 on: 13 Sep 2017, 18:41 »
How. Did I. Not see this. Before now.

This would have saved me so much aggravation. Downloading and experimenting with it now!
« Last Edit: 13 Sep 2017, 18:50 by Dave Gilbert »

Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #29 on: 13 Sep 2017, 22:33 »
My main use for Crimson Wizard module is doing something after a tween is finished when that tween must be nonblocking. Example: Turn the visible property of a GUI false once I have tweened it's Transparency to 100.

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #30 on: 13 Sep 2017, 22:48 »
My main use for Crimson Wizard module is doing something after a tween is finished when that tween must be nonblocking. Example: Turn the visible property of a GUI false once I have tweened it's Transparency to 100.
I never used Tween module, but is not there a way to just check if tween has ended? If not maybe Edmundito could add a kind of end signal to the tween, similar to ones I use in my modules, so that you could just check it in repeatedly execute, without need of extra module.

Monsieur OUXX

  • Cavefish
  • Mittens Vassal
  • Mittens Half Initiate
    • I can help with proof reading
    •  
    • I can help with translating
    •  
    • I can help with voice acting
    •  
Re: MODULE: Timer v3.01 - Now OO! Updated 09 August 2013
« Reply #31 on: 14 Sep 2017, 09:53 »
I never used Tween module, but is not there a way to just check if tween has ended?

I was using the old Tweens (1.5 I think?) and I remember it was a pain -- not sayin' that feature was not available, but if it was, it wasn't obvious to find, 'cause I never found it.
I've just started thinking of migrating to the latest Tweens and didn't check if it was there, thinking "of course it will be there, how could it not? the module is mature and ending a tween is as important as starting it". You're scaring me now.