Accurate Playtime

ChampX

Veteran
Veteran
Joined
Aug 14, 2016
Messages
188
Reaction score
123
First Language
English
Primarily Uses
RPG Maker MV is designed to run generally at 60 fps. As such, it keeps track of the total amount of play time a player has been playing by monitoring the total number of frames that have been run. This is all fine and dandy if the game is always running at 60 fps. If, however, the player is on a machine that is older and cannot run MV games at 60 fps, the accuracy of the total play time way off. If the game is running at 30 fps on average, then the play time recorded by MV will be HALF of the play time played in reality. The inverse is also true if the game was running at higher frame rates, which can happen on 144hz monitors.

This simple plug-n-play plugin addresses this issue by using real-time to keep track of the total play time played by the player instead of the total number of frames.

You can also reset the playtime as well, useful for those with games that start a little bit after the new game is launched. You can also pause and resume the playtime if you wish to avoid the game ticking up during cutscenes.

To use: Simply copy/paste the plugin code below into a text editor and save it as AccuratePlaytime.js (you can call it whatever you want actually as long as you include the .js). To reset the play time, type ResetPlaytime in a plugin command window. To pause the play time, type PausePlaytime and to resume the play time, type ResumePlaytime. Pausing will not persist if the game is saved then reloaded.

Terms: Use as you like. Free or commercial. Modify or extend as needed. Credit isn't even necessary though you are welcome to.

Code:
/*:
* @plugindesc Counts total playtime accurately regardless of framerate
* @help This plugin is plug-n-play and requires no parameters to set.
* To reset the play time, type ResetPlaytime in a plugin command window
* Version 1.3: Pause and ResumePlaytime commands added, also addressed a bug with doubling playtime
* Version 1.2: ResetPlaytime command
* Version 1.1: Addresses bug fix to saves not storing time correctly after multiple saves
*/

(function()
{
   
    var startTime = 0;
    var pausedTime = 0;
    var paused = false;
    
    var _DataManager_setupNewGame = DataManager.setupNewGame;
    var _GameSystem_initialize = Game_System.prototype.initialize;
    var _GameSystem_onBeforeSave = Game_System.prototype.onBeforeSave;
    var _GameSystem_onAfterLoad = Game_System.prototype.onAfterLoad;  
    var _GameInterpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
    Game_Interpreter.prototype.pluginCommand = function(command, args) 
    {
        _GameInterpreter_pluginCommand.call(this, command, args);
        if (command === 'ResetPlaytime') 
        {
            startTime = Date.now();
            $gameSystem._playtime = 0;
        }
        if (command === 'PausePlaytime')
        {
            if (paused) return;
            paused = true;
            pausedTime = Date.now() - startTime;
        }
        if (command === 'ResumePlaytime')
        {
            if (!paused) return;
            paused = false;
            startTime = Date.now() - pausedTime;
        }
    };
    DataManager.setupNewGame = function() 
    {
        _DataManager_setupNewGame.call(this);
        startTime = Date.now();
    };
    
    Game_System.prototype.initialize = function() 
    {
        _GameSystem_initialize.call(this);
        this._playtime = null;
    };
    Game_System.prototype.onBeforeSave = function() 
    {
        _GameSystem_onBeforeSave.call(this);
        var saveTime = Date.now() - startTime;
        this._playtime = paused ? this._playtime + pausedTime : this._playtime + saveTime;
        startTime = Date.now();
        pausedTime = 0;
    };
    Game_System.prototype.onAfterLoad = function() 
    {
        _GameSystem_onAfterLoad.call(this);
        startTime = Date.now();
    };
    Game_System.prototype.playtime = function() 
    {
        return Math.floor((paused ? (this._playtime + pausedTime) : (Date.now() - startTime + this._playtime)) / 1000);
    };

})();
 
Last edited:

JohnOminae

Villager
Member
Joined
Mar 13, 2012
Messages
28
Reaction score
20
First Language
English
Primarily Uses
Other
Thanks for this, ChampX! I can see this one being essential. Gonna try this out soon! :D
 

TenTranVN

Veteran
Veteran
Joined
Apr 13, 2017
Messages
165
Reaction score
17
First Language
Vietnam
Primarily Uses
RMMV
Does this plugin reduce lag for weak devices?
 

ChampX

Veteran
Veteran
Joined
Aug 14, 2016
Messages
188
Reaction score
123
First Language
English
Primarily Uses
@TenTranVN No it doesn't reduce any lag. It instead just makes it so the playtime counter is accurate regardless of if there is lag or not.
 

SiiOs

Villager
Member
Joined
Oct 13, 2017
Messages
16
Reaction score
4
First Language
English
Primarily Uses
RMMV
Was just looking for something like this today and stumbled across the thread. Did as you said and it's working great. That inaccurate playtime was bugging me so bad. Thanks!
 

ChampX

Veteran
Veteran
Joined
Aug 14, 2016
Messages
188
Reaction score
123
First Language
English
Primarily Uses
Updated to address bug fix regarding time not storing properly after multiple saves
 

Ace of Spades

Veteran
Veteran
Joined
Jan 19, 2017
Messages
103
Reaction score
133
First Language
English
Primarily Uses
Updated to address bug fix regarding time not storing properly after multiple saves
Thanks for the update, it seems to be working properly now. Quick question; I was wondering if there's a script call I can use to reset the time played back to 0. I'm using an evented title screen, so by the time the player starts their first game and gets past the title, there's already a minute or so on the save file. It'd be nice to be able to reset the timer after the opening cinematic, right after the player gains control of their character.
 

ChampX

Veteran
Veteran
Joined
Aug 14, 2016
Messages
188
Reaction score
123
First Language
English
Primarily Uses
Added a bug fix to where time could potentially double on same slot sessions. Also added Pause and Resume Playtime commands.
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Managed to find a way to hang a bed cover over my window. So no more being toasted alive in the afternoon!:kaoswt2:
So, uh, has anyone tried buying any resources from KokoroReflections? Because I've been looking for a 'buy' button on the site for like 10 minutes now and can't find one.
Any also thought it's too cringy to put the word "and you" in your credit as a special thanks?
Cause that's what I thought...
Everyone in the US right now: Please stay safe.
Symphony Celestia: Prophecy of the Falling Stars RPG Maker music pack WIP. Symphony Celestia is a new series that focuses on symphonic and orchestrated music to tell a story. Each story will include compositions that make up that story from beginning to end. Themes include: Battles, adventures, character themes, etc. Below is a video preview to the first story.

Forum statistics

Threads
97,980
Messages
948,329
Members
129,246
Latest member
Flaminguex
Top