Making Custom Classes in Ren'Py

in #python7 years ago

Ren’Py_Logo_6-13-6_200x307px.png

Making Custom Classes in Ren'Py

Ren'Py, the free game engine that powers the indie hit Doki Doki Literature Club, is a simple yet versatile piece of software that puts game development within reach of the average person. If you happen to know Python, you can even make the engine go beyond the narrow confines of the visual novel format, as I did with my own game, Parasite Lance.

An excellent way to get more out of Ren'Py is to make custom classes in Python that perform functions you need for your game.

A Simple Custom Class: Rolling a Die

As an example, we will make a class that rolls a die and outputs a number. Follow the onscreen prompts to start a new Ren'Py project, then open your Python editor of choice to get started.

First, we make a little header to explain what the file is for. Commenting on your code is important, since it allows you to understand what your code does even after you've been away from it for a while.

## -*- coding: utf-8 -*-

## This program simulates the rolling of dice.

Next, we save the file in the "game" folder of your project as "ExtraStuff.py" without quotes. Save early and save often.

Once that is done, we create a class called DiceStuff, which we will use to control dice-related functions.

Drag and drop the following code into your Python editor.

import random

## The class DiceStuff will let us
## simulate the rolling of dice.
class DiceStuff:

    ## Here, we initialize any variables
    ## we might need. For now, we don't
    ## need any, so we just put "pass"
    ## underneath the initial method.
    def __init__ (self):
        pass

    ## This method will output the result
    ## of a single die roll. sides=6 indicates
    ## that unless you specify a number of sides,
    ## the method will assume you are rolling a
    ## six-sided die, or d6.
    def rollDie (self, sides=6):

        ## The rolled value will go here.
        value = None

        ## The roll is made here. The method
        ## random.randint(a,b) chooses a value
        ## between a and b, including a and b
        ## themselves.
        value = random.randint(1,sides)

        ## Output the roll here. If you call
        ## the method and assign it to a variable,
        ## that variable will contain an integer
        ## chosen by the above line of code.
        return value

After that, save as usual and open your script.rpy file.

Replace define e = Character("Eileen") with the following:

init:

    define e = Character("Eileen")

Now, before the "define e" line, we will import what we wrote in the Python editor. Put the following line between init and define e, making sure that you have indented it four spaces:

    $ import ExtraStuff as es

This will allow us to use the classes in ExtraStuff. Next, we will move on to the code indented after "label start". After "show eileen happy," place the following line:

$ stuff = es.DiceStuff()

Now we can call the rollDie method inside Ren'Py itself. Right after it, place:

$ whatRoll = stuff.rollDie()

Finally, we will make Eileen say the roll value. After "Once you add a story...", place the following line:

e "You rolled a [whatRoll]."

Now save everything and run the project, making sure that all indentations are done with four spaces; Ren'Py does not tolerate tabs. Eileen should say her first couple of lines, then state the value of the roll determined by stuff.rollDie().

And there you have it! You've created your first custom class in Ren'Py!

Top image © Tom "PyTom" Rothamel and Piroshiki

Rawle Nyanzi Logo small (red background).png

I'm Rawle Nyanzi, a professional author who seeks only to entertain. My blog is a convenient place where you can find all my writings and some of my opinions on various topics relating to politics, pop culture, and even gender.

Also, do purchase a copy of Sword & Flower, a story of a Japanese pop star and and English Puritan banding together to fight demons.

Logo image by Kukuruyo. Check his commission rates.

Coin Marketplace

STEEM 0.22
TRX 0.26
JST 0.039
BTC 98362.82
ETH 3451.59
USDT 1.00
SBD 3.21