MODULE: Timer v3.01 - Now OO! Updated 09 August 2013

Started by monkey0506, Tue 07/11/2006 18:22:54

Previous topic - Next topic

monkey0506

Timer v3.01

Due to a recent suggestion by Snarky, I decided to go ahead and update this module, for the first time in six years! Also, the old download links are all dead anyway. So what's new in v3.0? The timers are now completely OO, which means no more mucking about with everything being called on the Timer class.

Code: ags
// room script
Timer tBomb;

function oFuse_Useinv()
{
  if (player.ActiveInventory == iMatch) tBomb.Set(5.0, eTimerSeconds);
}

function room_RepExec()
{
  if (tBomb.IsExpired())
  {
    oBomb.Animate(10, 5); // bomb explosion animation
  }
}


How does it work? Magic, that's how!

Download v3.01

09 August 2013 I've updated this to v3.01, which changes a few things. The first parameter to Timer.Set is now a float to allow setting partial seconds, and there is an optional RoundDirection parameter (following the optional TimerUnit parameter). To pass an integer you can now call Timer.SetInt, or just use IntToFloat. I've added methods Timer.Pause and Timer.UnPause to allow temporarily pausing a timer, and the property Timer.IsPaused to check the current pause state. I've added properties Timer.RunWhileGamePaused and Timer.RunWhileBlocking to give greater control over when the timers are updated (set per instance, defaults are false and true respectively). Finally, Timer.Count was returning the wrong value, so I fixed that bug.

v3.0 Download

Timer v2.01

In making the demo for my Flashlight module, I realized that the way I was storing the timer names was highly susceptible to collision, and that these collisions would result in crashes. This version is merely a fix for this issue.

I've also included a Timer.IsNameValid function for checking timer names for validity. Timer names cannot contain the strings "//TimerStart", "//TimerEnd", nor can they be null or empty Strings. Timer.Set will reject invalid names.

Download Timer v2.01
Read the Timer manual online

Timer v2.0

Now you can create as many timers as you want (:P)! I could have worked this out before, but I was just being lazy, and thinking it through the wrong way. I've rethought this entire module, and I've even completely rewritten it from scratch.

As always, feel free to let me know if you find any issues, or just if you have any comments or questions!

~monkey~

Timer v1.0
I wrote another module. This one basically just gives the user more timers to work with (100 by default). Except that the user can also control whether the timer is updated only while rep_ex is running or while rep_ex_always is running, the timers can be temporarily paused, and you can give the timers names. My original idea was way cooler but it would never work. So...

Mirror v1.0 Thanks Neole!

Janik

What was your original idea?
Play pen and paper D&D? Then try DM Genie - software for Dungeons and Dragons!

Kweepa

I, also, accept your thinly veiled invitation to ask about the original idea.
Still waiting for Purity of the Surf II

monkey0506

Wouldn't you like to know? ;)

I guess it wasn't really way cooler...it just made it sound better to exaggerate...a lot. I was thinking I could offer the user a way to create infinite numbers of named timers...but the overhead in trying to update them all was way more than even remotely reasonable. :-[ :-X

Anyway...I always found the built-in timer functions somewhat limited. So, if anybody finds this useful...great. If not...that's cool.

gypsysnail

Hey Monkey!! Thank you thank you! This is what I have been waiting for! I need to ask some questions, being such a newbie lol. Ok here goes.... exactly how can the timer work? I will give u an example of how my game is to be. It will be set out as day 1, day 2, day 3, so on to about day 8, and what I am hoping for is the timer could be for such things during the day. Or.... is this timer suited for timing things/tasks/puzzles during a game? And does anything need to be created - sprites such as clocks? Forgive me for bein a newbie, if I have gotten any idea wrong :).
Cheers,
Snail
Believe in afterlife! It's true in a metamorphical way ;)
Ken & Roberta - my inspiration!! 20 years.
U are what you love doing and passionate about - keep up what you love most.

Rocco

its magic, this morning i wake up with a new game idea,
its a very ambitious idea, not an adventure but rather a RPG - Sim.
a little bit of a mix between oil-imperium and theClou!
I have a good imagination how the programming can be done, but i was the whole day insecure with
the time management of the game, cause i need a lot of timers for this game, thats for sure.

so your module is a present of godÃ,  ;D, and a upper advice to make some real game out of this idea.

thanks, its a great thingÃ,  :)


monkey0506

Hehehe. := Thanks.

But snail...I'm not entirely sure I understand what you mean. As for keeping track of what day it is, you could do something like:

Code: ags
// script header
import int day;

// global script
int day;
export day;


The module is designed for creating timers to time things (such as tasks or puzzles)...so....I hope I've answered your questions. Anything else you'd like to know, feel free to ask.

gypsysnail

Ahh thats it, thanks Monkey :):) I had been thinking maybe the timer was for timing tasks and puzzles. I would be interested in knowing more on how your timer would work in timing a puzzle? Maybe an example here, would that be ok? Would design of a puzzle have to be taken in consideration for the timer while designing puzzle images and stuff like that?

Other question, thanks for the info on the day thing :) I am just starting out here and I have a view in my mind, well already have designed some stuff for the Day 1 Day 2 etc etc part such as a room just for "Day 1....." text, but how to actually make it work I have to really think on this, as I am no expert or even intermediate scripter - not yet anyway lol. I also wonder if the day thing works with a game clock? Could you maybe define more on the script if thats ok? I do understand where the script must go yes, but its only one part of a bigger script I feel. If I am posting this stuff in this thread as a wrong thread, please let me know and I will move it to a fresh new thread so forgive me for any mistakes :).
Cheers,
Snail
Believe in afterlife! It's true in a metamorphical way ;)
Ken & Roberta - my inspiration!! 20 years.
U are what you love doing and passionate about - keep up what you love most.

monkey0506

#8
Well...hmmm...how about a good old crappy example from our friend the bomb!

Code: ags
// bomb gets activated
Timer.Set("bomb", GetGameSpeed() * 60 * 5); // sets timer "bomb" for 5 minutes

// repeatedly_execute
if ((player.Room == 5) && (Timer.IsExpired("bomb"))) {
  Display("You didn't escape the bomb in time!");
  Display("Game over!");
  }

// on_event
if ((event == eEventLeaveRoom) && (data == 5) && (Timer.GetTimerID("bomb") != -1)) {
  Display("You escaped the bomb!");
  Timer.Remove("bomb");
  }


That would set the timer for the bomb, and if the player didn't leave the room within 5 minutes display the game over message; otherwise the timer would be removed.

[EDIT:]

Retrospectively Timer.GetTimerID seems a bit redundant...but then I'm not sure if I want to reduce it to Timer.GetID...Anyway I'm busy right now, so for now it'll stay the way it is.

gypsysnail

:D ahhh thanks! That really helps me start off :) I will try it over the weekend and the next week. Cheers :)
Believe in afterlife! It's true in a metamorphical way ;)
Ken & Roberta - my inspiration!! 20 years.
U are what you love doing and passionate about - keep up what you love most.

monkey0506

*bump* for new version! Hooray for SSH for lying his way into forcing me to write this new and improved version! :D

Lt. Smash

Hey! Just wanted to ask if someone could reupload the latest version. The links are dead.

monkey0506

I'll have to see if I can dig up a copy of this version, otherwise I'll just rescript it.

In the mean-time, there is a mirror of some of my old files here, which includes Timer v1.0. ;)

Lt. Smash


Knox

I know this is an older thread, but I was interesting in this module and the link seems to be broken...does anyone have a copy of this? It doesnt seem available on american girl scouts either :P...DOH!
--All that is necessary for evil to triumph is for good men to do nothing.

Lt. Smash


Knox

--All that is necessary for evil to triumph is for good men to do nothing.

monkey0506

Thanks for posting that. I'm actually doing some work to be able to try and better manage and organize my modules and such.. ::)

Alarconte

I found the two links of this file down the past weeks and now, any chance to reupload the file?

Thanks!
"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

Hey, sorry but I don't have a copy of the module handy at the moment (if I indeed have one at all), but it's not a very complicated one. I think if I were to revise this I'd probably take an approach like I did with my StateMachine module where I actually created managed instances of an internal class statically...that's all a bit technical, but how would you like to do something like this?

Code: ags
  Timer bomb;
  bomb.Load("bomb");
  bomb.Set(20); // optional
  // ....
  if (bomb.Expired) Display("You died!");

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Alarconte

"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

#22
Six year bump baby!

Update in the first post. Download link

xil

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

BrainMayhem

I would like a link for the v3.01 as well, please :)

Crimson Wizard

Oh, what a coincidence, I was recently thinking that I would use such module in the game I am working on too :).

Crimson Wizard

#26
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: ags


// Maximal number of timers, change at will (not related to built-in AGS limit).
#define MAX_TIMERS 20

managed struct Timer
{
  /// Start the timer, giving timeout in game ticks.
  import static Timer *Start(int timeout, RepeatStyle repeat = eOnce);
  /// Start the timer, giving timeout in real time (seconds).
  /// Remember that timer can be only as precise as your GameSpeed (40 checks per
  /// second, or 0.025s by default).
  import static Timer *StartRT(float timeout_s, RepeatStyle repeat = eOnce);
  /// Stop the timer
  import void Stop();
  
  /// Tells whether timer is currently active (counting down).
  import readonly attribute bool  IsActive;
  /// Signal property telling that the timer has expired. This flag will remain set
  /// for one game tick only and self-reset afterwards.
  import readonly attribute bool  EvtExpired;
  
  /// Tells whether timer has just expired. Safe to pass null-pointer.
  import static bool   HasExpired(Timer *t);
  /// Stops the running timer. Safe to pass null-pointer.
  import static void   StopIt(Timer *t);
  
  protected int   _id; // internal ID of the timer
  protected bool  _realtime; // is timeout in seconds (otherwise in game ticks)
  protected float _timeout; // timeout (ticks or ms)
  protected bool  _repeat; // should auto-repeat or not
  protected float _remains; // time remaining (ticks or seconds)
  protected bool  _evt; // expired event flag
};


Timer.asc
Code: ags

// Check against this value instead of 0.0 to reduce potential floating-point mistakes
#define TINY_FLOAT 0.00001

// Internal timer references, need them to actually count down
Timer *Timers[MAX_TIMERS];
// Number of seconds in a game tick (updated)
float GameTickTime;


// Find free timer slot, returns internal ID, or -1 if timers limit reached
int FindFreeSlot()
{
  int i;
  for (i = 0; i < MAX_TIMERS; i++)
  {
    if (Timers[i] == null)
      return i;
  }
  return -1;
}
// Remove timer reference
void RemoveRef(this Timer*)
{
  if (this._id >= 0)
  {
    Timers[this._id] = null;
    this._id = -1;
  }
}
// Stop the timer
void Timer::Stop()
{
  this.RemoveRef();
  this._evt = false;
}
// Init timer parameters
void Init(this Timer*, int id, bool realtime, float timeout, RepeatStyle repeat)
{
  this._id = id;
  this._realtime = realtime;
  this._timeout = timeout;
  this._repeat = repeat;
  this._remains = timeout;
  this._evt = false;
}
// Start the timer with the given parameters
Timer *StartTimer(bool realtime, float timeout, RepeatStyle repeat)
{
  int id = FindFreeSlot();
  if (id == -1)
  {
    Display("Timer.asc: timers limit reached, cannot start another timer before any of the active ones has stopped.");
    return null;
  }
  Timer *timer = new Timer;
  timer.Init(id, realtime, timeout, repeat);
  Timers[id] = timer;
  return timer;
}
// Start the timer, giving timeout in game ticks.
static Timer *Timer::Start(int timeout, RepeatStyle repeat)
{
  return StartTimer(false, IntToFloat(timeout), repeat);
}
// Start the timer, giving timeout in real time (seconds).
// Remember that timer can be only as precise as your GameSpeed (40 checks per
// second, or 0.025s by default).
static Timer *Timer::StartRT(float timeout_s, RepeatStyle repeat)
{
  return StartTimer(true, timeout_s, repeat);
}
// Tells whether timer is currently active (counting down).
bool get_IsActive(this Timer*)
{
  return this._id >= 0;
}
// Check to know if timer has expired. This property will only return TRUE once,
// and will reset afterwards until timer restarts (automatically or manually).
bool get_EvtExpired(this Timer*)
{
  return this._evt;
}
// Tells whether timer has just expired. Safe to pass null-pointer.
static bool Timer::HasExpired(Timer *t)
{
  return t != null && t.get_EvtExpired();
}
// Stops the running timer. Safe to pass null-pointer.
static void Timer::StopIt(Timer *t)
{
  if (t != null)
    t.Stop();
}
// Countdown once and check if timeout was reached
bool Countdown(this Timer*)
{
  // Otherwise, counting down
  if (this._realtime)
    this._remains -= GameTickTime;
  else
    this._remains -= 1.0;
  // If timer just ran out, set event flag
  if (this._remains <= TINY_FLOAT)
  {
    this._evt = true;
    if (this._repeat)
      this._remains = this._timeout;
    return this._repeat; // keep the timer only if it is repeating one
  }
  this._evt = false;
  return true;
}

// Repeat each game tick, even if paused or during blocking action
function repeatedly_execute_always()
{
  // We have to update value of GameTickTime each time, unfortunately, in case game speed changed
  GameTickTime = 1.0 / IntToFloat(GetGameSpeed());

  int i;
  for (i = 0; i < MAX_TIMERS; i++)
  {
    Timer *timer = Timers[i];
    if (timer != null)
    {
      if (!timer.Countdown())
      {
        // If timer has stopped, remove its reference from the array
        // (but keep event flag to let other scripts check it)
        timer.RemoveRef();
      }
    }
  }
}


Use example:
Code: ags

Timer *tSay;
function room_AfterFadeIn()
{
  tSay = Timer.StartRT(3.0, eRepeat);
}
function room_RepExec()
{
  if (Timer.HasExpired(tSay))
  {
    cBman.SayBackground("hello!");
  }
}

eri0o

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

#28
How. Did I. Not see this. Before now.

This would have saved me so much aggravation. Downloading and experimenting with it now!

eri0o

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

Quote from: eri0o on Wed 13/09/2017 22:33:50
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

#31
Quote from: Crimson Wizard on Wed 13/09/2017 22:48:11
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 to check if a tween was finished -- 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.
 

SMF spam blocked by CleanTalk