Twine logic

IF THIS, THEN THAT

output_2O1ZTz

You can use if with your Twine variables in order to hide or show things to the player. The code below says to the computer, “If the player’s health is less than one, they died. If the player’s health is greater than zero, keep playing.”

(if: $health < 1)[
  You died. GAME OVER! 
]
(if: $health > 0)[
  Ouch! That hurt! Now where do you want to go?
  [[Go outside]]
  [[Go upstairs]]
]

A simpler way to check for a dead player is to use if and goto together. The code below says to the computer, “If the player’s health is less than one, send them immediately to the passage titled Game Over.”

(if: $health < 1)[
  (goto:"Game Over") 
]


IF-THEN-ELSE-END_flowchart

You can also use else along with if. The code below says to the computer, “If the player’s health is less than zero, they died. Otherwise, keep playing.”

(if: $health < 1)[
  You died. GAME OVER! 
]\
(else:)
[
  Ouch! That hurt! Now where do you want to go?
  [[Go outside]]
  [[Go upstairs]]
]

EQUAL TO, GREATER THAN, LESS THAN

In the previous examples, we used > greater than and < less than to compare our variables. Here are some of the most common comparisons:

is

Equal To

>

Greater Than

>=

Greater Than Or Equal To

<

Less Than

<=

Less Than Or Equal To

is not

Is Not Equal To

FOR SALE

35719137079economy4gif6

If you want to sell an item to the player, you can do this:

(if: $money >= 500)[
  [[Buy a sword for 500]]
]\
(else:)[
  Swords cost 500, but you only have $money.
]

ONLY ON THE FIRST VISIT

When a player picks up an item (a key, some money, etc.), how can we make sure that the player cannot pick up that same item again? Use the code below, which says, “If the player has already visited the Blue Room, the room is empty. Otherwise, they found a key.”

(if: (history:) contains "Blue Room")[
  The room is empty.
]\
(else:)[
  You found a key. (set: $keys to $keys + 1)
]


7w7wej

AND IF THIS

Sometimes you need to say if this is true and if this other thing is true. The code below says, “If the player has flour, eggs, and sugar, they can bake cookies.”

(if: $flour > 0 and $eggs > 0 and $sugar > 0)[
  [[Bake cookies]]
]

The code below says to the computer, “If the player’s health is less than or equal to zero and they have one or more lives, lose a life but keep playing.  If the player’s health is less than or equal to zero and they have no more lives, game over.”

(if: $health <= 0 and $lives >= 1)[
  (set: $lives to $lives – 1)
  (set: $health to 100)
  You lost a life! You have $lives lives left.
  [[Continue]]
]
(if: $health <= 0 and $lives <= 0)[
  You died! You are out of lives. GAME OVER!
]


mario

CONTINUE TO THE NEXT POST: Twine random numbers





RECENT POSTS