[Workaround] Object too deep?

Default RM Code from their core script:

Spoiler
JsonEx._encode = function(value, depth) { depth = depth || 0; if (++depth >= this.maxDepth) { throw new Error('Object too deep'); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< crash } var type = Object.prototype.toString.call(value); if (type === '[object Object]' || type === '[object Array]') { var constructorName = this._getConstructorName(value); if (constructorName !== 'Object' && constructorName !== 'Array') { value['@'] = constructorName; } for (var key in value) { if (value.hasOwnProperty(key)) { value[key] = this._encode(value[key], depth + 1); } } } depth--; return value;};
So I added a new object to the baseItems and then added a reference back to the baseItem in that. And voila, object too deep. Even without a direct reference but with a function:

Item_SG.prototype.item = function(){ return this.db[this.item_id]; // this.db = $dataArmors and it does not return a null value, this specific function actually works.}Same problem. Object too Deep crash.

Why does RPG Maker MV not allow me to have a reference back to the parent object of items in the database? Why is this explicitly forbidden?
It's not explicitly forbidden, it's just the bad design of the save function.


It's the same issue that would happen if you save your game while a parallel process is running: RM simply tries to save the whole objects without looking at what's being saved and ends up with a circular reference. Yours is just one of the problems that can happen because of that.
Oh... I see. It iterates it perhaps. FML.... Because I'm working with the actual database that is not even saved/loaded anyway. The RM-code once more slaps me in the face with its (in my opinion) sub-optimal-design with some kind of silly workaround for another piece of poor code that for some reason also triggers when it's not even saved nor loaded to the disk... So basically the workaround for their 'poor code' is gonna need a workaround...

Or did I understand this wrong?

It's even worse when I don't even store a reference back but make it happen indirectly though that function by splitting the database and the id so there is not even any circular reference at all... But even that is not allowed.

I swear, making a simple script in MV takes 10x longer than in most engines/languages. And it makes me pretty pissed off at times. At least it's better than Ace in terms of how fast plugins can be made.

Anyway, I solved it by working around it. I applied a workaround to the workaround to their 'problem'. But it works.
Or did I understand this wrong?


It's even worse when I don't even store a reference back but make it happen indirectly though that function by splitting the database and the id so there is not even any circular reference at all... But even that is not allowed.
If you only keep an id and then load it from the databse through a function when needed, it should have worked.
If you only keep an id and then load it from the databse through a function when needed, it should have worked.
I kept a reference to the id and to the database ($dataArmor, $dataItems, $dataWeapons) and also tried it as a string value: 'dataItems'. But it seems that RPG Maker simply tests the function Item_SG.prototype.item as a whole and then see's the circular reference and still throws the error... It's really awful...

Out-commenting the function made it work again so it really did not like the function even.

Original thread: https://forums.rpgmakerweb.com/threads/workaround-object-too-deep.53036/