Variables, Part 2

1. Show content depending on reader's choices

In the previous section, you have learned how to show the stored information of a variable to the reader when you write the variable name in the text.

Now let's go one step further. Imagine that you let the reader choose whether they want to open a door or not. You can "remember" their decision with variables. Now you can show extra content throughout your story, for example, if the door was opened. If the door is not opened, the content is not displayed.

This is how it works:

  1. You need a variable name and the instructions to create the variable, as you learned in the section before.

  2. The difference: The information you store in the variable is either the word true or the word false. This can be used later to define what content the reader will see.

  3. This is how your variable looks in the end:

    <<set $openDoor to true>>
    (In our example, this means that that reader opened the door.)
    or
    <<set $openDoor to false>>
    (In our example, this means that that reader did not open the door.)

  4. To show your reader different content, based on their decision, you need the variable name, which you combine with a set of additional instructions:

    • An instruction to use with the variable: <<if>><</if>>
    • An instruction that checks if the reader made the choice: is
    • The content you want the reader to read if the reader made a certain choice: You try to open the door, but it is locked.
  5. This is how it looks:

    <<if $openDoor is true>>
    You try to open the door, but it is locked.
    <</if>>

  6. Now you can show the reader a bit of extra content if they opened the door. If they didn't open the door, they just wouldn't see this bit of content.

2. Watch how it's done

3. Start writing yourself

  1. Open a passage.
  2. Create a variable and store the information true or false in it.
  3. Create the content you want the reader to see if they make a particular decision and activate it with the set of additional instructions.

You can copy the following samples and paste them into your passages. Use the first one to create the variable after a choice. Use the second one in the passage where you want to show the content based on the choice. You can use it throughout your story in different passages showing different contents based on the same choice.

<<set $drinkTea to true>>
<<if $drinkTea is true>>
You drank the magic tea.
<</if>>

4. Be aware of this

If you store the information true or false in your variable and simply write the variable name in your text, the reader will see the actual word true or false.
Do not use double-quotes with true or false.

5. Try it out with the sample story

To better understand this topic, you can look at the sample story from the Introduction section. Download and import it here if you have not done so yet.

  1. Open the story.

  2. Locate the passages "Variable Part 2a" and "Variable Part 2b"

    Screenshot of 2 passages in Twine.
  3. At the top of either passage, a variable has been created. This is where that choice is creating a variable within the story.
    If you grabbed the box, in passage "Variable Part 2a" "$hasbox" has been set to true.
    If you grabbed the folder in passage "Variable Part 2b" "$hasfolder" has been set to true.

    Screenshot of how to set an variable in Sugarcube 2.
  4. Open the Passage "Variable Part 2c"

  5. Examine the content in the passage. You can see different paragraphs. Your reader will only see one of these paragraphs, depending on which item they decide to take.

    Screenshot of how to use an if instruction in Sugarcube 2.

6. Takeaways

Store the information true or false in a variable to show content based on the reader's decisions.
Use the variable with the instruction: <<if>><</if>>
Check, if the reader made the choice with: is

In this article, we showed you how to display extra content if, for example, the door was opened. In the next article, you will learn how to display another content if the door was not opened.