MZ Help Understanding how to save custom data in rmmz

Hello all, I've been faced with a dilemma as of late, and have tried many methods in an attempt to get this to work.
1. I've tried creating a custom json file to handle the data its seems to work but there are many things I don't understand so this method seems to bring me to an impasse.
2. I've also attempted modifying existing saved variables to store the data needed in simplified forms. I've been met with the same problem as the first attempt.

lastly after a little while of searching I found this made by @Poryg and modified albeit slightly then tried implementing it.


var old_CGO = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
old_CGO.call(this);

$myObject_SaveInfo = new My_Object();

};

var old_MSC = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
var contents = old_MSC.call(this);

contents.myObject_SaveInfo = $myObject_SaveInfo;
return contents;

};

var old_ESC = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents){
old_ESC.call(this, contents);

if (contents.myObject_SaveInfo) {$myObject_SaveInfo = contents.myObject_SaveInfo;}

};

needless to say it still isn't working I've found out that the code needs an additional part maybe even two, as I understand it the function/methods needed are already available if only I knew where to look I could copy and modify them.

conclusion. I lack the knowledge necessary to make the modifications and as there are no documentation assistance is necessary.

any info on how to save data int, Boolean, string, arrays multidimensional arrays, objects even if only an int I can work with that. Many thanks in advance.
I had similar problem, but in MV, hopefully this will help you

Hi, there friend!

The easy way is that you just save your data in a game object that is already on the save file.
Ex:

Code:
Game_System.prototype.yourData = function(){
         this._myData = // whathever you want.
}
Then you can call it with $gameSystem._myData
The other way around, is that you create your data just like MZ does:

Code:
// Create your content:
function Eli_SavedContents() {
    this.initialize.apply(this, arguments);
};
Eli_SavedContents.prototype.initialize = function(){
    this.contents = {};
};

// Put it into the save file
const _DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _DataManager_createGameObjects.call(this);
    $eliData = new Eli_SavedContents();
};
const _DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
    const alias = _DataManager_makeSaveContents.call(this);
    alias.eli = $eliData;
    return alias;
};
const _DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
    _DataManager_extractSaveContents.call(this, contents);
    $eliData = contents.eli;
};
I had similar problem, but in MV, hopefully this will help you

thank you ill review the document.
Hi, there friend!

The easy way is that you just save your data in a game object that is already on the save file.
Ex:

Code:
Game_System.prototype.yourData = function(){
         this._myData = // whathever you want.
}
Then you can call it with $gameSystem._myData
The other way around, is that you create your data just like MZ does:

Code:
// Create your content:
function Eli_SavedContents() {
    this.initialize.apply(this, arguments);
};
Eli_SavedContents.prototype.initialize = function(){
    this.contents = {};
};

// Put it into the save file
const _DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _DataManager_createGameObjects.call(this);
    $eliData = new Eli_SavedContents();
};
const _DataManager_makeSaveContents = DataManager.makeSaveContents;
DataManager.makeSaveContents = function() {
    const alias = _DataManager_makeSaveContents.call(this);
    alias.eli = $eliData;
    return alias;
};
const _DataManager_extractSaveContents = DataManager.extractSaveContents;
DataManager.extractSaveContents = function(contents) {
    _DataManager_extractSaveContents.call(this, contents);
    $eliData = contents.eli;
};
This seems efficient i think ill take advantage of this info. thank you for your assistance. ill contact you with results.
Hi, there friend!

The easy way is that you just save your data in a game object that is already on the save file.
Ex:

Code:
Game_System.prototype.yourData = function(){
         this._myData = // whathever you want.
}
Then you can call it with $gameSystem._myData
Hello, I am new to RPG Maker but I have some coding experience. Do you have to explicitly call this function or is it automatically called? And when you continue or load a save is the data automatically returned to memory?
Hello, I am new to RPG Maker but I have some coding experience. Do you have to explicitly call this function or is it automatically called?
It depends which you mean...there were a bunch of functions up there 😛

This syntax:
Code:
const _DataManager_createGameObjects = DataManager.createGameObjects;
DataManager.createGameObjects = function() {
    _DataManager_createGameObjects.call(this);
says you're overwriting a function that's a part of the default engine - DataManager.createGameObjects() - calling its original contents, then adding more.

So whatever you write there will be executed whenever that original function is called.

Note that something like this is only necessary if you have some kind of complex data structures. For anything basic, you can simply add a property to $gameSystem and it will automatically be saved by the engine's default processes.

For example, you could just in any script event command anyplace say $gameSystem.newThingy = 5 and you can check that after saving and loading a game and it'll still be there with a value of 5.
It depends which you mean...there were a bunch of functions up there 😛

I am asking only about the "easy way", i.e. attaching an implicit class to a game object which is already saved.

Code:

Game_System.prototype.yourData = function(){
this._myData = // whathever you want.
}
I am asking only about the "easy way", i.e. attaching an implicit class to a game object which is already saved.
I already addressed that in my previous reply. You don't need a setter function like that, you can simply declare it the way I showed you.
It seems that what happened was I quoted (in my first post) just a part of Eliaquim's post but because it was collapsed it isn't readily apparent. Thus you replied without being aware that I was in fact not asking about the other part.

Now that I have thought about it a bit I realize that the question I originally asked doesn't really make sense. What is saved in a savegame can only be an instance of a data structure thus it couldn't exist to be saved if the function was not called at least once.

Thank you for your help I think I better understand what is going on now.
What is saved in a savegame can only be an instance of a data structure thus it couldn't exist to be saved if the function was not called at least once.
Just to triple clarify, I think that you're correct in that you can't save something you haven't created, but there doesn't need to be any external function involved.
So my idea is this. I'm writing a little quest system and want to save a table of active quests. I've created two classes -- a quest class and an objective class. The objective class is used in an array internal to the quest class to store the quest completion data. There is other data in the quest class as well like rewards, whether quest is repeatable, and static methods.

Edit: I've removed the code I put here for two reasons: 1. It doesn't work and I don't want to put bad code out there that causes trouble for others, and 2. According to the resources Soulrender linked, the map won't be saved correctly. So I've found the answer to my own question.

Original thread: https://forums.rpgmakerweb.com/threads/help-understanding-how-to-save-custom-data-in-rmmz.132600/