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

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,851
First Language
English
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
 
Last edited:

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,564
Reaction score
3,758
First Language
Java's Crypt
Primarily Uses
RMMZ
Your object class must have a name.
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,777
Reaction score
1,200
First Language
Portuguese - Br
Primarily Uses
RMMZ
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 ^^'''
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,851
First Language
English
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...
 

Hudell

Dog Lord
Veteran
Joined
Oct 2, 2014
Messages
3,564
Reaction score
3,758
First Language
Java's Crypt
Primarily Uses
RMMZ
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.
 

Latest Threads

Latest Posts

Latest Profile Posts

2021? I still wonder if people still doing MV than MZ more
Seeing a lot more active Ace topics recently, is it making a comeback? :o
0-7, left-to-right, top-to-bottom
As soon as I'm able to get out of bed I'm probably gonna go so hyper from being too cooped up, and burn myself out into another awful flare. lol
It makes me feel proud whenever an RM game does well. Even though I had nothing to do with it.

Forum statistics

Threads
107,627
Messages
1,031,011
Members
139,746
Latest member
Krunga
Top