Get, compare, and store System Time

autophagy

tank nerd
Veteran
Joined
Jun 9, 2014
Messages
81
Reaction score
72
First Language
German
Primarily Uses
RMMV
Hello!

Today, out of necessity, I had to try my hand at creating my very first Ruby script. It's only small, but I thought that some people who have no scripting knowledge might find it useful. I am pretty sure that it is bug free, since it's difficult to put many bugs into so few lines. I don't know if something like this exists already, but if it does, I was unable to find it. Maybe someone else will have a use for it too.

If people want me to add other date functions (such as converting the timestamp this script gives you into an actual date with day, month, year, etc) I can do that.

What it does

I was looking for a method to count the passing of seconds in the game. This is possible through eventing and using a wait command in a loop, however I had trouble with the accuracy, and it also had a very serious fault...if you tab out of the game (or quit, obviously), the wait command will pause and therefore mess up your calculations. To solve this, I created a script that can keep track of time even when the game events aren't running. It does so by simply taking the system's current time and putting it into a variable whenever you call the method. It also contains a second method that allows you to use this variable and find out how much time has passed since you stored the last time...and put the result in seconds into another variable.

Possible uses

You could use it to keep track of how long a BGM/BGS/ME/SE has been playing to stop it at a specific time - since the audio keeps playing even if you tab out of the window (that was my original problem), or you could let the player know how long it has been since they last played the game, make your actors yell at the player if they haven't played in days, or show how much lifetime they have already invested in your pixels,...Basically, whatever you can think of.

Screenshots





How to use

The script gives you two methods to work with:

get_time(variable_id)You can use this in an event with the call script command. Replace variable_id with the ID of the variable you want the current time (in seconds since 1970) stored in, and this variable will receive the data.

compare_time(variable_then,variable_difference)This will let you store the difference in seconds between the value the first method gave you and the current time. Replace variable_then with the ID of the variable where you stored the time, and variable_difference with the ID of the variable you want the result to be stored in.

That's it. You can also find the instructions in the script itself.

The actual script

##################################### Time to Variable Script# by autophagy##################################### VERSION: 1.0 created 9th June 2014##################################### DESCRIPTION:# Can save the current time as a timestamp into a variable and compare it.# I was trying to figure out a way to measure the passing of seconds ##################################### INSTRUCTIONS:# *Use get_time(variable_id) in a script call to save current timestamp in a # variable.# Replace variable_id with the id of the variable you want the time to be stored # in.# * Use compare_time(variable_id_stored,variable_id_difference) to compare stored # timestamp to current time.# This will substract the stored variable with the id variable_id_stored # from the current timestamp and put the difference in seconds into the # variable with the id variable_id_difference.# * Go wild.###################################### DON'T TOUCH THIS#####################################def get_time (variable_id) now = Time.now.to_i $game_variables[variable_id] = nowenddef compare_time(variable_then,variable_difference) now = Time.now.to_i $game_variables[variable_difference] = now - $game_variables[variable_then]end################# end of script################ 
Have fun! Please credit me if you use this script.
 

tale

Volunteer
Veteran
Joined
Dec 16, 2016
Messages
631
Reaction score
1,063
First Language
English
Primarily Uses
N/A
Fixed script format, credit autophagy.
Code:
####################################
# Time to Variable Script
# by autophagy
####################################
# VERSION: 1.0 created 9th June 2014
####################################
# DESCRIPTION:
# Can save the current time as a timestamp into a variable and compare it.
# I was trying to figure out a way to measure the passing of seconds
####################################
# INSTRUCTIONS:
# *Use get_time(variable_id) in a script call to save current timestamp in a
#   variable.
#   Replace variable_id with the id of the variable you want the time to be stored
#   in.
# * Use compare_time(variable_id_stored,variable_id_difference) to compare stored
#     timestamp to current time.
#     This will substract the stored variable with the id variable_id_stored
#     from the current timestamp and put the difference in seconds into the
#     variable with the id variable_id_difference.
# * Go wild.
#####################################
# DON'T TOUCH THIS
#####################################

def get_time (variable_id)
  now = Time.now.to_i
  $game_variables[variable_id] = now
end

def compare_time(variable_then,variable_difference)
  now = Time.now.to_i
  $game_variables[variable_difference] = now - $game_variables[variable_then]
end

################
# end of script
################
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

A collab cover of White Wishes with @PixeLockeT!!~

Multiple takes of my recordings ended in this way, I wish I could've done it better, but I still love this!
Just saw an article stating a Back to the FutureXTransformer comic crossover--and Doc Browns car is an Autobot. I am ALL FOR THIS!!
Well I sure didn't know fans could overheat and die. Room fans.
Felt like I dodged a bomb, my project was fine right after I force resetted due to a crash as I have yet to start working on it today..... :kaoback:
I have no idea what to do with my evening. I was working on the next Slip into Ruby but I kinda want to do something else for a bit. But I have no idea what. Yay indecision!

Forum statistics

Threads
99,176
Messages
962,657
Members
130,751
Latest member
Julian16
Top