Variables and Logic in Chapbook
Adding interactivity and state to your stories.
A passage in Chapbook has a vars section at the top that is
separated from the text by a line with just two dashes. To assign a value
to a variable, you write the variable name, followed by a colon
(:), followed by the value.
Values can be numbers, strings (written like Python strings), and
booleans, which are true or false. Here is the
Chapbook version of the beginning of the Island Adventure:
score: 0 apple_present: true -- # **Island** Adventure [[Let's Go!->South Beach]]
Variables can only get assigned values in the top section, but they can be printed out in the bottom section. In the code for the Pit in Island Adventure, you can see how the score is modified and then displayed:
score: score - 3
--
You fell into a very deep pit. You have died.
Game over. Score is {score}.
You can insert modifiers in your text to control the alignment. Modifiers are in single square brackets and are not visible to the player. For example:
As you keep walking down the street, the buildings become more run down. You finally reach a small wooden building with a screen door through which the most enticing odors are wafting towards you. There is a little sign on the door that says, "Come In." Above the door is a large sign that reads, [align center] CAVE CANEM [continue] > [[Go in]] > [[Give up and go home]]
The align modifier changes the horizontal alignment of the
text. You can use left, center, or
right. The continue modifier ends the effect of
the previous modifier.
You can conditionally display text based on a Boolean value using the
if modifier. For example, in this code, if the
hasKey variable is true, one set of text is
displayed; otherwise, something different is displayed:
You reach the elevator and look for a button, but all you see is a brass plate with a hexagon-shaped hole. [if hasKey] The hole is the perfect size for the hexagon-shaped key that the robot gave you. [[Insert key]] [else] > [[Kick the elevator]] > [[Give up and go home]]
You can use comparison operators like in Python, except that the equals operator is three equal signs, ===, and the not-equals operator is !==. Here are some examples:
Why three equal signs? Chapbook is based on JavaScript, which has both loose equality (==) and strict equality (===). Experience has shown that loose equality can cause bugs, so everyone uses strict equality now.
The after modifier creates a time delay. Each time value is
measured from when the passage has begun.
You approach the robot and say, "Good morning!" The robot just stares at your feet. [after 5 seconds] The robot looks up, stares into your eyes with an expression of intense sadness and heaves a big sigh. [after 10 seconds] He then says, "that's the most depressing thing anyone has ever said to me... [after 15 seconds] Never mind. I'm sure you don't care how I feel. The only thing you care about is getting to the end of your story. I can just stand here all day and rust, and no one cares. [after 20 seconds] You will need a key if you are going to use the elevator. Here."
Here is a sample story that uses some of these capabilities.
Try adding some variables and modifiers to your story.