Skip to main content

How to use variables / what are variables?

Author: NingNing
Last update:2022-05-21


This tutorial is about the use of variables. If you don't know what a variable is or how to use it, keep reading~

What are variables?

  • A variable is a value stored by the system and can be changed/increased or decreased by player choice or story progress
  • It can be number, text, yes(True) / no(False)...
  • We can use variables to define many things, such as:
    • Character's name, age, occupation, etc...
    • A character's affection, dislike, etc. towards another character (it can also be an item or something)
    • Character's stats, like intelligence, stamina, abilities, etc...
    • Has the player experienced something - True/False statement

How to use variables?

So you know that variables can be used in many places.
Today I will introduce a few commonly used way.
The first thing to do is to prepare some things in advance as usual

Prepare in advance

  • The things here are not necessarily needed, but if you want to follow this tutorial, it is recommended to do it
  1. Add a new character (Char A)
  2. Prepare a background and character sprite (Char A's portrait)
  3. Define role A

The code will look something like this:

define a = Character("Char A")
image park = "images/bg/park.jpg"
image cha = "images/characters/a.png"
label start:
scene park

This completes the preparation in advance, let's start the tutorial now!

Save player data

One of the ways to use variables is to store the text entered by the player.
Today we are going to store the gender of the player.
So first, open Renpy and let's add a variable! You can use this code:

default gender = "MyGender"
  • So now you have the first variable and the gender of the player is currently unselected so I called it by any other name (MyGender) Now, We want to let the player choose the gender
    You can use this code:
"Please choose a gender"
menu:
"female":
$ gender = "female"
"male":
$ gender = "male"

Confirm Player's Choice / Display Variables

After you change the value of a variable you may want to know if the code is correct / works correctly so you will want to display the variable
You can do this --> type [] and write the name/code of the variable in [here] to display the value of the variable
So you can use something like this to display the player's choice:

"You have decided your gender"
"your gender is [gender]"

  • Gender is the codename of our variable
  • If you choose female, the word stored in the variable will become female
  • If you choose male, the word stored in the variable will become male
  • If nothing is selected (although this is unlikely) it will show MyGender because we first defined gender with default gender = "MyGender"
  • If you write default gender = "female" at the beginning, the default gender will be female

Let the player enter an answer

In addition to giving the player a few options to choose from, you can also choose to let the player answer the value they want to use
Today's example is for players to enter their own title/nickname
For example Dark Flame Master or maybe end-of-the-century magician or other title...
We can do it with code like this: First let's add a variable:

default nickname = "Dark Flame Master"
  • The Dark Flame Master above is the default title

Now let the player enter the title themselves:

python:
nickname = renpy.input("Please enter your nickname!")
nickname = nickname.strip()
  • We let players enter a title themselves, so now let's make sure the code works
"You have entered your title"
"Your nickname is [nickname]"

Character affection / other stats

Here will be a simple favorability system tutorial / other stats system tutorial
So first, let's add some variables:

$a_aff = 0
$inter = 0
  • a_aff is the favor of Char A
  • inter is the intelligence point of the character

Let's add some conversations now!

"Hello~"
a "Hello [nickname]"
"Wow you already know my name?! I know your name too"
"You are Char A!"
$a_aff += 5
a "Yes, I am a Char A"
a "How did you know my name?"
"Because I'm so smart!"
$inter += 2
"Please show the current value"
"OK"
"Char A's favorable of you is [a_aff], and your intelligence is [inter]"

You can copy and see the above code to see if it is successful~

Special Event

Sometimes you might want to make somehting like that

  • Trigger a story when the player has experienced something Or if the player hasn't passed or experienced something, they can't continue the story
    Such a function will also be presented through an example in this tutorial
    First, let's add 2 variables
$ meet = False
$ smallTalk = False

Now we have 2 variables

  • The first is to see whether the player has met a certain character
    • If you want to specify whether you have met Char A, you can also write the variable like this $ meet_A = False
  • The second sentence is to see if the player has had a small conversation with a certain character

So let's add some conversations now

"The weather is great today"
"You see a girl in front of the park, do you want to say hi to her?"
menu:
"Yes":
$ meet_A = True
call labelmeet
"The conversation is over, you say goodbye to Char A"
a "bye"
hide cha
jump label1
"No":
jump label1

The above paragraph is:

  • When Yes is selected - You will say hello to Char A, the plot will jump to labelmeet
    • Because of the use of call, it will return to this point after the labelmeet's story ends
  • When No is selected - You will leave without saying hello to Char A, the plot will jump directly to lablemeet

So we define 2 new labels here:

  • labelmeet and
  • label1 Now let's complete these 2 labels!
label labelmeet:
show cha
a "Hello Hello"
"Do you want to respond?"
menu:
"Yes":
$ smallTalk = True
"How's your day?"
a "Great"
return
"No":
return

  • If the player selected "Yes" in the previous label, they will enter the current labelmeet
  • Here the player can make another choice - to respond or not to respond to Char A
  • smallTalk will change from False -> to True if it responds to Char A
  • If you don't respond, it will return directly to the point of the previous call
label label1:
"Let's see if the code works successfully~"
if meet_A == True:
jump end
else:
"It's been 10 minutes..."
"You wandered around in the park without doing anything and went home"
jump end
  • If you chose not to say hello to Char A in label start, you will directly enter the current label1
  • If you choose to greet Char A at label start, you will enter the current label1 after going through labelmeet
  • Here we are going to jump to ending --> label end

Now let's create the label end!

label end:
if meet_A == True:
"Get achievement 1 - meeting with Char A"
else:
pass
if smallTalk == True:
"Get achievement 2 - Conversation with Char A"
else:
pass

-If the player has previously experienced meet_A and smallTalk, the achievement will be displayed separately

This post is over now~ You can give me / my website a clap if this help you! My website --> https://spaceofningningen.blogspot.com/ Image Alt Thank you for your support~