Please check out This Way or That, a new collaborative fiction site.

Advanced Topics

It's time to learn all about variables and conditional statements here.

I. Variables

If you're writing a story with branches, you'll often want to have your script 'remember' a reader's choice early in the story and then later use that decision to change the story presented.

For example, your story may have the protagonist encounter a character who asks them if they know how to dance. If the reader says they do, later in the story you may present them with a branch where they dance. If they answer that they do not dance, you can present them with a different branch.

You could even do things like use variables to keep track of items that the reader has picked up along the way. Later, if they possess certain items, you can present a branch to them. For instance, in one room, you may have the reader choose to pick up a key. If they pick up the key, later on in the story you can present with an option to use the key.

The two main things you need to know about variables are how to 'set' them and how to 'test' them to see if they are set using conditional statements. We'll cover these topics in the next sections.

II. Setting Variables

To set a variable, you start a line with a forward slash '/' and follow it with an assignment statement, like 'opened_crypt = true'.

You are standing in a room.
-
There is a crypt here.
{ Open the crypt

  You open it!
  / open_crypt = true
}
{ Leave it alone }

Variables must start with a letter from A to Z (they can be lowercase like 'a' or 'b') and you can use any combination of letters, numbers, and the underscore ('_') after that.

Variables are usually set within branches, but you can set them in any bit of text.

You see a key on the ground.
{ Pick up the key.

  You picked it up.
  / have_key = true
}
{ Don't bother. }

You keep walking along the path.

The right-hand side of the assignment statement can be any arithmetic evaluation of whole numbers (no fractions!) and can make use of other variables. You may also use 'true' or 'false' as values.

Here are some more examples.
/ undead = true
/ bar = 5
/ foo = 10
/ found_crystal_1234 = bar * foo

Let's see what happens now...
found_crystal_1234 == 50 ?: Yes, that value is 50 since we did the math.
foo - 10 == 0 ?: This is also true since foo was 10 and subtracting 10 from 10 gives us zero
bar + 1 == 6 ?: This is also true
undead == false ?: This is not true since undead == true

Finally, if you reference a variable that was never set to a value, it will get evaluated as 'false' or '0'.

You're in the green room.
picked_up_key ?: You have a key in your hand.
!picked_up_key ?: There is a key here.
!picked_up_key ?: { Pick up the key

  You picked it up
  / picked_up_key = true
}
!picked_up_key ?: { Leave the key alone }

You consider what to do next...
=> main

In the above example, picked_up_key is used but is not yet set until the line after "You picked it up". It will simply get evaluated as false, so the statement "You have a key in your hand" doesn't show up until you pick up the key.

III. Conditional Text and Branches

Of course, after you've set a variable, you will want to test it. You do this simply by prefixing a line of text, a branch, or a redirect with a conditional statement followed by the two special characters '?:'. See below.

You see a red stapler on the ground.
{ Pick up the stapler.

  You picked it up.
  / have_stapler = true
}
{ Don't bother. }

You keep walking along the path.

You encounter a man with glasses. 
-
have_stapler == true ?: I believe you have my stapler.
have_stapler == false ?: Have you seen my stapler anywhere?
have_stapler == false ?: { I saw it back down the road.

   Oh, thank you!
}
have_stapler == true ?: { Nope, I haven't see it.

   You lie to the man.
   -
   "Oh, ok. It's red. If you see it, let me know."
}

You can use any logical statement as a condition. If you know any programming already, the conditional statements are just like those in C-based languages. You can use logical operators !, &&, ||, ==, <, <=, >, >=, !=, and parentheses. The example above can be re-written without the == true since 'have_stapler' on its own can evaluate to true or false. You can also use the ! operator to invert the boolean value.

You see a red stapler on the ground.
{ Pick up the stapler.

  You picked it up.
  / have_stapler = true
}
{ Don't bother. }

You keep walking along the path.

You encounter a man with glasses. 
have_stapler ?: I believe you have my stapler.
!have_stapler ?: Have you seen my stapler anywhere?
!have_stapler ?: { I saw it back down the road.

   Oh, thank you!
}
have_stapler ?: { Nope, I haven't see it.

   You lie to the man. "Oh, ok. It's red. If you see it, let me know."
}

Conditional Redirects

You may also prefix redirects with a conditional statement. The first redirect that has a true conditional statement before it (or no conditional statement) will get executed.

You are in the main section.
/ red = true

We check to see if red or blue is true to redirect
blue ?: => blue
red ?: => red
=> yellow

[red]
You are in the red section.

[blue]
You are in the blue section.
This will not get executed since blue was not set to true.

[yellow]
You are in the yellow section.
This will not get executed since we red gets evaluated true first.

Try editing the story above but instead of 'red = true', try 'blue = true'. You'll see that you are redirected to the blue section. Also, see what happens if you move the "=> yellow" line above all the other redirect lines.