Multiple Timers
By: Ares
IntroductionBasically, 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
- #==============================================================================
- # 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[i].active==true
- @timers[i].time-=1 if @timers[i].time>0
- @timers[i].active=false if @timers[i].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[i].active==true
- result.push(@timers[i])
- 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[i].name==name
- return true if $game_timer.timers[i].time==0
- end
- end
- end
-
- end
Expand to see the code.
InstructionsStep 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)
FAQQ: 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. CompatibilityI haven't tested it for any compatibility issues, so please report them should you encounter them (which I think you will not).
Credits and ThanksThanks to Totum for "requesting" the script.
Author's NotesThis 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 ConditionsPlease 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.