Add an element of chance

1. Create a variable with random information stored in it

Imagine your reader makes a decision, and the outcome of that decision will differ on each play-through. You can achieve this by creating a variable and storing a list of different information in it. Twine will pick one of the elements randomly.

This is how it works:

  1. Create a variable, but instead of storing one piece of information in it, you add a list of different things, and Twine picks at random which of those will be stored in the variable.

  2. This is how it looks:

    <<set $playingCard to either ("ten of spades","four of diamonds","jack of hearts")>>

  3. Now, you can simply write the variable name, and the reader will see one of the random elements. Alternatively you can use the variable name with the instruction <<if>><</if>> and show additional content.

2. Watch how it's done

3. Start writing yourself

  1. Open a passage.
  2. Create the variable with a random list.
  3. Write the variable name, and Twine will pick one of the random elements to show to the reader now stored in the variable.
  4. You can also add certain content that will be shown when a certain element will be picked.

Copy the following instructions and paste them into your passage, to start writing.

<<set $coinToss to either ("heads","tails")>>

<<if $coinToss is "heads">> 
He frowns, and holsters his weapon. "This is your lucky day, punk." 
<</if>> 
<<if $coinToss is "tails">>
He grins and raises his gun. "Unlucky, punk!"
<</if>>

4. Be aware of this

Each element in a list must be put in double-quotes, also numbers.
The list of elements must be put in brackets.
Remember that you don’t have control over what element will be chosen.

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 and open the passage "Random Variable".

  3. At the top of the passage, you have the random list:

    Screenshot of how to set the information of an variable randomly in Sugarcube 2.
  4. Then you have the variable name:

    Screenshot of how to use a variable in Sugarcube 2.
  5. Then you have content that is connected to the random elements:

    Screenshot of how to use a variable and if instructions in Sugarcube 2.

So, depending on whether Twine "rolls" a 1, 3 or 5, or a 2, 4, or 6, the last line the reader sees will vary.

6. Takeaways

Put brackets around the list elements.
Each element inside the brackets has to be put in double quotes.