Press "Enter" to skip to content

Conditions

Sample game

Comparison operators

OperatorDescription
isis equal?
is notis different from?
>is bigger?
>=is bigger or equal?
<is smaller?
<=is smaller or equal?

See more in documentation:

The if-elseif-else instructions

(if: )

Example 1

Depending on whether the gate is closed or opened (true/false), you can see a hidden message. The value of the $gate_closed variable may be changed in any slip or in any particular location in a particular fragment of the story (it does not have to change its value at the top of the slip – it can be done between the paragraphs of the text).

(set: $gate_closed to true)

You try to open the gate...
(if: $gate_closed is true)[- unfortunately it is closed! First, you need to get the key.]

Result 1

Result: You try to open the gate... [- unfortunately it is closed! First, you need to get the key.

Example 2

If you use a lot of variables, IF instructions, etc. to increase the clarity of the code (a possibility of using whitespace characters, such as ENTER), use { } characters and code indentation. It will allow you to avoid awkward and unexpected empty space on your slips.

(set: $tip to  1)


You try to open the gate... unfortunately, it is closed!
{
    (if: $tip is 1)[maybe I should look for the key in my pocket?]
    (if: $tip is 2)[maybe I should check under the flowerpot?]
    (if: $tip is 3)[it seems you need to find another way!]
}

Result 2

Result: You try to open the gate... unfortunately, it is closed! maybe I should look for the key in my pocket?

See more in documentation

(if: )(else: )

Example 3

(set: $key_found to false)


You try to open the gate...
{
    (if: $key_found is true)[you open the gate with the key you have found and you enter the courtyard.]
    (else:)[- unfortunately, it is closed! First, you need to get the key.]
}

You can save the IF instruction in its shorter version (if: $key_found)[...] because the $key_found variable shall take true or false – it does not have to be additionally compared using the is operator.

Result 3

Result: You try to open the gate... - unfortunately, it is closed! First, you need to get the key.

See more in documentation

(if: )(else-if: )(else: )

Example 4

(set: $points to 10)


SUMMING UP: you have got $points points.
{
    (if: $points >= 15)[Well done!]
    (else-if: $points is 0)[Bad luck!]
    (else:)[Not bad! It will be even better next time.]
}

Result 4

Result: SUMMING UP: you have got 10 points. Not bad! It will be even better next time.

See more in documentation

The IF instruction, links and variables

If the gate is closed (true), you will see a text with a link to the “key” slip, otherwise (false) you will see a text with a link to the “courtyard” slip. You can fully decide which links will be visible for players and which links will stay hidden.

Example

(set: $gate_closed to false)
(set: $congratulations to "You’ve made it!")


You try to open the gate...
{
    (if: $gate_closed is true)[First, you need to get the [[key]].]
    (else:)[$congratulations You can go to the [[courtyard]].]
}

An editor view

Twine - View from the editor. The start card is connected with the key and courtyard cards.

Result

Result: You try open the gate... You`ve made it! You can go to the courtyard.

The IF and (display: ) instruction

The (display: ) macro allows you to display the content of a particular (already existing) slip on another slip. You can also see another example of applying that macro in the tutorial part referring to randomness.

Example

(set: $action to 2)


You enter the courtyard.
{
    (if: $action is 1)[(display: "action-1")]
    (else-if: $action is 2)[(display: "action-2")]
    (else-if: $action is 3)[(display: "action-3")]
    (else:)[However, there is nobody there.]
}

An editor view

Twine - View from the editor. None of the cards are linked by arrows. There are cards on the graphic: start, action 1, 2 and 3.

Result

Result: You enter the courtyard. Action 2.

Logical operators

OperatorDescription
andlogical conjunction
orlogical disjunction
notlogical negation

Logical conjunction and

The result of the and (logical conjunction) operation is the True value if and only if the value of both arguments is True.

Argument 1Argument 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Logical disjunction or

The result of the or (logical disjunction) operation is the True if the value of at least one argument is True. The False value occurs if and only if the value of both arguments is False.

Argument 1Argument 2Result
TrueTrueTrue
TrueFalse True
FalseTrue True
FalseFalseFalse

Logical negation not

The result of the not operation is the opposite value of the argument.

ArgumentResult
TrueFalse
FalseTrue

Example

(set: $night to false)
(set: $flashlight_on to false)


It is (if: $night)[night](else:)[day].
There is an (if: $flashlight_on)[on](else:)[off] flashlight.


{
    (if: $night and $flashlight_on)[You notice some strange symbols on the wall. It might be some kind of a password...]
    (else-if: not $night and $flashlight_on)[Turning on your flashlight is not a good idea.]
    (else-if: $night and not $flashlight_on)[It would be a good idea to turn on your flashlight. It’s very dark in here.]
    (else:)[You should wait until the evening and turn on your flashlight to see whether there are any tips hidden in here.]
}

Result

Result: It is day. There is an off flashlight. You should wait until the evening and turn on your flashlight to see whether there are any tips hidden in here.

More information

Font Resize
Contrast