Twine code, keys, and locked doors

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

Comments are closed.