From f64679ca4319c126e450089dd04485fa6f0b2b7f Mon Sep 17 00:00:00 2001 From: transoptimal 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; }