MV [Solved maybe] How do I define custom objects in save files?

Data is serialized in JSON when it's saved.

When you save the $gameActors, and then you load them from the save file, they are still Game_Actor objects.

However, for my custom objects such as grid data, etc, they just get turned into Object and lose all the functions and stuff.

So it crashes when it tries to access it like a specific instance of a class, because it lost all that information when it was serialized.

Example: Game_Map has an array of Game_Vehicle objects stored as $gameMap._vehicles. When the map is saved and loaded, they're still Game_Vehicle objects.

1598495469240.png


Whereas for me, my objects get wiped out

1598495595094.png
Your object class must have a name.
Don't know if I really get it.
But if you want to store custom data in the save file, you can't create your classes normally with es6 syntax.
You have to do the way it is in the core files:

JavaScript:
function Game_Actor() {
    this.initialize(...arguments);
}
Game_Actor.prototype = Object.create(Game_Battler.prototype);
Game_Actor.prototype.constructor = Game_Actor;

I don't know, maybe there is a way to use es6 class and save it.
But I think the real problem is in the JsonEx class.

I have managed to do it, but with a lot of turn around that make my code ugly and not very readable.

I mean, assuming that I understand what you are talking about ^^'''
Your object class must have a name.

What does it mean to give an object class a name lol

Don't know if I really get it.
But if you want to store custom data in the save file, you can't create your classes normally with es6 syntax.
You have to do the way it is in the core files:

JavaScript:
function Game_Actor() {
    this.initialize(...arguments);
}
Game_Actor.prototype = Object.create(Game_Battler.prototype);
Game_Actor.prototype.constructor = Game_Actor;

I don't know, maybe there is a way to use es6 class and save it.
But I think the real problem is in the JsonEx class.

I have managed to do it, but with a lot of turn around that make my code ugly and not very readable.

I mean, assuming that I understand what you are talking about ^^'''

Hmm, I did use the class syntax.
I was planning to rewrite it using the prototype way due to the aliasing issues that were brought up before. Maybe it's a good time to rewrite...
What does it mean to give an object class a name lol

I assumed you were still using prototype, then just having the class definition with a function name will work:
Code:
function MyClassName() {
}

For ES2015, they intentionally prevented people from doing that with the new class syntax, so you need to instantiate a new object yourself and then assign the data to it with Object.assign.

I would recommend using Object.assign even if you're going to use ES5 as its a safer method.
Alright, I ended up just converting all of the class syntax into prototype syntax lol.
It was nice while it lasted.

Original thread: https://forums.rpgmakerweb.com/threads/solved-maybe-how-do-i-define-custom-objects-in-save-files.126201/