You can use if with your Twine variables in order to hide or show things to the player. The code below says to the computer, “If the player’s health is less than one, they died. If the player’s health is greater than zero, keep playing.”
(if: $health < 1)[
You died. GAME OVER!
]
(if: $health > 0)[
Ouch! That hurt! Now where do you want to go?
[[Go outside]]
[[Go upstairs]]
]
A simpler way to check for a dead player is to use if and goto together. The code below says to the computer, “If the player’s health is less than one, send them immediately to the passage titled Game Over.”
(if: $health < 1)[
(goto:"Game Over")
]
You can also use else along with if. The code below says to the computer, “If the player’s health is less than zero, they died. Otherwise, keep playing.”
(if: $health < 1)[
You died. GAME OVER!
]\
(else:)
[
Ouch! That hurt! Now where do you want to go?
[[Go outside]]
[[Go upstairs]]
]
In the previous examples, we used > greater than and < less than to compare our variables. Here are some of the most common comparisons:
is |
Equal To |
> |
Greater Than |
>= |
Greater Than Or Equal To |
< |
Less Than |
<= |
Less Than Or Equal To |
is not |
Is Not Equal To |
If you want to sell an item to the player, you can do this:
(if: $money >= 500)[
[[Buy a sword for 500]]
]\
(else:)[
Swords cost 500, but you only have $money.
]
When a player picks up an item (a key, some money, etc.), how can we make sure that the player cannot pick up that same item again? Use the code below, which says, “If the player has already visited the Blue Room, the room is empty. Otherwise, they found a key.”
(if: (history:) contains "Blue Room")[
The room is empty.
]\
(else:)[
You found a key. (set: $keys to $keys + 1)
]
Sometimes you need to say if this is true and if this other thing is true. The code below says, “If the player has flour, eggs, and sugar, they can bake cookies.”
(if: $flour > 0 and $eggs > 0 and $sugar > 0)[
[[Bake cookies]]
]
The code below says to the computer, “If the player’s health is less than or equal to zero and they have one or more lives, lose a life but keep playing. If the player’s health is less than or equal to zero and they have no more lives, game over.”
(if: $health <= 0 and $lives >= 1)[
(set: $lives to $lives – 1)
(set: $health to 100)
You lost a life! You have $lives lives left.
[[Continue]]
]
(if: $health <= 0 and $lives <= 0)[
You died! You are out of lives. GAME OVER!
]
Lots of things can be stored as variables. A variable can be something that is typically found in a video game (points, lives, power-ups, coins, items, weapons, health, etc.) or a variable can be something weird (ninjas, cellphone, fire breath, pizza toppings, random treasure, etc.). You can use $icespell to keep track of if the player has learned the ice spell. The number of coins can be remembered with either $coins or $money or $rupees or something like that.
You can use $lives to store the player’s extra lives and $pizzatoppings to store the number of toppings they have collected. The player’s $health might start at 100, but can change during the game. The player might start without a $weapon, but then find one later. The first thing you need to do is set these things to zero (or to 100 or to whatever number they should start at) in the starting passage.
(set: $money to 0)
(set: $keys to 0)
(set: $sword to 0)
(set: $health to 100)
Just like with the number of keys (which I mentioned in my last post), use set to change things later in the game. The code below says to the computer, “Take the player’s health, subtract ten, and set this as the player’s health.”
You fell down the stairs.
(set: $health to $health – 10)
The code below says, “Take the player’s money, add 25, and set this as the player’s money.”
You found $25.
(set: $money to $money + 25)
You can store either words or numbers in a variable, but so far we’ve only been talking about storing numbers. One common word variable is the player’s name. We can use the code prompt to create a popup box in which the player can type their name. The following code shows the player a popup box that says, “What is your name?” and stores what they type as $name.
(set: $name to (prompt:"What is your name?",""))
Once the player’s name is stored, you can use $name to show their name .
Hello $name, how are you today?
CODE
Wouldn't it be great if your Twine game could remember if the player picked up an item? Code will let you do this and much more. Code is special directions for the computer that will not be seen by the player. In Twine, code is written inside of parenthesis:
(write code inside parentheses, like this)
The first thing that you need is a variable. A variable stores something so that the computer remembers it later. You can store either words or numbers in a variable. A variable MUST start with a dollar sign. The variable $name might contain the player's name, “David”, and the variable $keys might contain the number of keys David has in his pocket, 1.
You can keep track of the number of keys that the player is holding. At the beginning of the game, in the starting passage, use this set code:
You have one key in your pocket. (set: $keys to 1)
Later if the player finds a key, you can change the stored number with another set code. The code below says to the computer, “Take the number of keys, add two, and set this as the number of keys.”
You found two keys on the floor. (set: $keys to $keys + 2)
If you lose a key, you will subtract one key from the total number of keys. The code below says to the computer, “Take the number of keys, subtract one, and set this as the number of keys.”
You lost a key. (set: $keys to $keys – 1)
You can also attach this type of code to a link so that the code only runs when the player clicks on the link. This type of link is called a setter-link. It's a little more complex than a typical link. The code below says, “When the link is clicked, take the number of keys, subtract one, and set this as the number of keys, then continue to the room.”
(link:"Give a key to the old man")[
(set: $keys to $keys – 1)
(goto:"Old Man says Thank You")
]
After "link:" you will type what the link will say and after "goto:" you will type where the player will go next.
You can show the player the number of keys like this:
In your pocket you have $keys keys.
The player will not see the code, $keys. The player will see:
In your pocket you have 2 keys.
If you make a mistake with code, a highlighted error message will show up. For example, if you forget to put the dollar sign in front of the keys variable you will see:
The best process for creating a locked door will only show the link to unlock the door if a player has keys. When they click the link, it will automatically subtract one key and take them to the next room.
The blue door is locked.
(if: $keys > 0)[
(link:"Unlock the blue door")[
(set: $keys to $keys – 1)
(goto:"Blue Room")
]
]
Now let's look at how that works. Variables can determine what the player sees. For more information about variables, you should check out the next post, Twine variables. The player should only be able to unlock a door if they have more than zero keys. You can use the code if to do this. Anything you type between the left square bracket "(if: $keys > 0)[" and the right square bracket "]" is ONLY displayed if the number of keys is greater than zero.
The blue door is locked.
(if: $keys > 0)[
This text is only displayed if you have keys.
]
RECENT POSTS