MV Include in save file custom variables from custom class

I have own, custom class wich is initialized the same way as default classes in MV:
Code:
var $myClass= null;

myClass.prototype = Object.create(myClass.prototype);
myClass.prototype.constructor = myClass;

function myClass(){
    this.initialize.apply(this, arguments);
}

myClass.prototype.initialize = function() {
    this._test = 0;
};
(...)

How do I include _test variable in save file?
Three relevant methods (cf rpg_managers.js):
  • Initialise - DataManager.createGameObjects - on new game
  • Make save data - DataManager.makeSaveContents - on save
  • Read save data - DataManager.extractSaveContents - on load
I'd suggest aliasing each of these, e.g.
Spoiler: Code example
JavaScript:
(function(alias) {
  DataManager.createGameObjects = function() {
    alias.apply(this, arguments);
    $myClass = new myClass();
  };
})(DataManager.createGameObjects);

(function(alias) {
  DataManager.makeSaveContents = function() {
    var contents = alias.apply(this, arguments);
    contents.mySaveProperty = $myClass;
    return contents;
  };
})(DataManager.makeSaveContents);

(function(alias) {
  DataManager.extractSaveContents = function(contents) {
    alias.apply(this, arguments);
    var data = contents.mySaveProperty;
    if (data) $myClass = data;
  };
})(DataManager.extractSaveContents);
If you don't want to save the entire object then you can build your own data structure as needed. Also watch out for naming conflicts, as usual: the contents variable seen here is the data for the entire save file.

Since the save data is recursively passed through JSON stringify/parse, primitive values and standard objects are OK; functions, maps, etc will be dropped. Probably not relevant but I thought I'd mention it~

Edit: if you intend this to be a single instance (?) another option could be to store custom save data on an existing object that gets saved, e.g. $gameSystem. It would increase the chance of naming conflicts, but you wouldn't have to bother with writing custom code to handle new/save/load for those values.
The super lazy way is to just assign it to a game variable. $gameVariables.setValue(13, $myClass);

This sets the 13th game variable to your global object. If you wanna be even less safe you can use $gameVariable._data[13] = $myClass.

The game already saves $gameVariables. While normally you'd only consider putting numbers in there, there is nothing stopping you from storing anything else you want in there. The game only ever references individual indexes from $gameVariables so it won't slow anything down considerably (it might if it were to iterate through the entire $gameVariables._data array but it never does that).

If your code uses $myClass directly you will need to reset it upon load with a $myClass = $gameVariables.value(13); but you could always just use the game variable to store it right from the start.

Actually doing what the first reply says is a more professional way of handling it.
I'll first refer to you, @RinTheWanderer actually something similar came across my mind, but I don't want to use to much $gameVariables, MV editor of $gameVariables drives me nuts, so I prefer to store variables in own JS objects (better readability and clarity and also easy to find things to adjust in further development. So I appreciate your idea and you have a beer from me 🙂

@caethyril I'm full of admiration and respect for your JS knowledge. I thought I could overcome all obstacles that MV engine drawns for me, but... I was wrong., but most importantly thanks to your advices I'm learning how to overcome things in JS and... Well... There is no single word that can show appretation for you, so there is only one thing I can say. "Thank You".
Yeah I totally understand and I'd probably do it the way caethryil said as well, just wanted to throw out the lazy alternative.

And yeah, with clever use of console.log and just looking at the default js, it's surprisingly easy to figure out just about anything the game does. I still remember trying to solve this same problem back in vx ace when I didn't know what I was doing and making an event to puts the unecrypted save data so I could see how the hell it was stored. RPG Maker and me have come so far since then.
So, to finally close this topic only one question bothers me. What's meaning (purpose) of aliasing functions? I deduct it's for executing normal function (mother) and kinda to attach another methods for it (children) and if I wouldn't alias them "mother's" methods would be overwritten by "child's" method, correct?
alias a function (so far I know it), is to get the data what the function already have,
and add aditional functions to it (instead of overwrite the full function).

because existing function that you overwrite can break many plugins (unless you
know what you do) what most dev's do in their own project).

Aliasing is always the safest way, unless those a new and non-existing functions "yet" =)
You can alias a function as many times as you want without creating any conflicts. If you simply overwrite the function only the last version of it will go through.

Like if you have a function called blah, aliasing would be like this.

blah = old blah + new stuff.

So whatever blah was before that line, you take that and you add the new stuff to either the beginning or end and then call the whole thing blah.

If you aren't using plugins and thus aren't overwriting a function more than once, you don't NEED to alias, but it's still a good habit since it's easy to do.
Pretty much, yes. To reiterate, the structure of an alias (a.k.a. "monkey patch" in this context) is:
  1. Store original method;
  2. Replace that method with something that calls the original.
It is basically a non-destructive override, the only difference is that you have retained a reference to the original process that can be called before, after, or in the middle of your override. As mentioned, it's a much better option if you want stuff to remain compatible with unknown code from other authors. 🙂
So in short aliasing function:
Code:
myClass.prototype.doStuff = function(){
a.beer();
a.chips();
a.movie();
}

Code:
(function(alias){
myClass.prototype.doStuff = function(){
alias.apply(this, arguments);
a.girls();
a.turnOffPhone();
}
})(myClass.prototype.doStuff());
will tell interpterer to do:
Code:
a.beer();
a.chips();
a.movie();
a.girls();
a.turnOffPhone();
and without aliasing doStuff() will run only when it will be applied as plugin:
Code:
a.girls();
a.turnOffPhone();
Correct! 🙂
@caethyril just one more question, how it would like if I wish to add more than 1 items to save? (Without aliasing function inside of each JS file with custom class)

Nevermin, already found out - Sorry.

Original thread: https://forums.rpgmakerweb.com/threads/include-in-save-file-custom-variables-from-custom-class.129477/