documentation index

Remembering User Choices

Changing Game Flow Based on Earlier Choices

You can remember user choices by assigning to variables. For example:

menu:
    "Buy her an Iguana":
        $ gift = "iguana"
        b "I'll take an iguana, my good man!"
    "Buy her a Corn Snake":
        $ gift = "corn snake"
        b "I'll take a Corn Snake, my good man!"
    "Buy her a tortoise":
        $ gift = "tortoise"
        b "Give me your slowest tortoise, please!"

Please note that a single "=" is used to assign a variable.

Later in the game, you can then use the if statement to change the script based on the previous choice:

b "Open it, Mary."
if gift == "iguana":
    m "Oh Bob, I've always wanted an iguana!"
if gift == "corn snake":
    m "Oh Bob, I needed a corn snake that matches my red dress! Thank you so much!"
if gift == "tortoise"
    m "Bob! It's so slow! I love you!"

Please note that the double "==" is used to test for equality.

You can call your variables anything that you like, as long as it doesn't conflict with keywords or character objects. The only rule with variables is that you have to assign to a variable before you can use it in a conditional.

Implementing a Point-Based Game

A point-based game is one in which the choices that a user makes at menus cumulatively change the story after many of them have been made.

First, you'll need to decide what you want to be able to gain points for. For example, you might want the user to be able to win points by (1) building big muscles (strength) (2) learning to juggle large numbers of objects (dexterity) and (3) performing chemical experiments and creating increasingly advanced forms of life (mad science).

Next, you'll need to initialize the variables that you're going to use to keep track of these points. You do this immediately after the start label:

label start:
    $ strength_points = 0
    $ dexterity_points = 0
    $ mad_science_points = 0

During the game, you'll present the user with choices that affect these points, and then modify the variables based on the choices. To increase the points, use:

$ variable += 1

(That's plus followed by equals.) You can use any number, it doesn't have to be 1. To decrease the points:

$ variable -= 1

(That's minus followed by equals.) You can again use any number, not just 1.

For example:

menu:
    "Go to the gym":
        $ strength_points += 1
        "The gym was hot and sweaty."
        "I picked up heavy things, then put them down again."
        "I always wonder why I bother, since I could achieve
         the same thing by not picking them up in the first place."
    "Practice juggling balls":
        $ dexterity_points += 1
        "I practiced juggling seven balls."
        "It was hard, but I finally managed to get it at least once."
    "Practice juggling angry porcupines":
        $ dexterity_points += 3
        $ strength_points -= 1
        "Practicing with angry porcupines really sharpened my reflexes.
         Unfortunately, I'm feeling weak from blood loss."
    "Invent a happy porcupine":
        $ mad_science_points += 1
        "Unfortunately I was only able to create a melancholy echidna,
         but I learned a lot and I was sure that next time would be better."

(You can of course customize the responses based on the current points, using the techniques outlined in the earlier section on variables.)

Finally, customize your ending using the variables:

if strength_points > max(dexterity_points, mad_science_points):

    "You pick up a cow and jump over the moon."

elif dexterity_points > max(strength_points, mad_science_points):

    "You juggle 215 balls and set the world record."

else:

    "You create the perfect companion and retire with it to the castle
     built by the army of servile eight-armed giant gorilla-men
     you created earlier in the day."

return

The above code decides ties in favor of the last option (mad science). If you would like to be explicit about how to handle ties, it's more complicated, as there are seven possible conditions, but still quite doable. It looks like this:

if strength_points > max(dexterity_points, mad_science_points):

    "You pick up a cow and jump over the moon."

elif dexterity_points > max(strength_points, mad_science_points):

    "You juggle 215 balls and set the world record."

elif mad_science_points > max(strength_points, dexterity_points):

    "You create the perfect companion and retire with it to the castle
     built by the army of servile eight-armed giant gorilla-men
     you created earlier in the day."

elif strength_points == dexterity_points == mad_science_points:

    "Your band of servile sixteen-armed giant gorilla-men built you
     three castles, which you juggle to impress the perfect companion
     you created earlier in the day."

elif strength_points == dexterity_points:

    "You juggle three large boulders and many attractive people of
     the opposite sex swoon."

elif dexterity_points == mad_science_points:

    "You invent a species of round humming bird, and juggle 5,000
     of them at once."

elif strength_points == mad_science_points:

    "You invent a 30 ton 24-armed gorilla man, and then pick him up
     so that he can paint the top part of the house he's building
     for you."

return

The ordering of the conditions above is important, because later alternatives assume that previous ones have been false. In general, you want to first test if any single variable wins, then test if they're all the same, then test to see which ones tied. This strategy avoids several possible pitfalls in the game logic.


Previous:
Giving the User a Choice With Menus
Ren'Py Web Tutorial Next:
Branching & Recombining the Story

documentation index