The RPG Maker Resource Kit

RMRK RPG Maker Creation => XP => XP Scripts Database => Topic started by: shaz on August 30, 2009, 12:36:47 AM

Title: Multi-timers - set and forget timers for game switches and event self switches
Post by: shaz on August 30, 2009, 12:36:47 AM
Multi-timers
Version: 1.0
Author: Shaz
Date: August 30, 2009

Version History



Planned Future Versions


Description


This script allows you to set timers to turn on an unlimited number of switches or event self switches after a set delay.  

Features


Instructions

Copy the entire script below into a new slot above Main and below all other scripts.

Create a Script event command, and place the call to $game_system.set_timer inside with the appropriate arguments.

To set a timer for switch 15 to be turned on in 30 minutes, use $game_system.set_timer(30, 15)
Have an event or common event that's conditioned to run when switch 15 is on.
Make sure you turn the switch off again if you only want it to run once.

To set a timer for self switch A on map 8, event 32 to be turned on in 45 seconds (3/4 or 0.75 of a minute), use $game_system.set_timer(0.75, [8, 32, 'A'])
You can also use $game_map.map_id and @event_id if you're referring to the current event on the current map: $game_system.set_timer(0.75, [$game_map.map_id, @event_id, 'A'])

Script


Code: [Select]
#==============================================================================
# ? Multi_Timer
#------------------------------------------------------------------------------
#   This script allows you to set up multiple timers in your game, to
#   turn on game switches or event self switches after a set delay.
#
#   To set a timer to turn a switch on, call
#     $game_system.set_timer(minutes, switch)
#
#   To set a timer to turn a self switch on, call
#     $game_system.set_timer(minutes, [mapid, eventid, selfswitch])
#
#   This switch or self switch will be turned on when the time elapses.
#   The trigger is then removed from the list of timers.
#   Self switches can be set & turned on even if you're not on that map.
#
#   Note - for fractions of minutes, pass in 0.5 rather than .5
#==============================================================================

class Game_System
  #--------------------------------------------------------------------------
  # ? Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :event_timer              # Switch/SelfSwitch timer
  
  #--------------------------------------------------------------------------
  # * Alias Listings
  #--------------------------------------------------------------------------
  alias_method :shaz_game_system_init, :initialize
  alias_method :shaz_game_system_update, :update
  
  #--------------------------------------------------------------------------
  # ? Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    shaz_game_system_init
    @event_timer = {}
  end

  #--------------------------------------------------------------------------
  # ? Update
  #--------------------------------------------------------------------------
  def update
    shaz_game_system_update
    check_event_timers
  end
  
  #--------------------------------------------------------------------------
  # ? Set an event timer to trigger a switch or a self switch
  #--------------------------------------------------------------------------    
  def set_timer(minutes, key)
    # If you're introducing this into a game where players will have saved files,
    # uncomment the following line to avoid nil object errors
    # @event_timer = {} if @event_timer == nil
    
    # this is the frame_count at which the event should trigger,
    # based on the current frame rate
    @event_timer[key] = Graphics.frame_count + (minutes * 60 * Graphics.frame_rate)
  end
  
  #--------------------------------------------------------------------------
  # ? Check event timers
  #--------------------------------------------------------------------------    
  def check_event_timers
    @event_timer.each do |key, value|
      if value <= Graphics.frame_count
        if key.is_a?(Array)
          $game_self_switches[key] = true
          $game_map.events[key[1]].refresh if key[0] == $game_map.map_id
        else
          $game_switches[key] = true
          $game_map.refresh
        end
        @event_timer.delete(key)
      end
    end
  end
end  

Credit



Thanks


Support


Post questions in this thread or send me a PM

Known Compatibility Issues

This should not have any compatibility issues with any other systems, unless one of them defines @event_timer, set_timer or check_event_timers for $game_system.

Demo


Demo attached.
The male NPC and the event in the top left corner combine to show an example of setting switches using the timer (NPC sets switch, corner event is conditioned by switch).
The flowers along the back provide examples of setting event self-switches using the timer.



Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: shaz on August 30, 2009, 12:43:25 AM
Ugh!  I can't believe I posted this in VX scripts! 

Can a mod please move it to XP for me?  Sorry...
Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: Grafikal on August 30, 2009, 12:43:33 AM
This is an XP script, not VX. Careful where ya post. I moved it for ya.

edit:: lol, i moved it before you posted that comment ~ im just slow on the upkeep here typing these things :P
Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: shaz on August 30, 2009, 12:45:18 AM
lol - no - you're fast.  Lightning fast!

Thanks :)
Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: turdalmighty on August 30, 2009, 01:15:42 AM
This is very useful. I've been looking for something like this for my game. Thanks!
Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: modern algebra on September 02, 2009, 05:44:38 PM
This is a great script. Nice work, shaz!
Title: Re: Multi-timers - set and forget timers for game switches and event self switches
Post by: shaz on September 02, 2009, 09:14:26 PM
not nearly as fancy as yours, but even little scripts can be handy ;)

I'm going to update it over the weekend to allow a timer to turn a switch ON or OFF.