Skip to content

Commit

Permalink
fix: restore choice tags on state reloading
Browse files Browse the repository at this point in the history
Choice tags were missing in serialisation and deserialisation of state to/from JSON.

Fixes y-lohse#1022
  • Loading branch information
Shepard committed Aug 5, 2023
1 parent e17f118 commit 676de20
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/engine/JsonSerialisation.ts
Expand Up @@ -580,6 +580,9 @@ export class JsonSerialisation {
choice.sourcePath = jObj["originalChoicePath"].toString();
choice.originalThreadIndex = parseInt(jObj["originalThreadIndex"]);
choice.pathStringOnChoice = jObj["targetPath"].toString();
if (jObj["tags"]) {
choice.tags = jObj["tags"];
}
return choice;
}

Expand All @@ -590,6 +593,17 @@ export class JsonSerialisation {
writer.WriteProperty("originalChoicePath", choice.sourcePath);
writer.WriteIntProperty("originalThreadIndex", choice.originalThreadIndex);
writer.WriteProperty("targetPath", choice.pathStringOnChoice);
if (choice.tags) {
writer.WriteProperty("tags", (w) => {
w.WriteArrayStart();
for (const tag of choice.tags!) {
w.WriteStringStart();
w.WriteStringInner(tag);
w.WriteStringEnd();
}
w.WriteArrayEnd();
});
}
writer.WriteObjectEnd();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/inkfiles/compiled/inkjs/tests.ink.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/tests/inkfiles/original/inkjs/tests.ink
Expand Up @@ -489,7 +489,7 @@ a bit of content
the next bit
-> DONE
= choicepoint
* choice 1
* choice 1 # a tag
* choice 2
- -> DONE

Expand Down
16 changes: 16 additions & 0 deletions src/tests/specs/inkjs/engine/Integration.spec.ts
Expand Up @@ -275,6 +275,22 @@ describe("Integration", () => {
expect(context.story.currentChoices[0].text).toEqual("choice 1");
expect(context.story.currentChoices[1].text).toEqual("choice 2");
});

it("should restore choice tags", () => {
context.story.ChoosePathString("saveload.choicepoint");
context.story.Continue();
expect(context.story.currentChoices.length).toEqual(2);
expect(context.story.currentChoices[0].tags.length).toEqual(1);
expect(context.story.currentChoices[0].tags[0]).toEqual("a tag");
console.log(context.story.currentChoices[0].tags);

const save = context.story.state.ToJson();
context.story.state.LoadJson(save);

expect(context.story.currentChoices.length).toEqual(2);
expect(context.story.currentChoices[0].tags.length).toEqual(1);
expect(context.story.currentChoices[0].tags[0]).toEqual("a tag");
});
});

describe("debug tools", () => {
Expand Down

0 comments on commit 676de20

Please sign in to comment.