- Jun 14, 2018
- 1,611
- 2,260
Newbie guide to basic variables in RenPy
RenPy is very flexible. Way more flexible than most of us will ever need.
But everyone has to start somewhere... and whilst the
(though I will say, the "
Except, when you're just starting to write your first RenPy game... you don't need to know everything... just enough to get you started.
I hope that is what this guide does... explain "just" enough... about the basics of variables.
I am not a RenPy expert. If you're reading this guide for help... chances are I'm just a page and a half ahead of you in the manual.
So... onward...
At our level of programming, there are two "types" of variables.
Normal variables are saved by autosaves and saves, etc. And when you load your save game, their values are loaded too.
Static variables aren't saved. They aren't ever expected to change either. RenPy sets them each and every time your game runs.
For the pedants out there... Yes, I know "normal" and "static" aren't the real names. And yes, I know there are a lot more variations than just these two "types". But for the sake of an introduction... this is where I'm drawing the line.
The "$ {variable} = {value}" is an example of what I'm calling a normal variable. There are other ways to set them up, but $ is as good a way as any. More about the other ways later...
the "define a = Character("Angela")" would be an example of what I'm calling static variable. In this case, the variable would be "a".
RenPy has a strongly hinted at rule that static variables should never be changed by the code. They should be set once and left alone. You can change them, and examples I've seen do work. But the documentation says it isn't guaranteed to work and bad things might happen.
It's simpler to just not think of "define"s as variables. They are defines.
Okay, back to "normal" variables.
There are 3 main variable types. (again, there are more... just don't worry about it).
Boolean... A simple Yes/No. Though for historical computer reasons, the values are True and False (actually 1 and 0).
As you can see, there are lots of ways you can check a boolean variables. "is True" works, "== True" too. But if you omit either, it'll still assume you are checking if it's true.
Also note that True and False are case sensitive. True is not the same as true.
Integer... A simple number.
String... Just text. Letters, numbers, punctuation... pretty much anything you can type.
Boolean is (almost) always True / False.
Integer is any number without quotes (") around it.
Strings are any text within quotes (").
RenPy sets the variable type when it first sets it's value.
It also gets a bit upset if you start mixing things up a bit. (Like trying to add 1 to a string, or some other such nonsense).
One thing to note is that the dollar sign ($) is not "set the value". It's effectively a shortcut for "python:". Generally speaking, if you're using 1 or 2 lines of python, you'd use $. If it's more lines that that, it's probably easier to use "python:".
The final thing I will mention is "default".
One problem with setting variables as you go along is that if you ever add new variables into existing code, there is a chance a player might load a save game where that new variable doesn't exist.
So you have some code that looks like this...
And sometime later you change it to this instead...
Now. If your player saves at "Hi, my name is Angela." - everything is fine. Because even though the player loads AFTER the "$ kissed_angela = 0", (I think) the "+= 1" will also create the variable.
But. If your player saves at "I'm so happy to meet you.". Any player who saved using the old version and loaded using the new version will encounter an error when it reaches "if kissed_angela > 0:" because kissed_angela doesn't exist.
The solution to this is the "default" statement.
It works like "define", in so much as it sets everything up before the main game even starts running.
It doesn't matter where they are in the code, RenPy scans everything looking for things like define/default and sets all their values before (almost) anything else happens.
So things would look like this instead...
So when your game first starts, kissed_angela is always 0. Even if the player is already past the place in the code where you added it.
If the player loads a save game that doesn't have the variable in... it doesn't matter, because the value has already been set to 0.
If the player loads a save game that DOES have the variable in... it just overwrites the default value with the value from the save file.
default can be used for any variable type. So "default seen_angela_naked = False" is just a valid as any other default line.
I know that's a lot of text to read. I could have left out the bit about default. Most games don't use it and still manage to work just fine... but I think it's safer and clearer to read if you use it.
More reading about variables:
RenPy is very flexible. Way more flexible than most of us will ever need.
But everyone has to start somewhere... and whilst the
You must be registered to see the links
is great... it also tends to explain everything.(though I will say, the "
You must be registered to see the links
" section of the documentation is very good for a newbie like me).Except, when you're just starting to write your first RenPy game... you don't need to know everything... just enough to get you started.
I hope that is what this guide does... explain "just" enough... about the basics of variables.
I am not a RenPy expert. If you're reading this guide for help... chances are I'm just a page and a half ahead of you in the manual.
So... onward...
At our level of programming, there are two "types" of variables.
Normal variables are saved by autosaves and saves, etc. And when you load your save game, their values are loaded too.
Static variables aren't saved. They aren't ever expected to change either. RenPy sets them each and every time your game runs.
For the pedants out there... Yes, I know "normal" and "static" aren't the real names. And yes, I know there are a lot more variations than just these two "types". But for the sake of an introduction... this is where I'm drawing the line.
The "$ {variable} = {value}" is an example of what I'm calling a normal variable. There are other ways to set them up, but $ is as good a way as any. More about the other ways later...
the "define a = Character("Angela")" would be an example of what I'm calling static variable. In this case, the variable would be "a".
RenPy has a strongly hinted at rule that static variables should never be changed by the code. They should be set once and left alone. You can change them, and examples I've seen do work. But the documentation says it isn't guaranteed to work and bad things might happen.
It's simpler to just not think of "define"s as variables. They are defines.
Okay, back to "normal" variables.
There are 3 main variable types. (again, there are more... just don't worry about it).
Boolean... A simple Yes/No. Though for historical computer reasons, the values are True and False (actually 1 and 0).
Python:
$ saw_angela_naked = True
# comment
if saw_angela_naked == True:
a "I saw you... you know... watching me."
# another comment
if saw_angela_naked:
a "I still haven't forgotten that you saw me naked."
if saw_angela_naked is True:
a "another line of dialogue."
if not saw_angela_naked:
a "You are a real gentleman sometimes."
if saw_angela_naked is False:
a "and I love you for it."
Also note that True and False are case sensitive. True is not the same as true.
Integer... A simple number.
Python:
$ kissed_angela = 0
# comment... add 1
$ kissed_angela += 1
# comment... subtract 3
$ kissed_angela -= 3
if kissed_angela > 5:
jump day3_whoopie_time
else:
jump day3_blown_out
String... Just text. Letters, numbers, punctuation... pretty much anything you can type.
Python:
# okay, maybe a bit over the top for an example....
python:
pcname = renpy.input("What is your name? {i}(Press ENTER to use the default name 'Simon'){/i}", length=20, exclude=" 0123456789+=,.?!<>{}[]'\"\%")
pcname = pcname.strip()
if not pcname:
pcname = "Simon"
Integer is any number without quotes (") around it.
Strings are any text within quotes (").
RenPy sets the variable type when it first sets it's value.
It also gets a bit upset if you start mixing things up a bit. (Like trying to add 1 to a string, or some other such nonsense).
One thing to note is that the dollar sign ($) is not "set the value". It's effectively a shortcut for "python:". Generally speaking, if you're using 1 or 2 lines of python, you'd use $. If it's more lines that that, it's probably easier to use "python:".
The final thing I will mention is "default".
One problem with setting variables as you go along is that if you ever add new variables into existing code, there is a chance a player might load a save game where that new variable doesn't exist.
So you have some code that looks like this...
Python:
label start:
a "Hi, my name is Angela."
a "I'm so happy to meet you."
a "Let's get on with the game."
"<< END >>"
Python:
label start:
$ kissed_angela = 0
a "Hi, my name is Angela."
a "Let me start by giving you a kiss."
$ kissed_angela += 1
a "I'm so happy to meet you."
a "Let's get on with the game."
if kissed_angela > 0:
a "thank you for the kiss."
"<< END >>"
But. If your player saves at "I'm so happy to meet you.". Any player who saved using the old version and loaded using the new version will encounter an error when it reaches "if kissed_angela > 0:" because kissed_angela doesn't exist.
The solution to this is the "default" statement.
It works like "define", in so much as it sets everything up before the main game even starts running.
It doesn't matter where they are in the code, RenPy scans everything looking for things like define/default and sets all their values before (almost) anything else happens.
So things would look like this instead...
Python:
default kissed_angela = 0
label start:
a "Hi, my name is Angela."
a "Let me start by giving you a kiss."
$ kissed_angela += 1
a "I'm so happy to meet you."
a "Let's get on with the game."
if kissed_angela > 0:
a "thank you for the kiss."
"<< END >>"
If the player loads a save game that doesn't have the variable in... it doesn't matter, because the value has already been set to 0.
If the player loads a save game that DOES have the variable in... it just overwrites the default value with the value from the save file.
default can be used for any variable type. So "default seen_angela_naked = False" is just a valid as any other default line.
I know that's a lot of text to read. I could have left out the bit about default. Most games don't use it and still manage to work just fine... but I think it's safer and clearer to read if you use it.
More reading about variables:
You must be registered to see the links
You must be registered to see the links
You must be registered to see the links