Ren'Py Variables and Conditions: How to Create Affection Points and Story Flags

In the previous post, we looked at how to create choices and branching stories in Ren'Py. But what if you want your choices to actually affect the story?

For example, maybe the mc can gain affection points with different characters, unlock hidden scenes, or discover certain items that change later dialogue.

This is where variables and conditions come in.

So today we'll go through how to create variables, change variable values, use conditions with if, elif and else, and build a simple affection point system.

If you haven't read the previous tutorial yet, you can check it out here:

Let's get started!

1. What Are Variables in Ren'Py?

A variable is basically a way to store information while your game is running.

For example, you might want to keep track of:

  • How many affection points the player has with a character
  • Whether the player has found a particular item
  • Which route the player has chosen
  • Whether a certain event has happened
  • Which ending the player has unlocked

Ren'Py uses Python variables for this, so you don't need to learn a completely new system.

Let's start with a simple example.

default affection = 0

This creates a variable called affection and gives it an initial value of 0.

You can then change its value during the game:

$ affection += 1

This adds 1 to the current value.

You can also subtract points:

$ affection -= 1

Or set a completely new value:

$ affection = 5

The $ symbol tells Ren'Py to execute a Python statement.

2. Using default to Initialize Variables

You might be wondering why we're using default instead of simply writing:

$ affection = 0

The difference is important.

In Ren'Py, default is used to initialize variables when a new game starts. It also works properly with Ren'Py's save and rollback systems.

For example:

default affection = 0
default found_letter = False
default met_eileen = False

Here, we have three variables:

  • affection stores a number.
  • found_letter stores whether the player has found a letter.
  • met_eileen stores whether the player has met Eileen.

Variables can store different types of values:

default affection = 0
default player_name = "Alex"
default found_key = False

Some common Python data types are:

TypeExampleWhat it stores
Integer10Whole numbers
Float2.5Decimal numbers
String"Hello"Text
BooleanTrue / FalseTrue or false values

A small but important note: Use default for normal gameplay variables. Don't use define for values that change during the story. define is generally intended for constants and character definitions.

3. Changing Variables During the Story

Now let's actually use these variables in a scene.

Imagine the protagonist meets Eileen and gets the chance to help her.

default eileen_affection = 0

label start:

    e "Could you help me carry these books?"

    menu:
        "Of course!":

            $ eileen_affection += 1

            e "Thank you! That's really kind of you."

        "Sorry, I'm busy.":

            e "Oh, that's alright."

    return

Depending on the player's choice, Eileen's affection points will either increase or stay the same.

You can also modify multiple variables in one scene:

$ eileen_affection += 1
$ found_letter = True
$ met_eileen = True

This is useful when one event affects several parts of your story.

For example, finding a letter might unlock a later conversation, while helping Eileen increases her affection.

4. Using if, elif and else

Now that we have variables, we need a way to check their values.

This is what conditional statements are for.

The if statement

An if statement checks whether a condition is true.

if eileen_affection >= 3:
    e "I'm really glad we became friends."

If Eileen's affection points are 3 or higher, the dialogue will appear.

Otherwise, Ren'Py simply skips it.

The else statement

What if we want different dialogue when the condition isn't met?

We can use else:

if eileen_affection >= 3:

    e "I'm really glad we became friends."

else:

    e "I hope we can get to know each other better."

Now the game has two possible outcomes.

The elif statement

You can also check multiple conditions using elif.

For example:

if eileen_affection >= 5:

    e "You're one of my favorite people."

elif eileen_affection >= 3:

    e "I really enjoy spending time with you."

else:

    e "We should hang out more often."

Ren'Py checks the conditions from top to bottom. Once one condition is true, the remaining branches are skipped.

This is particularly useful when you want different dialogue depending on how much affection the player has accumulated.

5. Understanding Comparison Operators

When writing conditions, you'll probably use these operators quite often.

OperatorMeaningExample
==Equal toaffection == 5
!=Not equal toaffection != 0
>Greater thanaffection > 3
<Less thanaffection < 3
>=Greater than or equal toaffection >= 5
<=Less than or equal toaffection <= 5

One thing to remember: = and == are not the same.

$ affection = 5

This assigns the value 5 to the variable.

if affection == 5:

This checks whether the variable is equal to 5.

It's a small difference, but mixing them up is a common beginner mistake.

6. Combining Multiple Conditions

Sometimes you need to check more than one thing at the same time.

Python provides three useful operators for this: and, or and not.

Using and

Both conditions must be true.

if eileen_affection >= 5 and found_letter == True:

    e "I can't believe you kept that letter!"

The dialogue only appears if the player has at least 5 affection points AND has found the letter.

Using or

At least one condition must be true.

if eileen_affection >= 5 or mika_affection >= 5:

    "Someone seems to really like you."

This dialogue appears if either character has at least 5 affection points.

Using not

This checks whether something is false.

if not found_letter:

    "You haven't found the letter yet."

This is a convenient way to check whether a Boolean variable is still False.

You can use these operators together to create more complex conditions as your story grows.

7. Creating Story Flags

Affection points aren't the only things you can track.

In visual novels, it's also common to use something called a story flag.

A story flag is usually a Boolean variable that records whether a particular event has happened.

For example:

default found_letter = False
default opened_secret_door = False
default discovered_secret = False

When the player finds the letter, simply change the variable:

$ found_letter = True

Later in the story, you can check it:

if found_letter:

    "You take the letter out of your bag."

    e "Wait... where did you get that?"

You can also use story flags to unlock hidden scenes:

if found_letter and opened_secret_door:

    "You discover a hidden room behind the door."

This makes it possible to create events that depend on the player's previous actions, rather than just their most recent choice.

8. Using persistent Variables to Remember Progress

There is another type of variable worth knowing about: persistent.

Normal gameplay variables are stored as part of the current game state. They can be saved, loaded and rolled back.

But what if you want the game to remember something even after the player starts a completely new game?

For example:

  • Whether the player has unlocked a certain ending
  • Whether a CG has been viewed
  • Whether a bonus scene has been discovered
  • Whether a special achievement has been completed

This is where persistent variables are useful.

You can initialize one like this:

default persistent.unlocked_secret_ending = False

Then, when the player reaches the secret ending:

$ persistent.unlocked_secret_ending = True

Later, you can check whether it has been unlocked:

if persistent.unlocked_secret_ending:

    "The secret ending is already unlocked."

Unlike normal gameplay variables, persistent data is stored separately from individual save files. It remains available across playthroughs and new games.

Important: Don't use persistent for ordinary affection points or temporary story progress. It is intended for information that should remain unlocked across different playthroughs.

9. Complete Example: A Simple Affection System

Let's put everything together!

We'll create a small scene where the player meets Eileen, makes a few choices, and eventually gets different dialogue depending on their affection points.

First, initialize the variables:

default eileen_affection = 0
default found_letter = False

Then create the scene:

define e = Character("Eileen")

label start:

    scene bg classroom

    "After class, you notice Eileen sitting alone."

    e "Oh, hey! Are you heading home?"

    menu:
        "Ask if she wants to walk together.":

            $ eileen_affection += 2

            e "Really? I'd love that!"

        "Just say goodbye.":

            e "See you tomorrow!"

    "The next day, you find a letter on your desk."

    menu:
        "Read the letter.":

            $ found_letter = True

            "You open the letter and recognize Eileen's handwriting."

        "Leave it alone.":

            "You decide not to open it."

    if eileen_affection >= 2 and found_letter:

        e "You read my letter? I was hoping you'd find it..."

        "She smiles shyly."

    elif eileen_affection >= 2:

        e "I wanted to tell you something yesterday..."

    elif found_letter:

        e "Oh, you found my letter."

        "She looks a little embarrassed."

    else:

        "The day continues as usual."

    return

Depending on the player's decisions, the scene will play out differently.

You can expand this system with more characters, additional story flags, hidden scenes and multiple endings.

And that's really the fun part of using variables: once you understand the basics, you can create much more interactive stories without having to write completely separate scenes for every possible choice.

What's Next?

Now you know how to use variables and conditions to make your visual novel more interactive!

We've covered the basics of affection points, story flags, conditional dialogue and persistent progress. These are some of the most useful tools you'll work with when creating a visual novel with multiple routes and endings.

In the next tutorial, we'll look at how to add background music, sound effects and voice audio to your Ren'Py game.

And if you're working on a visual novel and need help with custom UI, scripting or implementing specific game features, you can also check out YoruBox. We're a small indie game development team, and we enjoy helping other creators bring their projects to life!

yorubox.team
yorubox.team
Articles: 13

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newsletter
This field is required.