This is part 1 of a 4 article series on some useful techniques for making a RenPy game. We’ll show you how to implement a simple inventory system and build an intelligent menu for a shop/vending machine that reacts and responds appropriately to player inventory and stats.
The other articles are listed below, links will be added as the articles come out.
- Stats/Inventory system
- Dynamic menus and dialog based off
- Event Triggers
- Recursive menus that continue until all options are exhausted
- Theme music
- Scrolling Credits
Stats and Inventory in RenPy
If you’re reading this article, you’re interested in the fastest, most efficient way to set up stats and/or inventory management in RenPy and how to use stats/inventory to impact the player’s experience and dialog options.
The easiest way to implement S&I (Stats and Inventory) is to simply declare a Python code block at the top of your scritp.rpy file. Here’s an example from the sample RenPy game found here:
# The script of the game goes in this file. ###custom python stuff init python:#declare a python black so we can write python code class resource:#this is a convenience class for handling resources def __init__(self, starting_amount): self.level = starting_amount def dec(self): if self.level > 0: self.level = self.level - 1 def inc(self): self.level = self.level + 1
The init python: block tells RenPy that the following lines will be python code.
I use the concept of a resource in this example which could be currency i.e. 50 gold coins, a stat i.e. 18 HP, 32 Heart, or even a single use item i.e. The Master Key. A resource is simply anything that we need to track if the character has it (or not) and how much they have.
For convenience, we create a resource class that is going to handle how accruable resources behave. In this case we defined three methods for resources.
This is a special Python method that handles how the class is instantiated. In this case, that means how we create new resources for our Player Character.
def __init__(self, starting_amount):
We can create a new resource “love” later on using the following code:
love = resource(0)
This function handles decreasing our resources. We put a simple check in the function to make sure that resource values do not go into the negative.
This function is used to increase our resource. In this case, I never wanted to increase or decrease by more than one, but if you wanted to, say if the character has a job and has just earned $100 you could use something like this:
def inc(self,amount=1): self.level = self.level + amount
where amount will default to 1 if we do not provide one.
Creating the Resources
When we put it all together, we can now declare new resources in two different ways.
Inside a Python control block (recommended for stats):
label start: python: love = resource(0) booze = resource(0) money = resource(0)
or inline using RenPy’s $ syntax for single line Python:
$love = resource(0) $booze = resource(0) $money = resource(0)
Making Magic Happen
Our S&I system is all setup and ready to go. It’s time to put in in action.
Thank’s to RenPy’s fantastic support for conditional statements, this turns out to be pretty trivial.
Let’s say we want to show different dialog choices base on a certain resource i.e. money:
label vending_machine: menu: "A reshing beverage" if money.level >= 1: "You purchase the bevage, it looks delicious." $money.dec() $beverage = resource(1) "You don't have enough money" if money.level < 1: "You walk away sadly." "Maybe you should get a job?"
In this example we present the player with two choice menu options: “You don’t have enough money” or “A refreshing beverage”.
If the player has less than 1 money, they will see only the “You don’t have enough money” and then be derided by the game.
If they have at least 1 money, then they will only have the option to choose “A refreshing beverage”. Once they choose it, the player’s money is decreased (by 1) and the player gains a new beverage resource.
If the player’s going to be purchasing things frequently, consider creating a purchase method that checks to see if the player has enough money. If the items are unique, or there’s no need for the player to have multiple items, consider writing some generic “gatekeeper” code that informs the player they are A) too broke B) already have the item or C) have successfully purchased the item.
In the next tutorial, we’re going to implement an inventory and purchase system for our vending machine that does everything listed above.