Reliably holding ECMAScript 6 class instances in memory
-
I ran into an issue that seemed like a bug when trying to store an instance of a class in Memory. Here's some minimal code to reproduce it:
main.js:
class Doggo {
bark() {console.log('woof')}
}module.exports.loop = function () {
if (Game.time % 5 == 0) {
Memory.pooch = new Doggo();
console.log("Single pooch instanceof Doggo (same tick as created): " + (Memory.pooch instanceof Doggo));
Memory.pooch.bark();
}
else {
console.log("Single pooch instanceof Doggo: " + (Memory.pooch instanceof Doggo));
Memory.pooch.bark();
}}
And sample output:
-
Anything that is put into memory is run through json stringify and then back through json parse.
Instead of saving classes in memory you need to save the data to initialize them.