User Tools

Site Tools


twine1:code

Why Use Code?

So far, the stories you have learned how to create with Twine have been displayed more or less as you have entered them in your source code. The only exceptions have been certain notations you use for formatting and creating links. You can create complete stories with what you already knew, but what if you wanted to recreate a passage like this?

As you reach the door you hear the crash of a giant stone slab as it falls from the ceiling. Turning around, you see that your exit is now blocked.

If you have a Silver Key, you may try to open the door by turning to 158.

If you do not possess a Silver Key, turn to 259.

(Joe Dever, Flight from the Dark)

You could certainly ask your readers to click the appropriate link, but wouldn't it be better if your story could remember if the protagonist picked up the silver key earlier in the story, and only display it if it's appropriate? Code lets you accomplish these kinds of tasks. In short, code consists of instructions to be carried out by a computer, not text to be read by the reader.

In Twine, code takes the form of macros placed in your story's text. These are set off by double angle brackets, «like this», in a manner resembling HTML tags. When a passage is displayed, the macro invocation isn't displayed; instead, its code runs. For example, this source code would produce something akin to the text quoted above:

As you reach the door you hear the crash of a giant stone slab as it falls from
the ceiling. Turning around, you see that your exit is now blocked.
<<if $hasSilverKey>>\
* [[Open the door with your key]]
<<else>>\
* [[Try to force the door open]]
<<endif>>

Even though macros are never seen by a reader, they are designed so that they make sense to someone reading your source code – even someone who may not be familiar with them, as you are right now. Nevertheless, you don't need to understand how a macro works. You only need to know how to use it to accomplish what your story needs to do.

twine1/code.txt · Last modified: 2017/08/19 17:15 by videlais