Twine variables

May 10, 2014

POWER-UPS, COINS, AND OTHER ITEMS

ocarina1Lots 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.

WEAPONS, HEALTH, AND WEIRD THINGS

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)

mariocoinsJust 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)

WHAT IS YOUR NAME?

fightclub1You 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?


mario

CONTINUE TO THE NEXT POST: Twine logic


CODE

dollarsign2Wouldn'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.
batman-mario-2-key

KEYS

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)


finalfantasy

Lose A Key

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:

twine6

LOCKED DOORS

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")
  ]
]


knockNow 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.
]

mario

CONTINUE TO THE NEXT POST: Twine variables


Each section of your Twine project is a box called a passage. To add a passage, you could click the green +Passage button in the bottom-right corner, but the easiest way is to simply create a link. Twine 2 will automatically create a New Passage for you every time you create a link. More about creating links in a moment.
To edit a passage, double-click it.

PASSAGES

A passage has three main fields: the title of the passage, tags, and the body. You can name other passages anything you like, but do not use the same passage name twice. Tags are only used for special types of code, so you can ignore the tags section. Everything that you type in the body will be shown to the player.

twine2

LINKS

linklinklinkLinks between passages are shown as arrows between passages. Links are like doors to other passages. They are the same as being told to turn to another page in a Choose Your Own Adventure book. Links are marked in the text of a passage by two square brackets:

[[This is a link]]

The text between the brackets must match the title of the passage that it links to. Passage titles are case-sensitive, which means that “Blue door” and “BLUE DOOR” are NOT the same.

twine5

HIDING LINKS

hiding

It is possible to change what text is shown to the player for a link. This can come in handy if you want to hide the title of the passage from the player. You can do that with an arrow, like this:

[[The text you want to display->The REAL Passage Title]]

For example: You may want to send the player to a passage called “Wrong” without letting them know that the link they are clicking is wrong. You can do this:

[[New York City is the capital of New York->Wrong]]

If your project has a broken link in it, it is shown in red, and clicking it shows an error message:

twine4

Here is an example of a passage body with links:

You decide to go to the hotel dining room.
You [[read the newspaper]] and order a coffee.
You get the strange feeling that some is watching you.

[[Look behind you]]
[[Hide under the table]]
[[Go back to your room->You lose]]


who-wants-to-be-a-millionaire1

MULTIPLE CHOICE QUIZ

Here is an example of a multiple choice question with links:

How many sides does an octagon have?

[[7->Wrong]] [[8->Question 2]] [[9->Wrong]]

The correct answer takes the player to a passage called “Question 2.” All of the incorrect answers take you to a passage called “Wrong” which could look like this:

I’m sorry. That is incorrect.
Click the back arrow and try again.

VIEWING YOUR PROJECT

To view your project, click the Play button in the bottom-right. Twine will open your project in a new browser tab.

To save your finished project, click the menu in the bottom-left with the name of your project and then Publish to File. The project will save as an HTML file (a webpage).

mario

CONTINUE TO THE NEXT POST: Twine Pictures, GIFs, and Background Images




RECENT POSTS