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:
Then I create an object like:
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
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:
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...
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...