What would you like to learn next?

Conditional Text with If Statements

Chapter 3 by gene.sis gene.sis

Conditional text with if statements

An if statement lets an author show different text in the same chapter. The reader sees only the part that matches their reader choices or current game.

Start with if and endif

This example checks whether the game variable named score is at least 5:

{if game:score >= 5}
The gate opens.
{endif}

When score is 5 or higher, the reader sees “The gate opens.” When the condition fails, that sentence is left out.

Every {if ...} needs a matching {endif}. Text between the two tags is shown only when the condition passes.

Add an alternative with else

Use {else} when the reader should always see one of two results:

{if game:score >= 5}
The gate opens.
{else}
The gate stays shut.
{endif}

An if statement can have only one {else}, and it must come after all other checks.

Check more choices with elseif

Use {elseif ...} or {else if ...} to add more possibilities:

{if game:reputation >= 10}
The guard welcomes you.
{elseif game:reputation >= 5}
The guard lets you pass.
{else}
The guard blocks the road.
{endif}

CHYOA uses the first matching branch and skips the rest. In this example, a reputation of 12 shows only “The guard welcomes you.” Put the most specific or highest requirement first.

Reader values and game values

Use reader: for a value chosen through Customize and game: for a value stored in the current game.

{if reader:name = "Alex"}
Welcome back, Alex.
{endif}

Text and Dropdown values should be placed in quotation marks. Whole numbers and the Yes/No values true and false do not need quotation marks.

Text and Dropdown comparisons match the complete value exactly, including capitalization. For example, {if game:route = "Forest"}...{endif} does not match a value of forest.

The value on the right may also be another variable. These are all useful comparisons:

  • game:coins >= game:price
  • reader:favorite = game:chosen_color
  • game:has_key = game:door_requires_key

Number, Percent, and Age can be compared with one another. Text and Dropdown can be compared with one another. Yes/No can be compared only with Yes/No.

Comparison symbols

Operator Meaning
= (also ==) is equal to
!= is not equal to
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to

Require more than one thing

Use AND when both checks must pass. Use OR when either check may pass. Parentheses make longer conditions easier to understand:

{if game:score >= 5 AND (game:key = true OR game:lockpick = true)}...{endif}

This shows the text when the reader has a score of at least 5 and also has either the key or a lockpick.

The symbolic forms && and || mean the same as AND and OR:

{if game:has_key = true && (game:strength > 5 || game:lockpick = true)}...{endif}

AND is checked before OR, but using parentheses is recommended because it makes the intended grouping clear.

Check whether a reader changed a value

{if reader:name} is a useful shortcut. It passes only when the reader saved a value different from the story's default for name.

It does not simply ask whether the current value contains text. To check the current value itself, use a comparison:

{if reader:name != ""}
A name is currently available.
{endif}

This shortcut is only for reader variables. Game variables need a complete comparison.

Missing values

If either variable in a comparison is missing, the comparison fails. This also applies to !=. For example, a missing game:key does not make {if game:key != true} pass.

Game checks also fail before the reader starts a game. Reader checks can still work because Customize is separate from the saved game.

Always copy the exact capitalization from the Story variables page. A differently capitalized name may not find the intended value.

Choose text for readers without an active game

Add @ immediately after if or elseif to choose the text that should appear when no game is active:

{if@ game:is_night = true}The room is dark.{else}Sunlight fills the room.{endif}

With an active game, the condition works normally. Without one, the branch marked with @ is chosen.

An elseif@ can be used when another branch should remain first during play:

{if game:has_key = true}
The door opens.
{elseif@ game:has_key = false}
You need a key to open the door.
{else}
The lock looks unfamiliar.
{endif}

Mark only the one branch that should be used without a game. The @ does not create a missing value and does not affect readers who have an active game.

Nesting and line breaks

An if statement may contain another if statement. This is called nesting:

{if game:has_coat = true}
You put on your coat.
{if reader:favorite_color = "blue"}
It is your favorite shade of blue.
{endif}
{endif}

Check each opening {if ...} against its own {endif}. Keeping nested parts on separate lines makes mistakes easier to spot.

Tags may contain ordinary spaces and may be split across lines. A line break outside a tag remains part of the visible chapter. Keeping each opening tag on one line is usually easiest; if you split one, preview the paragraph to catch doubled spaces, missing spaces, or punctuation left behind when a branch is hidden.

When writing chapter text in Markdown, put a tag in backticks or a fenced code block to show it as an example instead of using it. Tags in Markdown code formatting remain visible as written.

Older chapters

Older conditions may leave out game:. For example, {if Temperature <= 50}...{endif} still reads the game value named Temperature. Use {if game:Temperature <= 50}...{endif} in new chapters so its meaning is clear while editing.

Before publishing

Preview at least one example of every result:

  1. each {if} and {elseif} branch;
  2. the {else} branch;
  3. a missing game value;
  4. a reader without an active game;
  5. every nested branch.

Also read the finished sentences, not only the conditions. Hidden text can leave awkward spaces or punctuation even when the correct branch was chosen.

Related Guide chapters

Start your own immersive adult AI roleplay story
Ad

You've reached the end of this Guide topic.

  • No further chapters
Back Start Over View Story Map

34 comments