Twine Version: 2 / twee version: 3
SugarCube Version: 2.37.3
Hi everybody,
after much searching and tinkering I finally managed to use my own JavaScript classes to properly serialize/deserialize between passages etc. by using the pattern described here: Non generic object types. I used the second approach (Discrete parameters constructor), so a “simple” class (meaning without containing other classes) looks for example like this:
class Monster {
constructor(isBoss) {
this.bossFlag = isBoss;
}
isBoss() {
return this.bossFlag;
}
clone() {
return new this.constructor(this.bossFlag);
}
toJSON() {
return Serial.createReviver(
String.format(‘new {0}({1})’,
this.constructor.name,
JSON.stringify(this.bossFlag)));
}
}
window.Monster = Monster;
Now initializing objects with new Monster(false) and saving/loading as well as using the ‘reload page’ browser button too.
BUT!
This only works for “simple” classes with no nesting, like the one above. But I also want to use “complex” classes with a lot of containment and here the deserialization breaks. I get the “patched is undefined” error, like this:
Error: can’t access property “junctionEast”, patched is undefined
By “complex” I mean e.g. my dungeon contains several floors, each one with several rooms, each one possibly containing monsters, loot, etc.
I have written clone() and toJSON() methods for all my classes like in the example above. I have hoped that the serialization/deserialization engine would automatically recognize nested objects and handle them recursively correctly. But apparently this isn’t enough.
I have been debugging the code now for two days and can’t quite figure out how to proceed.
Has anyone of you encountered a similar problem and found a solution?
Cheers,
Pesha.