Simplifying a character code

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
richycapy
Regular
Posts: 51
Joined: Mon May 27, 2019 8:53 pm
Organization: EN Productions
Location: Mexico
Contact:

Simplifying a character code

#1 Post by richycapy » Fri May 31, 2019 1:08 am

Hi!

Im new with RenPy and I just started a game, Im testing with three characters (and it may come some more), so I making them using "LiveComposite" so it has movement with the eyes for blinking and mouth when talking

I have this code in characters.rpy

Code: Select all

default andy_talking = False
default andy_eyes = "Blinking" #Blinking, Opened, Closed
default andy_expression = "Happy" #Happy, Shy -- Pendientes: Horny, Sad, Surprise, Angry
default andy_side = "Right" #Left or Right
image andy mouth still = "images/personajes/Andy/[andy_expression]/[andy_side]_Mouth_0001.png"
image andy eyes closed = "images/personajes/Andy/[andy_expression]/[andy_side]_Eyes_closed.png"
image andy eyes opened = "images/personajes/Andy/[andy_expression]/[andy_side]_Eyes_opened.png"

image andy mouth talking:
    "images/personajes/Andy/[andy_expression]/[andy_side]_Mouth_0001.png"
    0.2
    "images/personajes/Andy/[andy_expression]/[andy_side]_Mouth_0002.png"
    0.2
    "images/personajes/Andy/[andy_expression]/[andy_side]_Mouth_0003.png"
    0.5
    repeat

image andy eyes blinking:
    "images/personajes/Andy/[andy_expression]/[andy_side]_Eyes_opened.png"
    choice:
        2.0
    choice:
        3.1
    choice:
        4.2
    choice:
        3.3
    "images/personajes/Andy/[andy_expression]/[andy_side]_Eyes_closed.png"
    .25
    "images/personajes/Andy/[andy_expression]/[andy_side]_Eyes_opened.png"
    .13
    repeat

image andy = LiveComposite((644, 720),
    (0,0), "images/personajes/Andy/[andy_expression]/Base_[andy_side].png",
    (0,0), ConditionSwitch(
        "andy_eyes == 'Blinking'", "andy eyes blinking",
        "andy_eyes == 'Closed'", "andy eyes closed",
        "andy_eyes == 'Opened'", "andy eyes opened"),
    (0,0), ConditionSwitch(
        "andy_talking", "andy mouth talking",
        "True", "andy mouth still")
    )

default chloe_talking = False
default chloe_eyes = "Blinking" #Blinking, Opened, Closed
default chloe_expression = "Happy" #Angry, Happy, Surprise  -- Pendientes: Horny, Sad, Shy
default chloe_side = "Right" #Left or Right
image chloe mouth still = "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Mouth_0001.png"
image chloe eyes closed = "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Eyes_closed.png"
image chloe eyes opened = "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Eyes_opened.png"

image chloe mouth talking:
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Mouth_0001.png"
    0.2
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Mouth_0002.png"
    0.2
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Mouth_0003.png"
    0.5
    repeat

image chloe eyes blinking:
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Eyes_opened.png"
    choice:
        2.0
    choice:
        3.1
    choice:
        4.2
    choice:
        3.3
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Eyes_closed.png"
    .25
    "images/personajes/Chloe/[chloe_expression]/[chloe_side]_Eyes_opened.png"
    .13
    repeat

image chloe = LiveComposite((644, 720),
    (0,0), "images/personajes/Chloe/[chloe_expression]/Base_[chloe_side].png",
    (0,0), ConditionSwitch(
        "chloe_eyes == 'Blinking'", "chloe eyes blinking",
        "chloe_eyes == 'Closed'", "chloe eyes closed",
        "chloe_eyes == 'Opened'", "chloe eyes opened"),
    (0,0), ConditionSwitch(
        "chloe_talking", "chloe mouth talking",
        "True", "chloe mouth still")
    )

default sophia_talking = False
default sophia_eyes = "Blinking" #Blinking, Opened, Closed
default sophia_expression = "Happy" #Angry, Happy, Surprise  -- Pendientes: Horny, Sad, Shy
default sophia_side = "Right" #Left or Right
image sophia mouth still = "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Mouth_0001.png"
image sophia eyes closed = "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Eyes_closed.png"
image sophia eyes opened = "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Eyes_opened.png"

image sophia mouth talking:
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Mouth_0001.png"
    0.2
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Mouth_0002.png"
    0.2
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Mouth_0003.png"
    0.5
    repeat

image sophia eyes blinking:
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Eyes_opened.png"
    choice:
        2.0
    choice:
        3.1
    choice:
        4.2
    choice:
        3.3
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Eyes_closed.png"
    .25
    "images/personajes/Sophia/[sophia_expression]/[sophia_side]_Eyes_opened.png"
    .13
    repeat

image sophia = LiveComposite((644, 720),
    (0,0), "images/personajes/Sophia/[sophia_expression]/Base_[sophia_side].png",
    (0,0), ConditionSwitch(
        "sophia_eyes == 'Blinking'", "sophia eyes blinking",
        "sophia_eyes == 'Closed'", "sophia eyes closed",
        "sophia_eyes == 'Opened'", "sophia eyes opened"),
    (0,0), ConditionSwitch(
        "sophia_talking", "sophia mouth talking",
        "True", "sophia mouth still")
    )
Basically I'm repeating the code over and over for every character, then in the script.rpy I use it like this:

Code: Select all

    $ chloe_talking = True
    $ chloe_expression = "Angry"
    $ chloe_eyes = "Blinking"
    $ chloe_side = "Left"
    $ sophia_talking = False
    $ sophia_expression = "Angry"
    $ sophia_eyes = "Blinking"
    $ andy_talking = False
    $ andy_expression = "Shy"
    $ andy_eyes = "Closed"
    chloe_character "Hello Andy!"

    $ chloe_talking = False
    $ chloe_expression = "Happy"
    $ chloe_eyes = "Blinking"
    $ chloe_side = "Right"
    $ sophia_talking = False
    $ sophia_expression = "Happy"
    $ sophia_eyes = "Blinking"
    $ andy_talking = True
    $ andy_expression = "Happy"
    $ andy_eyes = "Blinking"
    andy_character "Oh, Hi there! I'm Boo bo... I meen Andy... please to meet you"

    $ chloe_talking = False
    $ chloe_expression = "Surprise"
    $ chloe_eyes = "Opened"
    $ sophia_talking = True
    $ sophia_expression = "Horny"
    $ sophia_eyes = "Opened"
    $ andy_talking = False
    $ andy_expression = "Happy"
    $ andy_eyes = "Blinking"
    sophia_character "Hi, I'm Sophia, my friend call me Sophy, but you can call me when ever you want!"
Everytime I make a character talk, I set it up as "TRUE" when talking and everybody else as "FALSE", also, I set up the expression and what their eyes are doing, and the direction facing...

So my question is... isnt there a way to simplify this? May be create a function like so:

Code: Select all

 $ setcharacter ('Andy', False, 'Surprise', 'Opened', 'Left') 
Thanks for the assist! :D

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Simplifying a character code

#2 Post by Remix » Fri May 31, 2019 10:12 am

Code: Select all

ConditionSwitch(
        "renpy.get_say_image_tag() == 'sophia'", "sophia mouth talking",
        "True", "sophia mouth still")
    )
Presumes your Characters declare their image tag to use:

Code: Select all

define sophia_character = Character("Soapy", image="sophia")
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Simplifying a character code

#3 Post by Remix » Fri May 31, 2019 10:25 am

Setting the others might best be done with a generic store value toggler: (untested)

Code: Select all

init python:

    def toggle_states( character="andy", **kwargs ):

        for k in kwargs:
        
            # evaluate a string from character and the key
            # e.g. andy_expression
            #
            # set that named variable in the store to the value

            setattr( store, "{}_{}".format(character, k), kwargs[k] )

label start:

    $ toggle_states( "andy", eyes="Open", expression="Happy" )
Frameworks & Scriptlets:

User avatar
richycapy
Regular
Posts: 51
Joined: Mon May 27, 2019 8:53 pm
Organization: EN Productions
Location: Mexico
Contact:

Re: Simplifying a character code

#4 Post by richycapy » Fri May 31, 2019 9:17 pm

Is there a way to do this correctly:

Code: Select all


init python:
    class setcharacter:
        def __init__(self, character, talking, eyes, chexpression, chside):
           $ characterspecs_list[self.character]['talking'] = self.talking 
           $ characterspecs_list[self.character]['eyes'] = self.eyes
           $ characterspecs_list[self.character]['expression'] = "self.chexpression
           $ characterspecs_list[self.character]['side'] = self.chside

So I can do something like this:

Code: Select all

setcharacter("andy", False, "Blinking", "Happy", "Right")

Post Reply

Who is online

Users browsing this forum: Bing [Bot]