Get, compare, and store System Time

Discussion in 'RGSS3 Scripts (RMVX Ace)' started by autophagy, Jun 10, 2014.

  1. autophagy

    autophagy tank nerd Veteran

    Messages:
    75
    Likes Received:
    61
    Location:
    Austria
    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

    [​IMG]

    [​IMG]

    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.
     
    #1
    Ebanyle likes this.
  2. tale

    tale Volunteer Veteran

    Messages:
    379
    Likes Received:
    577
    Location:
    USA
    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
    ################
     
    #2
    Ebanyle likes this.

Share This Page