Lesson Notes — Adding Conditional Logic to Your Twine Game
Save your work every day: Export your story and save it to Google Drive at the end of every class. Twine only saves your story in the browser on the laptop you are using, and you may not get the same laptop next time.
Lesson overview
Right now each choice in your game leads to the next passage, but the game does not remember anything the player did. Conditional logic changes that. The game stores what the player did in a variable, then later checks a condition: if something is true, then show one thing, else show something different. This is cause and effect: a choice early in the game changes what happens later.
This lesson uses Harlowe, Twine’s default story format. Harlowe code goes right inside your passage text.
About the examples: The examples on this page come from a sample story about a player trying to get a driver’s license at the DMV. You do not need those passages. Do each step in your own game, using your own passages and your own variable names.
Step 1: Create a variable in your first passage
A variable is a named box that holds a value. In Harlowe, variable names start with $. Use (set:) to put a value in it.
Create every variable in your first passage (the one with the green rocket icon). That way it has a value even if the player never makes that choice.
In your first passage:
(set: $studied to false)
true and false are special values. A value that can only be true or false is called a Boolean. Do not put quotation marks around them.
Step 2: Change the variable when the player makes a choice
Find the passage where the player does the thing you want the game to remember. Put a (set:) line at the top of that passage.
In the passage where the player studies (in the sample story, it is named Go back home and study more):
(set: $studied to true)
Now the game remembers that the player studied.
What the full passage looks like in Twine
The code line goes at the very top. Your story text goes below it, just like before. The player will not see the (set:) line.
Passage name: Go back home and study more
(set: $studied to true)
You spend the whole night reading the driver's manual.
By midnight you can name every road sign.
[[Go back to the DMV]]
Step 3: Check the condition with (if:) and (else:)
In a later passage, check the variable. The text inside the square brackets right after (if:) is called a hook. It only shows when the condition is true. The hook after (else:) shows when it is not.
In a later passage (in the sample story, the written test):
The test is on the screen in front of you.
(if: $studied is true)[The questions look familiar. You pass!]
(else:)[You do not know half of these. You fail the written test.]
What the player sees
| If the player studied | If the player did not study |
|---|---|
| The test is on the screen in front of you. The questions look familiar. You pass! | The test is on the screen in front of you. You do not know half of these. You fail the written test. |
Step 4: Show a choice only when it makes sense
A hook can hold a link, so the player only sees that choice when the condition is true:
(if: $studied is true)[ [[Go get your photo taken]] ]
(else:)[ [[Go home and try again next week]] ]
Put a space between the hook bracket and the link brackets. Without the spaces, Twine can misread the three brackets in a row and make a broken link.
| Wrong | Right |
|---|---|
[[[Go get your photo taken]]] |
[ [[Go get your photo taken]] ] |
Step 5: Use numbers for more than two outcomes
A variable can also hold a number. Start it in your first passage, then add to it when the player does something.
In your first passage:
(set: $confidence to 0)
In any passage where the player does something that builds confidence:
(set: $confidence to $confidence + 1)
This means “take the current number and add 1 to it.”
In a later passage, use (else-if:) to check more than one condition. Twine checks them from top to bottom and shows the first one that is true:
(if: $confidence >= 3)[You walk in calm and ready.]
(else-if: $confidence >= 1)[You are a little nervous.]
(else:)[Your hands are shaking.]
Shortcut: use the If button
You do not have to type every macro yourself. The toolbar above your passage text can write the code for you.
- Open a passage and click where you want the code to go.
- In the toolbar at the top of the passage, open the Macro menu and choose If. A window opens.
- Choose the condition you want to check, such as the value of a variable or something the player has done, like visiting a passage.
- Click OK. Twine puts the
(if:)code into your passage. - Read the code it added. Put the text or link you want to show inside the square brackets.
The Value option in the same menu writes a (set:) line for you.
Good to know: After you click OK, you cannot reopen that code in the window. To change it later, edit the code by hand. That is why this page teaches the code too.
Comparisons you can use
| Code | Means | Example |
|---|---|---|
is |
is equal to | $studied is true |
is not |
is not equal to | $studied is not true |
> and < |
greater than, less than | $confidence > 2 |
>= and <= |
at least, at most | $confidence >= 3 means 3 or more |
and |
both conditions are true | $studied is true and $confidence >= 2 |
or |
at least one condition is true | $studied is true or $confidence >= 3 |
Example using and:
(if: $studied is true and $confidence >= 2)[You pass with a perfect score!]
(else:)[You pass, but just barely.]
Common mistakes
- Forgetting the
$. Write$studied, notstudied. - Spelling the name differently.
$studiedand$Studiedare two different variables. Use the exact same spelling and capital letters everywhere. - Using
=instead ofto. Write(set: $studied to true), not(set: $studied = true). - Missing a bracket. Every
[needs a], and every(needs a). - No space around a link inside a hook. See Step 4.
Ideas for your game
- A key, item or clue the player picks up that opens a door or path later.
- A friend the player helped earlier who shows up to help them.
- A score that leads to a good, okay or bad ending.
Your turn
Add conditional logic
- Add at least one variable to your Twine game and set its starting value in your first passage.
- Change the variable when the player makes a choice.
- In a later passage, use
(if:)and(else:)to change the text or the choices based on that variable. - Play your game down both paths to check that each outcome appears when it should.
Also do this
- Add 10 new passages to your story by Friday. At least one path must change because of a variable.
- Export your story and save it to Google Drive at the end of every class.
Check your understanding
- What does
(set: $studied to true)do? - Why should every variable get a starting value in the first passage?
- In the test example, what does the player see if
$studiedis false? - Write an if/then/else sentence that describes the conditional you added to your game.
Use this lesson’s Key Terms page to build your glossary.