I'm creating a new class whose data I want to access by map id and event id, similar to Game_SelfSwitches. So I've copied a number of functions from that class and renamed them, and have created my own
value and
setValue functions. I've added DataManager commands to create a new object, update save file contents, and retrieve save file contents, to include this new class.
function Game_SelfSwitches() {
this.initialize.apply(this, arguments);
}
Game_SelfSwitches.prototype.initialize = function() {
this.clear();
};
Game_SelfSwitches.prototype.clear = function() {
this._data = {};
};
// ---------------------------------------------
function Game_EventLocations() {
this.initialize.apply(this, arguments);
}
Game_EventLocations.prototype.initialize = function() {
this.clear();
}
Game_EventLocations.prototype.clear = function() {
this._data = {};
}
This is how my new class looks, compared with $gameSelfSwitches, when I start a new game:
Code:
$gameSelfSwitches
Game_SelfSwitches {_data: Object, initialize: function, clear: function, value: function, setValue: function…}
_data: Object
__proto__: Game_SelfSwitches
$gameEventLocations
Game_EventLocations {_data: Object, initialize: function, clear: function, value: function, setValue: function}
_data: Object
__proto__: Game_EventLocations
However, when I save a game and load it, I've lost the prototype and all the functions associated with the class (the data is still there and looks correct), while $gameSelfSwitches looks as it should:
Code:
$gameSelfSwitches
Object {_data: Object, @: "Game_SelfSwitches", initialize: function, clear: function, value: function…}
@: "Game_SelfSwitches"
_data: Object
__proto__: Game_SelfSwitches
$gameEventLocations
Object {_data: Object, @: "Game_EventLocations"}
@: "Game_EventLocations"
_data: Object
__proto__: Object
Have I done something wrong with the save/load process?
var _Shaz_REP_DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
_Shaz_REP_DataManager_createGameObjects.call(this);
$gameEventLocations = new Game_EventLocations();
};
var _Shaz_REP_DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
var contents = _Shaz_REP_DataManager_makeSaveContents.call(this);
contents.eventLocations = $gameEventLocations;
return contents;
};
var _Shaz_REP_DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
_Shaz_REP_DataManager_extractSaveContents.call(this, contents);
$gameEventLocations = contents.eventLocations;
};
Edit: I think I've solved it ... the only difference between the two was that my definitions were inside the (
function() {...}); block of my plugin, and Game_SelfSwitches was defined at the higher level. When I moved my Game_EventLocations functions above that block, so they were at the same level, it worked - the save file has the correct prototype.
I don't understand the logic behind this. Could someone please explain it to me?