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?

Totum

Member

Hi there!
I would like to make both a weather system and a day/night system. This would be a lot easier if i could use two timers. So far I had no luck and I was wondering if it is possible to use multiple timers at the same time. Anyone?
Thanks!
 

Ares

Member

I don't think this is possible in RMXP without a script, so I'll try and make you one.
Give me a few days and I should have finished it I think :)

Edit: I don't know how used you are to using scripts in Rmxp, so I'll try to make it as easy-to-use as possible.
 

Totum

Member

That would be really great, Ares! I'm just starting to use scripts, but I hope I get to understand it -and write it- in some time. I'm looking forward to your script!
 

Ares

Member

Well, I managed to make it, but they won't be shown on the screen (didn't have time to figure that out). You can still use it, though. I didn't test it completely, but it gave me no errors when doing some basic stuff with it.
If you do want to show the timers, I can make it for you, but it will take a bit longer, as I am quite busy as of tomorrow.
[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_accessor :timers
  attr_reader :active_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 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]

The instructions are in the header of the script. To implement it into your game, copy the script (line numbers off!) and paste it into a new script slot above 'main'.

If you get any errors or if you have any questions, feel free to ask (you can also pm me).

Edit: Now that I think of it I'm not even sure you're using RMXP (I think so based on your reaction to me). Maybe next time you should use the rmxp topic icon, and/or clarify what engine you're using in the first post.
 

Totum

Member

Hi, thanks for the code! It's great you made this! However, I have some problems implementing the code, for I don´t have very much expierience with scripting at the moment. I especially don't understand these lines:

# $game_timer.timers returns an array of all timers
# $game_timer.active_timers returns an array of all active timers

What should I do with them? It would be great to get the timers working, so I really hope someone can explain it to me!

PS.
I do indeed use the RMXP, I'll put it in the topic name next time :)
 

Ares

Member

Sorry I never got back to you :eek:
Totum":1hrji6l1 said:
# $game_timer.timers returns an array of all timers
# $game_timer.active_timers returns an array of all active timers
This may be useful if you wanted to do something script-related with the timers, but i think it should work by just using the set_timer, timer_exist?, at_time?, finished? methods.

If you haven't already figured it out, I'd be happy to whip up a quick tutorial for you.
 

Totum

Member

Ares":2gbmwlnp said:
Sorry I never got back to you :eek:
This may be useful if you wanted to do something script-related with the timers, but i think it should work by just using the set_timer, timer_exist?, at_time?, finished? methods.

If you haven't already figured it out, I'd be happy to whip up a quick tutorial for you.

I think I will need that tutorial! I have been working on some graphics and other parts of the game but I'm still anxious to get this timer thing working! So it would be great if you would write a tutorial, or else have a link where I can learn about scripting in RMXP (might be necessary anyway :P )!
 

Ares

Member

I am currently working on a few example scenario's for you, I'll post them once I've finished them.
I learned a lot of RGSS from this tutorial, just create a new project and follow the steps that are there, and then try to expand the scripts on your own.
Trying + failing + looking up = good method to learn rgss for me.

edit: here are two pictures which I think are the most plain options of this system. If this doesn't help you out, maybe you could be more specific on what you're trying to do and how you want to do it, so I can help more directly.
Common_event.PNG
Event_input.PNG
 
Trying + failing + looking up = good method to learn rgss for me.
so true for me as well, i never would have written any of my amazing scripts if i didn't fail like 50,000 times and kept getting up, well that and aton of patience on Blue's part. that reminds me i really need to clean up the code on like 5 of my scripts and post them lol
 

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