Understanding Variables and Conditional Logic in Twine: 5 Practical Examples for Interactive Stories
header01
Understanding Variables and Conditional Logic in Twine: 5 Practical Examples for Interactive Stories

Twine is a powerful tool for creating interactive fiction, allowing storytellers to craft dynamic narratives where player choices influence the story's direction. You can branch your story by using links (you go left or right), this is fine when consequences are immediate but it's not always enough. A crucial aspect of making interactive stories engaging is using variables and conditional logic to track player decisions, character attributes, and branching storylines. In this beginner-friendly tutorial, we'll explore how to use Twine's variables and conditional statements with five practical examples.

What Are Variables in Twine?

Variables in Twine store information that changes throughout the game, such as player names, inventory items, or story progress. They are essential for tracking choices and customizing experiences in interactive fiction. In Twine, you can use three main types of variables: numbers (integers), text (strings) and True/False (booleans). See below how to use them.

Note: don't forget the blocks between square brackets can be extended and they can contain multiple macros and lines of texts. I use short snippets only for the clarity of the examples.

How to Set a Variable in Twine (Harlowe Format)

To define a variable, use the (set:) macro:

  • $playerName stores the player's chosen name, this is a string variable.
  • $score starts at 0 and can be increased as the game progresses. This is an integer variable.
  • $hasKey is a boolean variable and it can be used as a flag/switch. Here it means the player has the key.

What Is Conditional Logic in Twine?

Conditional logic allows your story to react to player choices by displaying different text, paths, or interactions based on stored variables.

How to Use Conditional Statements in Twine

Conditional logic is implemented using the (if:), (else-if:), and (else:) macros. Here we use an integer variable to store the score:

This checks if $score is greater than 5. If true, the player sees "You are doing great!" Otherwise, they see "Keep going!".

5 Practical Examples of Variables and Conditional Logic in Twine

1. Personalized Player Name

Let players enter a name and use it throughout the story. prompt will show a popup and ask for user input. Its first argument is the text message and the second is the default value for the input field:

Note: if you want to avoid problems when players write special characters as name and Twine would try to interpret them as commands or HTML, you can be extra safe and wrap the player input like this:

(set: $name to (v6m-print: (prompt: "Please type your name!", "Adventurer")))

2. Basic Inventory System

Track whether the player picks up an item in a boolean variable and use it later. Here we initialize the boolean variable and set it to false (player has no key yet):

When the player goes to the passage called "Next," we check whether the key has been picked up or not. If the player has the key, the door will be open, and a new option will appear: "Enter the room." Otherwise, this passage is a dead end and the door remain locked:

3. Karma System

During the story, you can take actions to change your karma like this:

...and based on your karma, either during the game or at the and of the game, the story will continue / end differently:

4. Branching Dialogue Based on Previous Actions

You save a state, similarly as you did for the basic inventory:

And later in the game, you use that state for branching the story:

5. Health and Battle Mechanics

Twine interactive story scripting interface screenshot with the battle system implemented with variables, go-to links and branching.

I also implemented a very basic battle mechanic for my game (Demon Hunter: Legacy). Here we will use a very basic setup but something you can extend - if it's too complex for now, don't worry. I will write more about battle mechanics in a separate article probably later.

You need an init section before a battle, you can call this passage "Init". The go-to command will be redirecting the player to the passage called "Battle" after the variables are set. We need this, so we won't accidentally reset HP at every turn in the battle:

And then, probably you need a passage for the battle itself, I just called it "Battle". This will represent one round and will be looped. You take an action here, it changes HP values and you will be redirected to the "Check HP" passage:

In the "Check HP" passage we check if either the player or monster died (HP equals to or lesser then zero) and then you need branching to continue the battle, victory or death. In case of victory, you will see a link to "Proceed", if you died, it just prints this game over message and if no one died, we redirect back to the "Battle" passage and continue the battle.

Conclusion: Make Your Twine Stories Dynamic!

By using variables and conditional logic, you can create more immersive interactive stories in Twine. Whether tracking choices, managing inventory, or designing branching narratives, these techniques add depth to your Twine projects.

Start experimenting with these examples in your own Twine game and see how they enhance player engagement. In the next article, we will investigate links further and you can create a simple shop. Happy storytelling!

Need more tips and tricks for FREE?

Sign up to my Twine newsletter for more:

Do you need a boost?

I have resources, graphical templates, tutorials etc. for Twine starting from 1 EUR, these bundles can save you hours and hours and will give your good-looking templates so your game looks professional.

Need help? Let's talk!

I can give you a head start if you want by offering my time and help you personally. Use the button below to book a call with me and you can ask me your questions in advance.

2 thoughts on “Understanding Variables and Conditional Logic in Twine: 5 Practical Examples for Interactive Stories

Leave a Reply

Your email address will not be published. Required fields are marked *