Help - Search - Members - Calendar
Full Version: Super Simple No-Lag Timers
RPG RPG Revolution Forums > Scripting > Script Submissions > RGSS-Submissions
amaranth
Introduction
This script lets you create as many timers as you want without causing your game to lag. It's great if you want events to occur only after a specific amount of time has passed. For example, you go to a village, come back in 30 minutes, and a chicken has laid an egg.

This script contains two timers. You can always add more (I'll discuss below).

Update Game_System
In this section, we are going to add two timers.

1. Open your game and script editor.

2. In Game_System, add these methods at the bottom of the file, but above the last end tag:

CODE
  #--------------------------------------------------------------------------
  # ● set the current time to a variable (set timer)
  #--------------------------------------------------------------------------  
  def set_time(num)
    
      @total_sec = Graphics.frame_count / Graphics.frame_rate
      time = @total_sec/60 # minutes /60, hour /60 /60

      case num
        when 1 # Timer 1
            $game_variables[1] = time
        when 2 # Timer 2
            $game_variables[2] = time
        end
      
  end
  
  #--------------------------------------------------------------------------
  # ● compare two times
  #--------------------------------------------------------------------------  
  def get_time()
      @total_sec = Graphics.frame_count / Graphics.frame_rate
      time = @total_sec/60 # minutes
      $game_variables[10] = time  #Timer: in-game variable
  end


3. Save your script.

Create Timers Variables
Once your timers are added to your script, you can turn them on and off with variables.

Note: The number assigned to each data entry needs to match the number assigned to each switch. For example, @data[2] = "Get milk for Ma" | 0002: Help Ma.


1. Add a new event to one of your maps.

2. Open the Event Commands window.

3. Click Control Variables and add the following:
0001: Start Timer 1 (Blue Egg)
0002: Start Timer 2 (Brown Egg)
0010: Current Time

0011: Stop Timer 1 (Blue Egg)
0012: Stop Timer 2 (Brown Egg)
0020: Lay Blue Egg
0021: Lay Brown Egg

4. Save and delete event.

Create An Event that Starts Timer 1
Use this event to start Timer 1.

1. Add a new event to one of your maps.

2. Open the Event Commands window.

3. Click Script and enter: $game_system.set_time(1)

4. Save your work and close the Edit Event dialog box.

Repeat these steps for Timer 2. In step 3, replace 1 with 2.

Create An Event that Checks if Timer 1 has Finished
Use this event to stop Timer 1.

1. Add a new event to one of your maps.

2. Open the Event Command Window.

3. Command Window > Script. Enter: $game_system.get_time(). Save.

4. Command Window > Control Variables. To the right of Single, select 0011: Stop Timer 1. Select the = operator. To the right of Variable, select 0010: Current Time. Save.

7. Command Window > Control Variables. To the right of Single, select 0011: Stop Timer 1. Select the - operator. To the right of Variable, select 0001: Start Timer 1. Save.

9. Command Window > Click Control Branch > To the right of Variable, select 0011: Stop Timer 1. Select Greater than. To the right of Constant, enter the amount of minutes for the timer. For example, 60. Save.

10. Inside the conditional branch, put the commands that should happen when the timer has finished.



Repeat these steps for Timer 2, using timer 2 variables.

How do I add more Timers?
Adding new timers is easy! Open the script editor and go to Game_System. Add a new entry to def set_time(num) as follows:

def set_time(num)

@total_sec = Graphics.frame_count / Graphics.frame_rate
time = @total_sec/60 # minutes

case num
when 1 # Timer 1
$game_variables[1] = time
when 2 # Timer 2
$game_variables[2] = time
when 3 # Timer 3
$game_variables[3] = time

end

end
Blizzard
Uhm... Why does one need a script for a timer?
amaranth
The default timers cause lag... especially if you have more than one running.
Blizzard
Oh no, you misunderstood what I meant. You really don't need a script for that.

woratana
I didn't know timers cause lag, since I've never use multiple timers at a time. As in Blizzard's post, this can be done without script, unless you want to show timer on the screen (This part is also can be done with event, but script is easier). Anyway, thanks for sharing your script here. I'm sure someone will find this useful. ^^
Blizzard
Generally sprites cause lag. Just storing the value of a variable and restoring it doesn't cause any lag at all.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.