19 May 2009

System Time/Date Script

RMXP and RMVX has a built-in Time function that allows the display of the current date and time (based on the system clock). I found this by accident while sifting through the Help file - actually while searching for Bitmap and Sprite management - and after some experimentation discovered how to place it in windows.

To use it, there are two functions to be aware of:

Time.now

This reads the current time and date from the system clock.

strftime("%I:%M:%S %p")

This converts the time into a string, using the time of day (in 12-hour format), minutes and seconds, as well as showing AM/PM.

So to add the time and date, we can add the following:

t = Time.now
time = t.strftime("%I:%M:%S %p")
date = t.strftime("%a, %d %b, %Y")

What this will do is store the time/date stamp format into variables and convert them to strings so that they can then be "drawn" in a window.

In RMXP, the following script can be used to replace the default. In RMVX, create a new script called Window_PlayTime:
#=================================================================
# ** Window_PlayTime
#-----------------------------------------------------------------
#  This window displays play time on the menu screen.
#=================================================================
class Window_PlayTime < Window_Base
  #---------------------------------------------------------------
  # * Object Initialization
  #---------------------------------------------------------------
  def initialize
    super(0, 0, 160, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    refresh
  end
  #---------------------------------------------------------------
  # * Refresh
  #---------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.font.size = 16
    t = Time.now
    time = t.strftime("%I:%M:%S %p")
    date = t.strftime("%a, %d %b, %Y")
    self.contents.font.color = normal_color
    self.contents.draw_text(0, -2, 140, WLH, date, 1)
    self.contents.draw_text(0, 14, 140, WLH, time, 1)
  end
  #---------------------------------------------------------------
  # * Frame Update
  #---------------------------------------------------------------
  def update
    super
    if Graphics.frame_count / Graphics.frame_rate != @total_sec
      refresh
    end
  end
end
RMXP will display the replacement time/date in the default "Play Time" window on the menu status.

To make this work in RMVX, add the following to Scene_Menu:

In the start definition routine, add

@window_playtime = Window_PlayTime.new
@window_playtime.x = X
@window_playtime.y = Y

where X and Y are the x- and y-coordinates for the window placement.

Add the following to the terminate definition routine:

@window_playtime.dispose

This is to ensure that the window doesn't stay onscreen when the status menu has been exited.

And finally, add the following to the update definition routine:

@window_playtime.update

This is to ensure that the time refreshes every second while on the status menu.

The resultant "Play Time" window should now contain the current time and date, thus:

Tue, 19 May, 2009
12:33:22 PM

And that's it, but the strftime element does have a number of different format options, which I'll detail in the next entry.

10 Comments So Far:

steve said...

is there any way you can take the actual time and date and place them in separate in-game variables? like day(0-6) hour (0-23) minute (o-59) or something along those lines? i wish to make an rpg that runs of real-time events.

Companion Wulf said...

I'll be posting something on this over the next day or so.

awesomefist said...
This comment has been removed by the author.
Anonymous said...

Can you please just post the script once.
And not in 10 parts? I still don't know what to do with this script.

Companion Wulf said...

It isn't in 10 parts - even I'd be in trouble if it was! This script is pretty much plug-n-play. Just place it under Main and it'll place a window displaying the current (system) time/date. To change the position, alter the first two zeros in "span".

As far as using the Time/Date Formats with this script, you change the "time" and "date" variables depending on which how you'd like them to appear.

Example: If you want the date appearing as "Sun, 05 Dec, 2010", refer to the date/formats list and change the "date" to the following:

t.strftime("%a, %d %b, %Y")

And if you want the time appearing as "15:30", again refer to the date/formats list and change the "time" to the following:

t.strftime("%H:%M")

When the PlayTime window is displayed, the time and date will appear in the specified format.

Companion Wulf said...

Ugh! Alter the first two zeros in "super", I mean.

Anonymous said...

Would it be possible to cause certain events in the game to happen based on the date?

Anonymous said...

I think it would be possible if I could deposite the date value into one of the game's variables, but I have no idea how to do that.

Companion Wulf said...

To use variables and trigger time-based events, your best bet would be to use a script. I've compiled a list.

Anonymous said...

Hello everyone, it's my first pay a visit at this website, and piece of writing is genuinely fruitful designed for me, keep up posting such content.
My web site :: ipad video training review

Post a Comment

Related Posts with Thumbnails
Template Design "Perfect World" by SkinCorner