Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Multiple Timers

Ares

Member

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
[rgss]#==============================================================================
# Multiple Timers by Ares
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#  1. To set a new timer, use a call script command and enter this (without quote
#      marks):
#    "set_timer(name,time)" -> set_timer("timer1",40) [example]
#        (where name is an identifier, like "timer1" (with quotation marks),
#         and time is the amount of time in seconds.
#
#  2. To check wether a timer has reached a certain point(other than 0), use
#      a conditional branch, toggle 'script' on page 3 and enter this
#      (without quote marks):
#    "at_time?(name,time)==true" -> at_time?("timer1",20)==true [example]
#        (where 'time' represents the value (in seconds) you want to check the
#         timer with, and 'name' the name of the timer you want to check)
#
#  3. To check wether a timer has reached 0, use a conditional branch, toggle
#      'script' on page 3 and enter this (without quotation marks):
#    "finished?(name)" -> finished?("timer1")==true [example]
#        (where 'name' is the name of the timer you want to check)
#
#   $game_timer.timers returns an array of all timers
#   $game_timer.active_timers returns an array of all active timers
#------------------------------------------------------------------------------
 
#------------------------------------------------------------------------------
# The main timer class
#------------------------------------------------------------------------------
 
class Game_Timer
 
  attr_reader   :active_timers
  attr_accessor :timers
 
  def initialize
    @timers=[]
    @active_timers=get_active_timers
  end
 
  def update
    for i in 0...@timers.size
      if @timers.active==true
        @timers.time-=1 if @timers.time>0
        @timers.active=false if @timers.time==0
      end
    end
  end
 
  def add(timer)
    @timers.push(timer)
  end
 
  def get_active_timers
    result=[]
    for i in 0...@timers.size
      if @timers.active==true
        result.push(@timers)
      end
    end
    return result
  end
 
end
 
# Timer Object
 
class Timer
 
  def initialize(name,time,active=true)
    @name=name
    @time=time*Graphics.frame_rate
    @active=active
  end
 
  attr_reader   :name
  attr_accessor :time
  attr_accessor :active
end
 
# Create Timer instance
begin
  $game_timer = Game_Timer.new
end
 
# Update timer class every frame refresh.
module Graphics
  class << self
    alias :timer_update :update
    def update
      timer_update
      $game_timer.update
    end
  end
end
 
class Interpreter
 
  def set_timer(name,time)
    $game_timer.add(Timer.new(name,time,true))
  end
 
  def pause_timer(name)
    for timer in $game_timer.timers
     timer.active=false if timer.name==name
    end
  end
 
  def activate_timer(name)
    for timer in $game_timer.timers
     timer.active=true if timer.name==name
    end
  end
 
  def timer_exist?(name)
    for timer in $game_timer.timers
     return true if timer.name==name
    end
  end
 
  def at_time?(name,time)
    for timer in $game_timer.timers
     unless timer.active==false
      if timer.name==name
        return true if timer.time==time*Graphics.frame_rate
      end
    end
    end
  end
 
  def finished?(name)
    for i in 0...$game_timer.timers.size
     if $game_timer.timers.name==name
      return true if $game_timer.timers.time==0
     end
    end
  end
 
end
[/rgss]

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.
(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.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top