Accurate Playtime

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);
    };

})();
Thanks for this, ChampX! I can see this one being essential. Gonna try this out soon! 😀
Does this plugin reduce lag for weak devices?
@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.
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!
Updated to address bug fix regarding time not storing properly after multiple saves
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.
@Ace of Spades ResetPlaytime command has been added
@Ace of Spades ResetPlaytime command has been added
Perfect, works great. Thank you!
Added a bug fix to where time could potentially double on same slot sessions. Also added Pause and Resume Playtime commands.
Is this compatible with MZ?
@Animebryan I honestly do not know. Probably? I don't see why not as it isn't doing anything too special. I don't use MZ though.

If anyone wishes to test it out in MZ and if necessary make edits to make it compatible, be my guest!
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);
    };

})();
I feel like RM MV does not count time spent in battle to your overall playtime. Will this remedy that as well?
@justincofield998 I never tested if RMMV by default didn't count battle frames toward the playtime, however this plugin will include battle time due to the nature of how I wrote it. This plugin measures time from the system clock instead of frames elapsed.
Thank you for this. I used this recently and ended up making a small edit to the onAfterLoad function to make it compatible with older saves.

Code:
Game_System.prototype.onAfterLoad = function()
    {
        _GameSystem_onAfterLoad.call(this);
        if (!this._playtime){
            var t1 = new Date(1970, 0, 1);
            var t2 = new Date(1970, 0, 1, Math.floor(Math.floor($gameSystem._framesOnSave / 60) / 60 / 60).padZero(2), Math.floor(Math.floor($gameSystem._framesOnSave / 60) / 60 % 60).padZero(2), Math.floor(($gameSystem._framesOnSave / 60)  % 60).padZero(2));
            this._playtime = t2 - t1;
        }
        startTime = Date.now();
    };
So, I was toying with a game idea, and wanted a total play time after the player wins.
But the key element of my game, is that you die, and start over a new game, retaining some global variables/switches from gameplay, and your own memory.
So I'd need it to retain playtime across dying/gameover/start new game.

Since the loop would run as follows:

Start your first game -> play for say 6 minutes -> die -> start a new game -> play for 45 minutes -> die -> repeat start new game
So I'd need to be able to save the 6 minutes and add that to the 45 minutes.
I'm not sure if I'd need a script edit, or if I could just use a couple variables.
Total Playtime :
Current Playtime :
Where current playtime is the playtime for the current run, and total playtime is the overall.
So on death, it could do Total = (Current + Total) to get a new total?

Original thread: https://forums.rpgmakerweb.com/threads/accurate-playtime.104735/