Twine Tutorial: Conditional Questions
Posted on October 23rd, 2013
A friend on Twitter recently asked me how I did the question/answer part of my Twine game Eden. Basically 1)once you’ve asked a question, it’s marked as having been asked once you return to the list 2)asking certain questions makes new ones appear and 3)you only get the option to stop asking questions once at least one has been asked. Here are a few screencaps to show this:
Anyway, the steps involved are a bit lengthy to post on Twitter. So I figured I’d write this article for anyone who might find it useful. Please note that this tutorial assumes familiarity with Twine and some familiarity with Twine conditional statements. If you need a beginner’s tutorial, here’s an excellent primer from Auntie Pixelante.
For demo purposes, I created a tiny Twine file that does all of the above with minimal extraneous code. On the passage before the questions start, I declared a variable ($asked) for the number of questions asked and set it to zero, like this:
The setup for the question-asking passage is basically this:
And the source for each answer linked basically looks like this:
Once you land on an answer passage, a couple of things happen. First, it adds one to the $asked value (<<set $asked = $asked +1>>). Then, it declares that question’s variable name (in this case $first) and sets its value to “yes” (<<set $first = “yes”>>).
There’s also an if/else statement at the end of each question passage that checks to see if $asked is less than or equal to one. (That’s the <<if $asked lte 1>> part.) If so, the only link the player sees leads back to the question page so they can ask more. But if the player is on at least their second question, a new link appears, and the player can choose “That’s enough.”
Now, back to the question-asking page.
Once a question’s variable has been set to “yes”, the text “(Already Asked)” appears beside that question. This is what makes it happen:
<<if $first eq “yes”>>(Already asked)<<endif>>
Just substitute the variable name(s) for your question(s) for $first.
As for making hidden questions appear once you’ve asked others, it’s pretty much the same thing. All you have to do is enclose the question’s link/text in an <<if>> statement like the above.
Of course, if you’re like me, it might be more helpful to get your hands on the code itself. So, here’s a zip file containing the .tws and .html for the demo in the screenshots above. Feel free to use and modify it as you wish:
PS—I like the latest Alpha build (Note: Windows only) better than the current “official” Twine program. For me, it’s much more stable and makes debugging easier. If you’d like to check it out, here it is.
UPDATE (made sometimes in 2014): There’s a new official version out now, too. You can get that here.