Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
html
... i.e., two angle brackets surrounding something, where something is the name of the macro you want to use. (Some macros have other information between the brackets, as we'll see below.) Note
that macros are not HTML tags! They only work in Twine, not in any old web page.
1 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
2 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
This looks complicated, so let's break it down. The word textinput indicates to Twine that we want to use the textinput macro, which causes a text input box to appear on the screen. The
textinput macro needs a little bit more information to do its job, though.
First off, the reader's going to input some text, and we want to use that text later. We need to give that text some kind of handle or name so we can refer to it later. The name by which a value is
known in Twine is called a "variable name." In this case, $player_name is the variable name. (We could have chosen whatever name we wanted---it just has to start with a $ and only contain letters,
numbers and underscores.)
Secondly, the reader needs to go to a different passage when they click the button next to the text field. The name of the passage to link to is contained inside the square brackets. And yes, it does
look just like a Twine link! You can even change the text on the button using the pipe syntax, just like with a link. For example, this code would change the text on the button from "Greetings" to
"Click me!"
<<textinput $player_name [[Click me!|Greetings]]>>
Altogether, you can read the macro as saying: "Hey, Twine! Make a text input box. Whatever the reader types in, remember it in a variable named $player_name. Oh, and when the reader clicks on
the button, send them to a passage with the name Greetings. Thanks!"
Displaying the value of a variable
Of course, just putting what the player typed into a variable isn't going to do much. We need to actually do something with that variable. Twine has a number of macros for manipulating variables
and changing what the game does based on the value of a variable. The simplest of these is <<print>>, which you use like so:
<<print $variable_name>>
... replacing $variable_name with the name of the variable you'd like to display. In the example above, we told the the <<textinput>> macro to use the variable name $player_name, which is why
we put
<<print $player_name>>
... in the Greetings passage. This causes whatever value we stored in the variable $player_name earlier to be included in the text of the passage.
EXERCISE: Make a Twine game that asks the reader multiple questions about themselves, one after another. (E.g., "what's your name", "what's your hair color", "think of a number
3 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
from one to fifteen", etc.) In the final passage, display all of the information that the reader typed in.
Read more about the <<textinput>> macro here and about the <<print>> macro here.
Setting variables
The <<textinput>> macro is just one of many ways to get variables into our games. Sometimes we want to make our own variables and set their values to something of our own imagining, not that
of the reader. We can do this with the <<set>> macro. The syntax of the <<set>> macro looks like this:
<<set $variable_name to value>>
... where $variable_name (again) is a name that we choose for the variable, and value is some kind of value, usually either a number (like 5) or a string of text (like "Mother said there'd be
days like these"). (More about different kinds of values below.)
Here's a screenshot of a simple Twine game that makes use of the <<set>> macro.
4 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
Personality Quiz
This game is a simple personality quiz. Based on which color you prefer, the game sets the variable $personality to one of two things, then displays the result in the final passage. Here's a sample
playthrough:
5 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
The variable $favorite_color will now be "blue" until the end of the story (or until your story changes its value again!). Here's an example game that changes a variable that tracks the reader's
hunger level in response to the choices that they make.
6 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
7 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
If you want to increase the value in $score by 1, you might put this macro in another passage:
<<set $score to $score + 1>>
The $score + 1 in the above code is the "expression" in question: essentially, it's an instruction to the computer to figure out what the value of the $score variable is, with 1 added to it.
Here's an example Twine game that increases the number of "widgets" in the player's possession every time they visit a particular passage.
8 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
Widget game
The $widget_count variable gets set to 0 in the Start passage. Whenever the player clicks on the Take widget passage, the following macro is run:
<<set $widget_count to $widget_count + 1>>
This macro causes the value in $widget_count to increment by one. When the variable is <<print>>ed in the Showroom passage, you can see that the variable has been changed.
Here's what it looks like in action:
9 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
For example, the following code would display "You've got the key!" only if the variable $player_has_key has been set to a value with the <<set>> macro previously. (Technically, it happens if the
variable has been set to a value that isn't 0 or an empty string ""; full details here.)
Here's a Twine game that makes use of <<if>>/<<endif>> macro to show a stretch of text in a passage only if the player has visited another passage:
10 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
Cat of Time
Here you can see that the belongings passage <<set>>s the $wearing_crystal_specs variable to 1 when the reader visits the passage. When the player returns to Start, the previously hidden
stretch of text (linking to the cat) appears, allowing the reader to complete the story. Here's an animation showing how it happens:
11 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
In the example above, the phrasing is a little awkward: the reader is invited to go through their possessions to look for the crystal spectacles even if they're already wearing them! That's not quite
right. Ideally, we'd like to have a way to say something like: "If the $wearing_crystal_specs variable has been set, then do this. Otherwise, do something else."
Twine provides a macro for doing just such a thing, and it's called <<else>>. Here's how it's used:
<<if $player_has_key>>You've got the key!<<else>>You need a key. :(<<endif>>
The code above would display the text You've got the key! if the variable $player_has_key is set. If not, it displays You need a key. :(.
Here's a modified version of the Start passage from the example in the previous section that uses the <<else>> to only invite the reader to inspect their belongings when they're not already wearing
the crystal spectacles:
12 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
The above code will display the text You're out of coins! if the value of $coin_count is 0.
So let's write the story!
13 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
14 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
EXERCISE: There's another <<if>> macro in Flea Market that controls whether or not the s gets displayed after the word coin. Describe in prose how this macro does its job.
So, for example, to check whether a variable $player_name has some value other than Josephine, you might put the following macro in your code:
<<if $player_name neq "Josephine">>
This madeleine is only for Josephine! Go away!
<<endif>>
You can also combine expressions using the operators and and or. The and operator takes two expressions, one on its left, and the other on its right, and counts as "true" if both expressions are true.
The or operator similarly takes two expressions, and counts as "true" if either the left-hand expression is true, or the right-hand expression is true (or both). The expressions on either side of and and
or should be surrounded by parentheses.
So, for example, to make some text appear only if the reader has five coins AND that their name is "Robert":
<<if ($player_name is "Robert") and ($coin_count >= 5)>>
You are Robert, and you have enough coins to purchase this magnificent
and magical sword.
<<endif>>
To make some text appear if the player has either 0 points or 100 points:
<<if ($score is 0) or ($score is 100)>>
You are either really good at this game or really bad at this game.
<<endif>>
Finally, the operator not takes an expression directly to the right, and counts as "true" if the expression is "false," and "false" if the expression is "true." You can use this to check to see if a certain
condition does not obtain:
<<if not ($score is 0)>>
You have more or fewer than zero points! Good job.
<<endif>>
15 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
You may have noticed that every time we use a macro, some weird extra space appears in the rendered output. This happens because Twine counts the new lines that are inside the macro (i.e., after
the >>) even if the macro doesn't display anything (e.g., if an <<if>> condition isn't true). For example, the following code:
Here's some text before.
<<if $player_name is "Sandra">>
You are Sandra.
<<endif>>
And here's some text after.
...renders like this, if you put something other than Sandra in as the value of $player_name:
Excess whitespace
What's worse, even if the <<if>> macro condition is true, we still have extra whitespace!
16 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
One way to fix this is simply by removing as many new lines as possible, nestling the <<if>> macro up against the other text:
Here's some text before.<<if $player_name is "Sandra">>
You are Sandra.<<endif>>
And here's some text after.
This is fine (try it out!) but it's a little bit confusing, because the <<if>> macro is in a strange place. Ideally, we'd like to be able to retain the whitespace in our code, as a means of keeping things
clear, while still avoiding the appearance of unwanted whitespace.
Another way to do this is with the \ character (backslash). If you put this character at the end of a line, it instructs Twine to not display a new line in the rendered output:
Here's some text before.
<<if $player_name is "Sandra">>\
You are Sandra.
<<endif>>\
And here's some text after.
If you have a number of macros that you want to include in your code without introducing unwanted whitespace, you can use the <<nobr>> tag. The <<nobr>> macro, which instructs Twine to
ignore new lines up until it encounters <<endnobr>>. For example, to include a number of <<set>> macros without making a huge amount of whitespace:
<<nobr>>
<<set $cheese to 1>>
<<set $room_title to "Vestibule">>
<<set $current_trouser_type to "Denim">>
<<endnobr>>
Setter links
We've spent a lot of time here creating passages that serve one purpose only: to use the <<set>> macro to set a variable to a particular value, then continue to some other passage. It turns out that this
is a very common thing to want to do---so common, in fact, that Twine has a short-cut that allows you to set a variable right inside a link! This technique is called a "setter link" and it looks like this:
[[Text of link|PassageName][$variable = value]]
... where Text of link is the text that the link should display, PassageName is the passage the link should go to, $variable is the name of the variable you want to set, and value is the value you
want to set the variable to.
Using setter links, we can reduce our Personality Quiz Twine to just two passages:
17 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
18 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
Functions
Twine has still another way for you to introduce interesting behavior into your stories: functions. A "function" is a little piece of code, defined by Twine behind the scenes, that you can use in your
Twine code as if it were a value or an expression. We'll talk about a few of these below.
either()
The either() function takes a series of comma-separated values inside its parentheses. When you play your story, Twine will randomly choose one of these values. You can use this to add a bit of
color to your games:
Rain is falling from the <<print either("slate", "stone", "somber")>>
sky.
19 of 20
2/05/2016 4:08 PM
http://aparrish.neocities.org/twine-macros-variables-expressions.html
visited()
The visited() function can tell you how many times the reader has visited a particular passage in the game. Just put the name of the passage that you want to check inside the parentheses (and
inside quotation marks):
You have visited the cheese shop <<print visited("Cheeseshop")>> time(s).
You can also use this function in <<if>> macros to make decisions based on whether the reader has visited a particular passage:
<<if visited("University") > 0>>
You've been to the university, so you know everything you need to know
about interactive text.
<<endif>>
previous()
The previous() function evaluates to the name of the last passage the reader visited. You can use this to easily make a link that goes to the previous page:
Dead end. [[Try another passage|previous()]]
You could also use the value from previous() to check to see which passage the reader just came from, if a passage has more than one entry point:
You are successful in your career, even though <<if previous() is
"University">>you start with no real-world experience<<else if previous() is
"Training">>you have no formal education<<endif>>.
20 of 20
2/05/2016 4:08 PM