How to monitor for missing database file?

I'm working on a plugin that will create a new json file in the data folder.  After creation it will be loaded along with all the database files.


My problem is that until the code in the plugin is run to populate and save this file, it won't exist.


Is there a way I can override this part of the DataManager.loadDataFile so if my particular file doesn't exist, it just creates an empty object, without having to rewrite that function?


xhr.onerror = function() {
DataManager._errorUrl = DataManager._errorUrl || url;
};


I am trying to avoid plugin users having to create that file manually.


Edit: I suppose I could just do a fs.existsSync test to see if the file is there, and create it if it isn't, rather than worrying about handling a missing file when loading the database.
Edit: I suppose I could just do a fs.existsSync test to see if the file is there, and create it if it isn't, rather than worrying about handling a missing file when loading the database.

That will only work in nw.js, but you also won't be able to create the file without nw.js.
The file will only ever be created when running in test mode.
Ah, right. Then existsSync is a good way to do it.
different question, but for same plugin ...


Can I do something like this, and only have the additional code run when in test mode?


(function() {

// code in this section will only be run when play testing
if (Utils.isOptionValid('test')) {

var _Game_Interpreter_clear = Game_Interpreter.prototype.clear;
Game_Interpreter.prototype.clear = function() {
_Game_Interpreter_clear.call(this);
this._myVar = null;
};

};


// code in this section will always be run
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
Game_Interpreter.prototype.pluginCommand = function(command, args) {
_Game_Interpreter_pluginCommand.call(this, command, args);

// do my stuff here
};
})();


What I'm after is a section of code that would only be run when in test mode, and another section that would be run at all times.  So this._myVar would only be created and set to null when play testing (and would only ever be used in that Utils.isOptionValid('test') block.


Just doing some testing with this in an event:

Code:
Script: if (this._myVar === undefined) {
          $gameVariables.setValue(1, "undefined");
        } else if (this._myVar === null) {
          $gameVariables.setValue(1, "null");
        }
Text: None, Window, Bottom
      \v[1]

it shows "null" in the text box whether I test play or play from a deployed exe.


So maybe not?
Yup, when not in test mode, your version of the clear method simply won't exist. 
why does my event always set the variable to 'null' then and not 'undefined'?  I'd expect, if my version of the clear method doesn't get run, this._myVar would be undefined rather than null?
Are you sure you're not setting null to that variable anywhere else? And that your deployed version doesn't have test mode activated?


(If you can open the console in the deployed version, it has the test mode enabled)
Yep.  That line was the only place where the variable was used.  I'd only just started building the script, and that's the first thing I wanted to test.  What you see there is the entire plugin I was testing at the time - I didn't even have anything in the plugin function.


I couldn't access the console in the deployed version, which is why I had to resort to using a script call to check the value and showing the results in a text command (couldn't use console.log because I couldn't bring up the console).

Original thread: https://forums.rpgmakerweb.com/threads/how-to-monitor-for-missing-database-file.72347/