Variables, Part 3

1. Show alternative content depending on reader's choices

In the previous section, you have learned how to show different content depending on reader choices. Now we want to expand on this knowledge. Let’s add the possibility not only to show the reader content based on their choice but also content if they choose something different.

Back to our door example. If the reader chooses to open the door, they see the content that the door is locked. If they do not choose to open the door, they will see different content.

This is how it works:

  1. You need everything you learned in the section before.

  2. The difference:

    • You add the instruction: <<else>>
    • You add the content you want the reader to read if they do not make a particular choice: You don’t want to open the door now, maybe later.
  3. This is how it looks:

    <<if $openDoor is true>>
    You try to open the door, but it is locked.
    <<else>>
    You don’t want to open the door now, maybe later.
    <</if>>

  4. Now you can show the reader content if they open the door and different content if they do not open the door.

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 content that you want the reader to see if they do not make a particular decision. Activate everything 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 choice.

<<set $drinkTea to true>>
<<if $drinkTea is true>>
You have the feeling the tea does something magic.
<<else>>
Now you regret you didn't drink the tea. 
<</if>>

4. Be aware of this

You can only use the instruction <<else>> with the instruction <<if>>.
The instrcution <<else>> has to be within between <<if>> and <</if>>.

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. You have already seen the variables created in the passages “Variable Part 2a” and “Variable Part 2b”

  3. Open the Passage “Variable Part 3”

  4. 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 decided to take.

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

Takeaways

Expand your <<if>>-instruction to add alternative content with the instruction: <<else>>

So, those were the basic ways to use variables for your story. In the next articles you will learn more ways to use variables.