Setting up Variables in Ren’Py

Today I’m going to take you through how to set up Variables in Ren’Py and what they would be used for in Interactive Fiction or Visual Novel games.

What is a variable?

A variable is basically just a way to store information or value as it is called in coding.

Some examples of that are Player Names, Pronouns, Stats, Health, Inventory. Honestly, the list is endless and is only limited by your imagination.

You want a counter for every time your player sees a puppy. You can do that haha.

There are 3 main types of Variables I’ve found that come up the most in coding IF games.

  • Boolean Values
  • Number Values
  • String Values

#Types of Variables
#Variables have to unique 
menu choices:
    "Boolean Values": 
        $ boolean_name = "True"
        #Boolean values are True or False values. 
        #This is great for any value you need a yes/no.
        #Such as did they pick up the book = True/False.
        "You can add more text here to continue the scene"
        "This will be displayed before you Jump to the next label"
        jump new_label
    "Number Values":
        $ number_names -= 1 
        #can be - or +
        #can be any number value you want += 5 for example.
        #This is great for meters such as romance, health or skills.
        "Choice content goes here"
        jump new_label
    "String Values":
        $ string_name = "string"
        #String are words - such as player name or eye color.
        #You can make strings any words you want.
        #This is great for MC Customisation for example.
        "Choice Content goes here"
        jump new_label
#This space is not required. 
#I do it to help seperate my sections for editing.
label new_label: 

Things that are super important to remember. Variables have to be unique.

This is because if you use the same variable name twice it will overwrite the input data to the new data.

This works great if you want to add increases in Stat’s or decreases in health as it updates it but not for things like the Player Name for example.

Steps

1. Create the Variable

You can set this up at the top of your main script.rpy Page or in a separate variables.rpy

I’m creating an IF Template that will come with a variables.rpy included for you to edit.

Formula for True/False (Boolean)
default variable_name = False

Word Variables (String)

You can set up Word Variables as False if you prefer it’s up to you. I used to do false simply to save time when I writing a bunch of new variables however now that I am writing a lot more variables I’ve switched to None as I can tell at a glance what type of variable it is.

default variable_name = None
or
default variable_name = False

Formula for Number Variables
default variable_name = 0


#Variables
default boolean_name = False
default number_names = 0
default string_name = None

IMPORTANT – Ren’Py will give you an error when it tries to run if it can’t find a variable. So it must be created first.

Default!!

2. Make the menu for the variable

If you want to see how to make a menu in Ren’Py please take a look at my Coding Choice Examples Post. I will be looking at doing a more in-depth post on Ren’Py in the future as well as having a few example menus in the template.


menu newmenu:
    "This is going to be a menu":
        "And this is the text that will come after that choice."
        jump new_label


3. Set the variable in the menu.

This is done by writing

$ variable_name = “value”


menu newmenu:
    "This is going to be a menu":
        "And this is the text that will come after that choice."
		$ string_name = "words"
        jump new_label

And that’s it Voila! Your Variable is created and set, when the player chooses this menu choice it will set the string_name variable to words.

Want to display your variable? Simply type

[variable_name]

So if we wrote

“Tell me the [string_name].”

The player would see the

Tell me the words. I will show you different ways to display your variable soon.

Wait whats the difference between the Variables?

I will be going into more detail in a future post about variables but at the moment you can take a quick look at the top picture for a brief explanation.

Variables are super important to Interactive Fiction so it’s great that Ren’Py makes it so easy to code.

Hopefully this post made sense and gave you a basic idea about Setting Up Variables in Ren’Py. If you are an IF Dev and have been inspired to use Ren’Py for you IF Game let me know. I’m trying to put together some examples of games so people can see how versatile Ren’Py is.