From 5643fd86e2e5797da6999b16b45c122482a2b0a9 Mon Sep 17 00:00:00 2001 From: HyperOnion <> Date: Sat, 24 Jun 2023 18:10:58 +0200 Subject: [PATCH] fix world save & load bug. previously, serialization of world data couldn't handle boxes, since they contain references to their parents, meaning worlds are cyclic. this commit fixes this, and changes world loading to correctly re-assign parents to boxes after deserializing them. this bug is what happens when you commit without testing for more than a few seconds. --- js/world.mjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/js/world.mjs b/js/world.mjs index 3532557..36e714e 100644 --- a/js/world.mjs +++ b/js/world.mjs @@ -63,18 +63,21 @@ export function to_json () { } function reconstruct_parent (world, parent) { + world.parent = parent; for_each_tile(world, (x, y, tile) => { if (tile.type !== "box") return; - tile.parent = parent; reconstruct_parent(tile.box, world); }); } export function from_json (json) { const result = JSON.parse(json); + for_each_tile(result, (x, y, tile) => { - if (tile.type !== "box") return; - reconstruct_parent(tile.box, result); + if (tile.type === "box") { + reconstruct_parent(tile.box, result); + } }); + return result; }