MV Error when saving an object created from a class

So I can't get this. I've tried following the string back to the core, but I simply can't get what's wrong with it.

So I create a class like this:

function ClassName() { this.initialize.apply(this, arguments); } ClassName.prototype.initialize = function (name) { codeHere; }

Then I create an object like:

var exampleObject = new ClassName();

The exampleObject works perfectly. But when I try to save the game it throws me this error:
Cannot read property 'name' of undefined
at Function.JsonEx._getConstructorName

I followed this error into the core js file. I saw it takes a value, then it gets value.constructor.name.

So then I thought, I should see what exactly causes the problem by console.logging it. So I add
if(!value.constructor || !value.constructor.name) console.log(value)

And lo and behold, it logs all ClassName instances. So then I go back to my class, mimicking how other classes are made I also include:

function ClassName() { this.initialize.apply(this, arguments); } ClassName.prototype.constructor = ClassName; ClassName.prototype.initialize = function (name) { codeHere; }

And yet this doesn't matter at all. Then I went manually to my object and logged it's constructor.name and it's correctly logged! So I have simply no idea why it says it's undefined.

By the way, when logging, it also logged an Events instance, which was empty. However, removing all instances of ClassName before saving solved the problem. Well, my problem is that ClassName objects are used to store valuable information.

Sure, I could probably store that information without classes, by making other objects with ids, and just copying the keys and their values. But that's not the point, since it's an error I don't understand and it messes with other stuff as well (for example, an instance of PIXI.Graphics, that shows the same error - but since I don't need to save that I just don't save it)

Please help, it's doing my head in for a long time now...

EDIT: I have seen that JSON has a problem with stringifying class instances in general. However $gameScreen *is* a class instance and it does get saved by default. And looking at the Game_Screen code in objects.js it's not written any differently than mine - it doesn't have any extra parts I mean.

EDIT2: And I completely forgot I have actually already been saving a class object, and it had no trouble with it. It's just that this class object has 3 properties that are instances of ClassName in my previous example. It's those instances that saving has trouble with.

EDIT3: I solved the problem by serializing that class object when the player saves the game, and deserializing it when the player loads the game. I mean, the class object itself isn't saved, just its important changeable values. But to me it doesn't seem that much of a solution, more like a temporary one...
It sounds like the function you're trying to use as constructor may not have been available in general scope and thus wasn't found?

The way RPGMV/Z saves things is that it takes the objects and stores their values, while adding the original constructor function's name as a value registered to the at symbol.
1665960854048.png


Upon loading the game, the game checks every object it has to restore for this specific key and if the key is present, it will use this value as window property to get the constructor function.
e.g.
window["ANY_DialogueCharacterDatabase"]

However, if the function in question isn't available in general scope, but is scoped to an object, e.g.
ANY.DialogueCharacterDatabase, it won't be able to find "DialogueCharacterDatabase" in global scope. As a result, no constructor exists with which to create the function, which creates an error.

I'd have to see the details of your function to say more, but this would be my first suspicion.
It sounds like the function you're trying to use as constructor may not have been available in general scope and thus wasn't found?

The way RPGMV/Z saves things is that it takes the objects and stores their values, while adding the original constructor function's name as a value registered to the at symbol.
View attachment 242873

Upon loading the game, the game checks every object it has to restore for this specific key and if the key is present, it will use this value as window property to get the constructor function.
e.g.
window["ANY_DialogueCharacterDatabase"]

However, if the function in question isn't available in general scope, but is scoped to an object, e.g.
ANY.DialogueCharacterDatabase, it won't be able to find "DialogueCharacterDatabase" in global scope. As a result, no constructor exists with which to create the function, which creates an error.

I'd have to see the details of your function to say more, but this would be my first suspicion.

Hmm, this makes sense, as I make an object, let's say Obj = {} and then make properties from a class, let's say Obj.npc1 = new NPC() , Obj.npc2 = new NPC() etc. So I'm guessing it can't find the constructor because npc1 and npc2 are properties of the Obj, therefore their constructor isn't globally scoped?

EDIT: Is there a way then to help it find the constructor? Or must I do it like serializing it and de-serializing it like I mention above?
Hmm, this makes sense, as I make an object, let's say Obj = {} and then make properties from a class, let's say Obj.npc1 = new NPC() , Obj.npc2 = new NPC() etc. So I'm guessing it can't find the constructor because npc1 and npc2 are properties of the Obj, therefore their constructor isn't globally scoped?

EDIT: Is there a way then to help it find the constructor? Or must I do it like serializing it and de-serializing it like I mention above?
The problem isn't the object or its properties, it's the constructor's location and whether its a property of the window object. (The window object is the main object of the RPGM application window.)

You keep using the terminology "class" but your example earlier is one of a function, so I'm a bit confused which of those you're using.

If you mean actual ES6 classes, created with the class syntax, those are globally scoped, but they're not a window property of the window object and thus cannot be found with the way RPGMV/Z finds the constructor.

This can be remedied by making the class a window property.
Instead of
JavaScript:
class MyClass {
    constructor() {
    
    }
}

use:
JavaScript:
var MyClass = class {
    constructor() {

    }
}
This ensures the class is a property of the window.

So when RPGMV/Z searches for the class using window["MyClass"] it will find the variable that contains the class.

You can also directly create a class or function as property of a window,
JavaScript:
window.MyClass = class {
    constructor() {
        
    }
}

or assign it as property of a window in hindsight.
JavaScript:
class MyClass {
    constructor() {
        
    }
}
window.MyClass = MyClass;

Original thread: https://forums.rpgmakerweb.com/threads/error-when-saving-an-object-created-from-a-class.151820/