My game uses lots of complex objects to store game data. (Each block and entity has its own instance of a class)
I am trying to create a saving and loading system, where the user can save game progress to a text file.
But a simple JSON.stringify won't cut it. These objects are very complex and nested, with instances of different classes contained within. (For example, one instance of the Block class may contain multiple instances of the Text class)
I need a way to boil all this meta data down into text. I've tried multiple libraries, but those don't work with my custom classes.
Any advice on this would be super appreciated!
EDIT: An attempt at writing my own method for doing this
function superString(obj) {
let result = "";
for(var property in obj) {
if(Array.isArray(property)) {
result += superString(property)
}
else if(property instanceof Block) {
console.log("EXPORTING BLOCK")
result += property.exportString();
}
else if(property instanceof Entity) {
result += property.exportString();
}
else if(typeof property === 'object' && property !== null) {
result += superString(property)
}
else result += JSON.stringify(property)
}return result;
}
blockType = "Sign"andsignText = "some text string"- your loading method can build the rest and populate the variables \$\endgroup\$