# Twine Wiki

### Site Tools

displaying_text_conditionally

This is an old revision of the document!

## Displaying Text Conditionally

Variables can be very handy, but they would be much more useful if they could directly affect the text the reader sees. Consider a passage like this:

You return to Selator's hut. A merry fire is crackling in the kitchen, and something is cooking that smells delicious. He greets you warmly and asks, “Have you got the berry?” If you have got the purple berry of the Antherica plant, turn to 175. If not, turn to 52.

(Steve Jackson and Ian Livingstone, Scorpion Swamp)

It would be nice if the story could track whether the protagonist found the berry or not, and branch accordingly. In order to do this, we need to use conditions. A condition is a kind of expression that evaluates to either true or false. We can use these truth values directly to indicate whether the protagonist found the berries:

```You have no doubt, from Selator's description, that you have found the
village with the precious berry. <<set \$foundBerry = true>>```

Then we can use the `«if»` macro to display a passage indicating victory:

```You return to Selator's hut. A merry fire is crackling in the kitchen, and
something is cooking that smells delicious. He greets you warmly and asks,
"Have you got the berry?"

<<if \$foundBerry>>
"Wonderful!" he exclaims...
<<endif>>```

Anything in between the initial `«if»` and `«endif»` is displayed if the condition is true. You may also include macros inside `«if»` statements, so we could display a longer victory message this way:

```You return to Selator's hut. A merry fire is crackling in the kitchen, and
something is cooking that smells delicious. He greets you warmly and asks,
"Have you got the berry?"

<<if \$foundBerry>>
<<display "Victory">>
<<endif>>```

Our only remaining issue is that if the reader hasn't found the berry, nothing is displayed at all. To remedy this, we can use an `«else»` clause like this:

```You return to Selator's hut. A merry fire is crackling in the kitchen, and
something is cooking that smells delicious. He greets you warmly and asks,
"Have you got the berry?"

<<if \$foundBerry>>
<<display "Victory">>
<<else>>
"That's too bad," he says. "I had such high hopes for you..."
<<endif>>```

`«else»` clauses do the exact opposite as `«if»` ones; they are only displayed if the condition is false. In either case, it's important to remember the `«endif»` at the end; otherwise, it won't be clear where the story should resume.

Let's revisit the example of meals from the previous section. We had this passage:

`You are feeling tired and hungry and you must stop to eat. <<set \$meals = \$meals - 1>>`

Obviously, if the protagonist doesn't have any meals left, they can't eat. Let's fix this with «if».

```You are feeling tired and hungry and you must stop to eat.

<<if \$meals eq 0>>
<<display "Dying of hunger">>
<<else>>
You continue on your journey... <<set \$meals = \$meals - 1>>
<<endif>>```

`eq` is a logical operator that's short for 'equals.' Just like + adds two numbers together, `eq` compares two things together and returns whether they are identical. It works equally well with strings and numbers, but beware – the string “2” is not equal to the number 2.

There are several logical operators available:

Operator Function Example
eq Evaluates to true if both sides are equal. `\$bullets eq 5`
neq Evaluates to true if both sides are not equal. `\$friends neq \$enemies`
gt Evaluates to true if the left side is greater than the right side. `\$money gt 3.75`
gte Evaluates to true if the left side is greater than or equal to the right side. `\$apples gte \$carrots + 5`
lt Evaluates to true if the left side is less than the right side. `\$shoes lt \$people * 2`
lte Evaluates to true if the left side is less than or equal to the right side. `65 lte \$age`
and Evaluates to true if both sides evaluates to true. `\$hasFriends and \$hasFamily`
or Evaluates to true if either side is true. `\$fruit or \$vegetable`
not Flips a true value to a false value, and vice versa. `(not \$hungry) or (\$location eq “restaurant”)`

Conditions can quickly become complicated. The best way to keep things straight is to use parentheses to group things:

```<<if (\$master eq 'Selator') and (\$foundBerry)>>
The walk back to the village is a happy one...
<<else>>
Well, you may not have succeeded in your mission, but at least you're alive...
<<endif>>```