0

I was wondering how if it was possible save my engine.world Object as an exportable format. I tried converting it as a JSON string js JSON.stringify(engine.world) as it contains a circular structure. Are there any ways to overcome this problem, or any other formats I could convert it to?

I want users to be able to be able to load custom worlds.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • there are solutions for that ... a search for circular JSON reveals many ... – Jaromanda X Jul 05 '22 at 07:10
  • Does this answer your question? [How can I print a circular structure in a JSON-like format?](https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format) – evolutionxbox Jul 05 '22 at 08:29
  • Possibly an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/233676#233676). Even if you do save it, how are you planning on reconstituting the MJS state? There's potentailly a better way to achieve custom worlds. – ggorlen Jul 05 '22 at 20:07

1 Answers1

0

What you are looking for is resurrect-js

You can stringify the data using:

var necromancer = new Resurrect();
var data = necromancer.stringify(engine.world);

You can then save data as a string in whatever text file you want.

To turn the string back into the object, you can use:

var world = necromancer.resurrect(data);
engine.world = world;

Hope this helps!

CY83
  • 85
  • 7