code, howto, Ren'Py

Working with Ren’Py: Part 5: Variables and Conditional Statements

1_Logo

Working with Ren’Py:

Ren’Py is a engine for creating visual novels. It comes with a suite of tools for taking code and transforming it into programs that can be run on Windows, Mac, Linux, and even, with a little more work, mobile platforms like Android and iOS.

 

Variables

In programming terminology, a variable is a container for values that may change while code is being run. In Ren’Py, variables often come in the form of “flags”. They are changed as a result of moving through a project, and frequently, but not always, either one value or another.

In Python, and many other programming languages, the more abstract idea of a “flag” being raised or lowed can be represented as a Boolean value: either it is true or it is false, but not both at the same time.

3_TheQuestion

Open the “script.rpy” file for “The Question”.

1_Default

Lines 5-10 contains the first use of a variable. The variable book is set to the value of False.

Note: In Python, Boolean values, true or false, are represented as capitalized words. Either True or False.

Line 8 establishes (initializes) the variable book so that it can be used later in the project. By creating a variable early in a project, it can be used multiple times without worrying if it exists.

2_SetValue

In the label book, line 139 uses the variable book again. In this case, instead of creating it as happened at line 8, its value is changed.

Note: To use Python code in Ren’Py, the statement must start with the dollar sign ($). This lets Ren’Py know that it should not run it directly, but treat it as Python instead of the dialect that Ren’Py uses.

Through either creating or changing its value, the value of the variable book can be manipulated throughout the project.

 

Conditional Statements

In programming terminology, a conditional statement is run based on if a condition, the result of comparing some values, is true or not.

3_If

A conditional statement appears on line 175 in “The Question”. It starts with the keyword if and then checks if what follows it is True or not. If the value is True, that is, if the variable book is set to the value True, some code will be run. Otherwise, it will not.

If the variable is set to the value True when the project reaches line 175, then line 177 will be run. Otherwise, it will not.

In “The Question”, the variable book is first set on line 8 to the value False. Then, if the player reaches the label book, it is changed to the value of True on line 139. Once the player reaches line 175, a conditional statement checks if the variable book is True or False. If it is True, line 177 is run. If it is False, line 177 is skipped.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s