Custom/Bulk character definitions

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Custom/Bulk character definitions

#1 Post by Zetsubou »

As was the case with my previous image defining script, here I was looking for a way to define numerous resources with as little ongoing effort as possible.
This script requires a little more input, but some people might find it useful nonetheless.

So, what does it do, and why should I bother?
If your game has a lot of characters, or you want to override specific methods for all characters, or the characters you have been defining are complex, you may wind up with lengthy/numerous defining statements and a lot of repeat code.
Let's try to make it a little more manageable.

Code: Select all

init python early:
    stw = Character(None, color="#ffffff", font="font/Ubuntu-R.ttf", what_color = "#ffffff", show_two_window=True, what_font="font/Ubuntu-L.ttf")

    chars = {
            #Chapter 00
            "koro": {"color": "#D00a"},
            "anon": {"name": "???", "image": None, "color": "#006a"},
            "lucas": {"color": "#096a"},

            #Chapter 01
            "sara": {"color": "#F69a"},
            "lucia": {"color": "#C3Ca"},
            "cec": {"name": "Cecilia", "image": "cecilia", "color": "#F69a"}
            }
    for key in chars:
        if "name" not in chars[key]:
            chars[key]["name"] = key.capitalize()
        if "image" not in chars[key]:
            chars[key]["image"] = key
        character = Character(chars[key]["name"], image=chars[key]["image"], kind=stw, what_outlines=[(1,color(chars[key]["color"]),1,1)])
        def f(what, k=character, outfit=None, pose=None, expression=None, **kwargs):
            #renpy.transition(dissolve, layer="master") #example transition added to say method
            k(what, **kwargs)
        setattr(sys.modules[__name__], key, f)
Let me briefly explain what the above code does.
First, we define a template character, stw. We define a bunch of default options for this character that will be inherited by any new characters.
Next, we create a dictionary of characters. Each character includes its own parameters. In the above example, the optional parameters are name and image. The only mandatory parameter I've added above is color.
Then the character dictionary is iterated, checking for optional parameters and overriding the defaults where necessary.
Each iteration defines a new character.

So, this may seem like more work, but with the basic code there, definitions like:

Code: Select all

define sara = Character("Sara", image="sara", color="#ffffff", font="font/Ubuntu-R.ttf", what_color = "#ffffff", show_two_window=True, what_font="font/Ubuntu-L.ttf", what_outlines=[(1,color("#F69a"),1,1)])
can be shortened to

Code: Select all

"sara": {"color": "#F69a"}
which may become a more attractive option as your define statements get messy.


If you want to go a step further, you can use this method to override character statements, be it selectively or completely.
For example, in the above script I've commented out the line:

Code: Select all

#renpy.transition(dissolve, layer="master") #example transition added to say method
Since it was included in the character's say method, if uncommented, this code would execute whenever the say method is invoked.
In other words, if I ran:

Code: Select all

sara happy "This is a test."
sara confused "Or is it?"
sara happy "Oh, wait. It is."
the code

Code: Select all

renpy.transition(dissolve, layer="master")
would run, causing a dissolve effect between happy and confused. Otherwise I would need to run "with dissolve" after the second and third statements to achieve the same effect.
One or two "with dissolve" statements is no big deal, but if you wanted to dissolve between every expression, you'd save yourself a ton of "with dissolve" statements by including the line above just once.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

Post Reply

Who is online

Users browsing this forum: No registered users