JavaScript Visual Novel Engine: Using Flags

Some visual novels have paths that diverge and then rejoin. You need to be able to keep track of which paths have been taken in order to come to the appropriate ending. (Click the picture to see it at full size).

flowchart of novel that diverges and converges three times

In the summary, you want four possible endings:

To accomplish this, you set a flag, a variable that keeps track of the number of times a path was chosen, for each path.

At the beginning of the novel, set three user variables to zero:

setVars, {flagA: 0, flagB: 0, flagC: 0}

And then, every time you get to a choice point, you add one to the appropriate variable. For example, if the reader chooses path A, you do this:

setVars, "novel.userVar.flagA = novel.userVar.flagA + 1"

Then, in the summary, you can use a series of ifStatements to figure out what to do. You can see a complete example of flags here.