Howdy, Stranger!

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

(noob question) Problem with variable and state/back button

I'm just starting on my first ever twine attempt using Twine 1.4, and trying to get to grips with how variables work.  In particular, I'm trying to understand how they change when the user chooses to use the back button in the story.

I can't seem to find the basic documentation on how history or state works but I gather that if a variable is set in one passage and then changed in a subsequent one, when a user goes back in their browser they would expect to see it go back to the prior value.  Is that correct?

My basic example has a $testvar:
set to 1 in my start passage (effectively passage1)
set to 2 in passage2
unchanged in passage3
and set to 3 in passage 4

I would expect that when I go start -> passage2 -> passage3 -> passage4 it ends up as 3, which it does.
I would also expect that when in passage4 I click the back button in my browser and go back to passage 3 it would go back to the value of 2.  This does not happen, it instead stays at a value of 3.

Do I need to manually manage my variable state somehow in script?

Apologies if this has been answered elsewhere, but I am having a really hard time finding clear info on how the history/state system works.

Comments

  • Which browser and story format are you using because it should work as you expected.

    I used the following to test, and it wound back the history when I used browser back button:
    (note: using TWEE notation, line starting with double colon (::) indicates a new passage with the related title.)

    :: StoryInit
    <<set $var to 0>>
    <<set $inc to 0>>

    :: Start
    <<print passage()>>
    var: <<set $var to 1>><<print $var>> (eg: 1)
    inc: <<set $inc += 1>><<print $inc>> (eg: 1)
    [[First Passage]]

    :: First Passage
    <<print passage()>>
    var: <<set $var to 2>><<print $var>> (eg: 2)
    inc: <<set $inc += 1>><<print $inc>> (eg: 2)
    [[Second Passage]]

    :: Second Passage
    <<print passage()>>
    <<print $var>> (eg: 2)
    <<print $inc>> (eg: 2)
    [[Third Passage]]

    :: Third Passage
    <<print passage()>>
    var: <<set $var to 3>><<print $var>> (eg: 3)
    inc: <<set $inc += 1>><<print $inc>> (eg: 3)
    <<back>> (winds back history)
    <<return>> (does not wind back history)
    Generally it is a better idea to give your reader links to navigate instead of relying on them using the browser back/forward buttons, you can do this by using the <<back>> (goes to previous passage and winds back history) and <<return>> (goes to previous passage and does NOT wind back history) macros.
  • I wasn't really aiming to use the back button navigation as part of the story flow, I just wanted to make sure I could use variables without having them break the logic if the user did decide to go back and try another path.  Looking at your example, could it be a case of me not initially defining them in StoryInit?

    I am using a fresh download of Twine 1.4.2 from twinery.org and running in current Chrome

    Okay, my test story goes:

    :: Start

    Your story will display this passage first. Edit it by double clicking it.

    <<set testvar = 1 >>

    You are currently set to <<print testvar>>

    [[Go to next passage|passage2]]


    :: passage2

    Not much going on here yet.

    <<set testvar = 2>>

    Except that you are now set to <<print testvar>>.

    And now we go on [[to|passage3]]


    :: passage3

    And still not much going on.

    You are still <<print testvar>>.

    Moving on [[again|passage4]].


    :: passage4

    Not much going on here yet.

    <<set testvar = 3>>

    Except that you are now set to <<print testvar>>.
  • Your problem is that you're not using state $variables.  Variables in Twine story formats must start with a dollar-sign ('$'; e.g. $testvar).

    What you're doing in your example is creating auto-globals, since the vanilla story formats still allow that sort of thing.  Those globals aren't part of the story history, and thus aren't affected when you roll the history back.
  • Ahh, that makes sense.  I had absolutely intended to use proper state $variables but I must have missed out the $-sign without even noticing.  Figures it would be javascript being overly permissive again.  I like coding in js, but it can be a pain to debug sometimes.  Thanks for the troubleshooting!

    Is there somewhere that has the state and history system detailed a little more clearly?  It didn't really seem to be mentioned in the introductory material I was reading on the wiki.
Sign In or Register to comment.