Game data can be one of two types:
- Static
- Should not change mid-playthrough.
- Initialised once per session, on game boot (e.g.
Scene_Boot#start).
- Not included in save files.
Core script example: $dataActors (database actor data).
- Dynamic
- May change mid-playthrough.
- Initialised on new/load game (and game boot).
- May be included in save files. For example, the core scripts do not save
$gameTemp, $gameMessage, or $gameTroop.
Core script example: $gameActors (current actor data).
To include (dynamic) data in save files, there are 3 relevant stages to consider:
- Initialisation: hook into
DataManager.createGameObjects.
This method runs on new/load game, and on game boot (after the database has loaded).
- Add to save data: hook into
DataManager.makeSaveContents.
This method returns an object to be stringified and saved to file.
- Load from saved data: hook into
DataManager.extractSaveContents.
This method accepts one argument: an object, the un-stringified save data.
It applies after initialising the data through createGameObjects.
Template example (ES5 syntax):
Spoiler: Click to view example
JavaScript:
(function(alias) {
DataManager.createGameObjects = function() {
alias.apply(this, arguments);
console.log("initialise data");
// window.myData = { a: 0, b: 0 }; // example
};
})(DataManager.createGameObjects);
(function(alias) {
DataManager.makeSaveContents = function() {
var contents = alias.apply(this, arguments);
console.log("add save data");
// contents.myData = window.myData; // example
return contents;
};
})(DataManager.makeSaveContents);
(function(alias) {
DataManager.extractSaveContents = function(contents) {
alias.apply(this, arguments);
console.log("extract save data");
// if (contents.myData) window.myData = contents.myData; // example
};
})(DataManager.extractSaveContents);
(I'm not sure what the corresponding
class sugar is for this.)
Alternatively, you can simply attach your data to an object that is already included in save files, e.g.
$gameSystem. This is a bit more risky in terms of compatibility with other plugins, but much simpler to do!
😊
Also, some notes on the save format (stringification):
- Avoid saving irrelevant data:
JsonEx.stringify(new Array(10000)) yields a ~50 KB string, because all the empty entries get logged as null.
- Certain data, e.g.
Function objects or circular references, will be discarded unless you write some code to handle those cases. (Later versions of MV have a safeguard against circular references which has been dropped in MZ.) More details here:
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
developer.mozilla.org
- Under current specifications, it looks like strings have maximum length ~1 GB, which shouldn't be a problem in this situation:
The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances.
developer.mozilla.org
MZ zips its save data after stringification, so the final save file size may be smaller in that case.
I think this all applies to MV as well, except where explicitly noted.
👋