mnbroatch / yarn-bound

Quality of life wrapper around bondage.js, a yarn language parser.
32 stars 10 forks source link

Lookahead causes statements to execute out of order #8

Closed benwest closed 2 months ago

benwest commented 1 year ago

First, fantastic library, it's working great in our game and a lot of fun! But...

Issue

When a command is followed by an if statement, the lookahead in advance evaluates the if statement first and the command second. So if the command should have affected the result of the if statement, it's too late.

Reproduction

https://codesandbox.io/s/gallant-currying-bi8e7b?file=/src/index.js

Script:

import YarnBound, { CommandResult } from "yarn-bound";

let condition = false;

const output = [];

const runner = new YarnBound({
  dialogue: `
title: Start
---
Jim: Hi there. I'll set the condition to true.
<<SetConditionToTrue>>
<<if conditionIsTrue()>>
Jim: It's true!
<<else>>
Jim: It's false.
<<endif>>
===
`,
  functions: {
    conditionIsTrue: () => {
      output.push("Checking the condition.");
      return condition;
    }
  }
});

const handleCommand = (result) => {
  if (result.command === "SetConditionToTrue") {
    output.push("Setting the condition.");
    condition = true;
  }
};

while (true) {
  if (runner.currentResult instanceof CommandResult) {
    handleCommand(runner.currentResult);
  }
  output.push(runner.currentResult.text);
  if (runner.currentResult.isDialogueEnd) break;
  runner.advance();
}

document.body.innerHTML = output.join("<br/>");

Output:

Hi there. I'll set the condition to true.
Checking the condition.
Setting the condition.
It's false.

Expected output:

Hi there. I'll set the condition to true.
Setting the condition.
Checking the condition.
It's true!
mnbroatch commented 1 year ago

Hey, sorry for taking so long to respond.

I've been working on a big holy grail rework of the lookahead feature that allows CommandResults to properly receive the isDialogueEnd flag but that doesn't prematurely handle commands or set variables.

It wound up being a lot more complicated than I expected! But, it would be great if you could try out the latest version and let me know if there are still kinks left to iron out.

mnbroatch commented 2 months ago

closing, assumed to be fixed