Basics of Ren'Py #1
Labels, Dialogue, Calling and Returning
If you're making a simple visual novel with only some characters, backgrounds, music and a storyline to follow, you will be spending the vast majority of your time working inside labels. It is one of the most important features of Ren'Py, and in these tutorials, we'll be going over the basics of what you can do with it.
To start, I'd recommend creating an empty project, deleting the contents of script.rpy, and using that project (and/or file) as a guinea pig for all that we'll do here. Putting it into any .rpy file inside project's game folder will do, just make sure there aren't any conflicts across files, like two different start labels. Speaking of labels, let's dive into learning about them.
label start(): Welcome to our game!"
This is what a very simple label can look like. The first line starts with the label statement, followed by the label name, parentheses, and a colon. Parentheses are there for arguments (a slightly more advanced feature), and while they are optional, Ren'Py adds them behind the scenes if they are not included, and so I consider including them a good practice.
The colon is there to indicate that labels create a block. Block is just a fancy name for "section", a section which groups code together under a parent. The parent, in our case, is the label, and the code going into its block can be anything that labels support, ranging from dialogue lines to showing or hiding images to playing music.
The second line of code is all you need to create a dialogue line. And since we're inside the label named start, which is a label that's entered when you press the "Start" button on a project, we can try it out right away!
And that's all it takes to create a line of dialogue.
And once we see this line and move forward, we jump back to the main menu. Why? This is because we've reached the end of the label, and with that, end of the file, too.
At least if you only have the code above. In a moment, we'll learn that we can enter labels by calling them, and leave them by returning. A return statement should be included at the end of every label. Think of it like closing the door behind you. You don't have to do it, but you should, because otherwise, once the game reaches that point, it will continue to whatever code is below, either taking you places it's not supposed to, or leading to something that straight up throws an error.
So, let's add it. And let's add one more line of dialogue while we're at it, why not!
label start(): Welcome to our game!" "Lezalith" "I hope you're having a wonderful day!" return
Wait, are those two strings? Indeed. If there's only one, Ren'Py uses it for the dialogue line. However, if there are two, dialogue is taken from the second string, and the first one is used for the person speaking!
Let's go back to calling and returning. First, I will prepare a snippet of code, and then we'll go over it.
label start(): "Welcome to our game!" "Lezalith" "I hope you're having a wonderful day!" call park "Lezalith" "Thank you for being here!" return label park(): "You have entered the park." "Lezalith" "This is where I like to spend my time." return label home(): "You have entered the home." "Lezalith" "This is where I live." return
Here, we have defined three separate labels, named start, park and home. Each has their own block, all of which start at the very left of the file.
We're familiar with the bits of dialogue now, and we've seen the return statement already, which we're about to fully understand now. And we've added the call statement! So, what does this do?
I've mentioned that calling means "entering". Sticking with my door analogy, imagine walking through a house, rooms being labels. When you call a label, you enter a new room, and leave the door open for yourself. When you return, you go back to the room you came from, close the door behind you, optionally taking something with you, and continuing from where you left off in the previous room - or the previous label in our case.
If you try the code, you should see these lines:
"Welcome to our game!" "Lezalith" "I hope you're having a wonderful day!" "You have entered the park." "Lezalith" "This is where I like to spend my time." "Lezalith" "Thank you for being here!"
With all of the information above, we should be able to tell what's happening.
The game starts at the start label. It welcomes us to the game, I wish you a wonderful day, and then it calls the park label. Get into the imaginary bus, we're going to the park label everyone! And when we get there, I'll announce that you've entered the park, and say that I like to spend my time there.
But time flies fast. And so, we've reached the return statement. Where was it we came from..? Oh! The start label. So... We're returning to it. Quite literally. We pick up after the call park line, right where we left off. I will thank you for being here, and return you to the main menu.
And we never enter home. We have defined it, but we never call it, and thus we never enter it in our imaginary bus. But! One of the reasons why I included it is that you can try out what happens when you forget the return statement. Try deleting the one at the end of the park label and see what happens. With what you've learned here, you should be able to tell what's happening.
Couldn't agree with the image above more. What have we learned in this tutorial?
- What labels are
- How we can create dialogue lines, with or without someone saying them
- That we can have multiple labels
- That we can move between them with the call and return statements
And in the next one, I'll show you what Character objects are, what text tags are, and what either of them is for!