Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

What is the scope of variables created in passages vs. story javascript? Twine 2.1.0, Harlowe 1.2.3

Hello all,
I just started experimenting with Twine/Harlowe and noticed that a variable that is set in a particular passage seems to be forgotten after the player hits the 'back' arrow (Harlowe's CSS back arrow, not the browser back button). The execution flow is as follows:
(set: $bool_var to false) //this occurs in the start passage, and should be the default value
*user clicks link to a passage that presents them with a set of navigation choices, one of which will set $bool_var to true*
*user clicks link to the passage that sets $bool_var to true*
(set: $bool_var to true)
*user hits 'back' arrow*
*user clicks link to a different passage in which $bool_var is read*
(if: $bool_var is false)[bool_var is false](else:)[bool_var is true]
*user sees "bool_var is false"*

I tried adding a path from the passage where $bool_var is set true directly to the passage where it is read, and that resulted in the expected "bool_var is true" output.

I thought variables throughout Twine projects were global unless specifically attached to a passage, but this result suggests that hitting the back arrow somehow reverts the previous assignment. I guess that could be either a matter of scope or a function of the back arrow that I'm not familiar with. Anyway, what explains this behavior and how can I make my variables (and modifications to those variables) persist through user navigation using back/forward arrows?

Comments

  • I thought variables throughout Twine projects were global unless specifically attached to a passage, but this result suggests that hitting the back arrow somehow reverts the previous assignment.

    In Harlowe (and Sugarcube, for that matter) the variables that you set are not global. The back button in Harlowe is actually an "Undo" button, and it walks back the whole state one step, including changes to story variables.

    I'm not sure if there's an easy way to fix that, but I don't recommend making a bunch of global variables in the JavaScript. You could remove the navigation buttons and roll your own if you need the functionality of the back button but don't want to mess with the variables.

    Someone more familiar with Harlowe might have a better solution.
  • If you place your mouse cursor over that link you will see a tool-tip stating it is an 'Undo' link.

    Story variables are global from the point they are first defined but their current value is associated with the current passage being shown.

    Each time the story traverses from one passage to another the History system is updated with the name of the new/next passage as well as a 'copy'** of the current state of all known story variables. It is this copy which is made available to the new/next passage.

    The Undo link causes the History system to move backward in time to the previous passage, which results in the story variables being reset to the state they had before that previous passage was previously shown.

    If you want to move forward in time to the previous passage while keeping any variable changes done in the current passage then you can do something like the following.
    (link-goto: "Back", (history:)'s last)
    


    ** The method used to 'copy' the current state is different between Harlowe and SugarCube but the outcome is similar, I have deliberately left out technical details because I don't think they are relevant to this description.
Sign In or Register to comment.