Multiple Timers

Multiple Timers
By: Ares


Introduction
Basically, this script allows you to set up invisible timers which you can use to control events. This was made upon "request" of Totum, but I thought it might be helpful for other people too.

Features
  • Practically unlimited amount of timers (I don't know why you would need over ~50 active timers simultaneously anyway...)
  • Short methods, easy to work with

Screenshots
<Post screenshots of the script in action here. If the purpose of the script is not obvious from a single screenshot, try storyboarding the function of the script with screenshots. If screenshots cannot be taken of the system in action, please say so to prevent people from asking.>

Demo
-on demand-

Script
  1. #==============================================================================
  2. # Multiple Timers by Ares
  3. #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  4. #  1. To set a new timer, use a call script command and enter this (without quote
  5. #      marks):
  6. #    "set_timer(name,time)" -> set_timer("timer1",40) [example]
  7. #        (where name is an identifier, like "timer1" (with quotation marks),
  8. #         and time is the amount of time in seconds.
  9. #
  10. #  2. To check wether a timer has reached a certain point(other than 0), use
  11. #      a conditional branch, toggle 'script' on page 3 and enter this
  12. #      (without quote marks):
  13. #    "at_time?(name,time)==true" -> at_time?("timer1",20)==true [example]
  14. #        (where 'time' represents the value (in seconds) you want to check the
  15. #         timer with, and 'name' the name of the timer you want to check)
  16. #
  17. #  3. To check wether a timer has reached 0, use a conditional branch, toggle
  18. #      'script' on page 3 and enter this (without quotation marks):
  19. #    "finished?(name)" -> finished?("timer1")==true [example]
  20. #        (where 'name' is the name of the timer you want to check)
  21. #
  22. #   $game_timer.timers returns an array of all timers
  23. #   $game_timer.active_timers returns an array of all active timers
  24. #------------------------------------------------------------------------------
  25.  
  26. #------------------------------------------------------------------------------
  27. # The main timer class
  28. #------------------------------------------------------------------------------
  29.  
  30. class Game_Timer
  31.  
  32.   attr_reader   :active_timers
  33.   attr_accessor :timers
  34.  
  35.   def initialize
  36.     @timers=[]
  37.     @active_timers=get_active_timers
  38.   end
  39.  
  40.   def update
  41.     for i in 0...@timers.size
  42.       if @timers[i].active==true
  43.         @timers[i].time-=1 if @timers[i].time>0
  44.         @timers[i].active=false if @timers[i].time==0
  45.       end
  46.     end
  47.   end
  48.  
  49.   def add(timer)
  50.     @timers.push(timer)
  51.   end
  52.  
  53.   def get_active_timers
  54.     result=[]
  55.     for i in 0...@timers.size
  56.       if @timers[i].active==true
  57.         result.push(@timers[i])
  58.       end
  59.     end
  60.     return result
  61.   end
  62.  
  63. end
  64.  
  65. # Timer Object
  66.  
  67. class Timer
  68.  
  69.   def initialize(name,time,active=true)
  70.     @name=name
  71.     @time=time*Graphics.frame_rate
  72.     @active=active
  73.   end
  74.  
  75.   attr_reader   :name
  76.   attr_accessor :time
  77.   attr_accessor :active
  78. end
  79.  
  80. # Create Timer instance
  81. begin
  82.   $game_timer = Game_Timer.new
  83. end
  84.  
  85. # Update timer class every frame refresh.
  86. module Graphics
  87.   class << self
  88.     alias :timer_update :update
  89.     def update
  90.       timer_update
  91.       $game_timer.update
  92.     end
  93.   end
  94. end
  95.  
  96. class Interpreter
  97.  
  98.   def set_timer(name,time)
  99.     $game_timer.add(Timer.new(name,time,true))
  100.   end
  101.  
  102.   def pause_timer(name)
  103.     for timer in $game_timer.timers
  104.      timer.active=false if timer.name==name
  105.     end
  106.   end
  107.  
  108.   def activate_timer(name)
  109.     for timer in $game_timer.timers
  110.      timer.active=true if timer.name==name
  111.     end
  112.   end
  113.  
  114.   def timer_exist?(name)
  115.     for timer in $game_timer.timers
  116.      return true if timer.name==name
  117.     end
  118.   end
  119.  
  120.   def at_time?(name,time)
  121.     for timer in $game_timer.timers
  122.      unless timer.active==false
  123.       if timer.name==name
  124.         return true if timer.time==time*Graphics.frame_rate
  125.       end
  126.     end
  127.     end
  128.   end
  129.  
  130.   def finished?(name)
  131.     for i in 0...$game_timer.timers.size
  132.      if $game_timer.timers[i].name==name
  133.       return true if $game_timer.timers[i].time==0
  134.      end
  135.     end
  136.   end
  137.  
  138. end
Expand to see the code.


Instructions
Step 1. Copy/Paste Script into new script slot above 'main'.
Step 2. Enjoy!

Functions
  • set_timer("name",time[,active]) - using this in a call script will create a timer named "name", which will run for time seconds if active is true. (active is true by default)
  • pause_timer("name") - will pause timer "name"
  • activate_timer("name") - will activate timer "name"
  • timer_exist?("name") - will return true if timer "name" exists, and will return false if it doesn't.
  • at_time?("name", time) - will return true if timer "name" is at time. Otherwise returns false.
  • finished("name") - will return true if timer "name" has reached 0 (equal to at_time("name",0), but shorter)


FAQ
Q: Why is 'request' in quotation marks?
A: He didn't actually request the script, I designed it for him to solve his problem. But it is inspired by him, that's why he's in the credits.

Compatibility
I haven't tested it for any compatibility issues, so please report them should you encounter them (which I think you will not).

Credits and Thanks
Thanks to Totum for "requesting" the script.

Author's Notes
This script was whipped up quickly, so I'm open to any suggestions. Feel free to ask questions, I'll answer them if I have the time.

Terms and Conditions
Please do not redistribute this script on another forum (other than http://www.hbgames.org) without my permission (which you can get by PM-ing me).
You are free to use this script if you just mention my name in your credits.
i like it, i wrote my own version not too long ago, yours is clean and well documented.
Quote:
(I don't know why you would need over ~50 active timers simultaneously anyway...

i made my version of this script for respawning events. i like your idea for being able to check what time in the event is, i could do alot with that, thank you :D
Well, hmm. I saw the name of this thread, and was sure I'd find something else. I don't mean a script that does something else, though. I just mean this. I made that a couple of years ago, and never got much in the way of comments on the hbgames thread. I had assumed that this thread was some kind of repost, so my apologies for that.

Anyway, I have to admit, your solution is clean and simple. This was effectively the first script I finished, and it has a lot of stuff that could probably be done better, like you appear to have done.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=73922