Variables, Part 1

1. Create and use a variable

When we say variable, we mean a simple coding tool that allows readers to shape your story uniquely. There are many different ways you can use variables to make your story more flavourful. No worries, the following sections will help you understand the concept of variables without any coding knowledge.

Imagine you have a character called Mr. Lord Dukesworth Stanford Highpip the Thirty-Second Earl of Windybottom-Upon-Duckton. He insists on introducing themselves in full to everyone they meet. It would be time-consuming to type out his name throughout your story.

With a variable, you can use one tiny word to show his full name whenever you want. You don’t need to type it in full anymore!

This is how it works:

  1. Give the variable a name starting with a $-sign: $mrWindy

  2. Think of the information you want to store in the variable: "Mr Lord Dukesworth Stanford Highpip the Thirty-Second Earl of Windybottom-Upon-Duckton"

  3. Write instructions for Twine:

    • An instruction to create the variable: <<set>>
    • An instruction to connect the variable with the information: to
  4. This is how it looks:

    <<set $mrWindy to "Mr Lord Dukesworth Stanford Highpip the Thirty-Second Earl of Windybottom-Upon-Duckton!">>

  5. Now we can print “Mr Lord Dukesworth Stanford Highpip the Thirty-Second Earl of Windybottom-Upon-Duckton” in our story at any time by typing the variable name $mrWindy into our text. However, this feature is useful for other areas as well, which you will see in later sections.

2. Watch How It's Done

3. Start writing yourself

  1. Open a passage.
  2. Create a variable and store information in it.\
  3. You can use the variable name throughout your story in different passages and the reader will see the information stored in it.

You can copy the following instruction and paste it into your passage to start using variables.

<<set $pizzaToppings to "tomato, salami, pineapple and peppers">>

“Hello. I would like to order a pizza with $pizzaToppings.”
“All right. Just to confirm, you would like $pizzaToppings on your pizza, correct?”

4. Be aware of this

You can have multiple variables in your story.
The name of every variable must be unique.
You can also use numbers instead of text:
<<set $number to 12>>
Be aware that when the information you want to store in your variable is a number, you may not use double-quotes.

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. Open the passage “Start”. There is a variable created at the top of the file and immediately used in the text.
    Screenshot of how to create and use a variable in Sugarcube 2.
  3. Play through by clicking “Play” and a playable version opens in your browser. Every time you read “B.D Gavanagh”, we used a variable to display the name.

6. Takeaways

Name your variable always as one word starting with: $
Create the variable with the instruction: <<set>>
Connect the variable with the information by using the instruction: to
Use variables by typing the variable name directly into your text.

In the next article, you will learn how to use variables to make the impact of choices more personalized.