Is it possible to customize the Character class ?

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
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Is it possible to customize the Character class ?

#1 Post by renardjap » Sun Mar 01, 2020 11:38 am

Hi,

Is there a possibility to customize the Character class in Ren'Py ? When we start the game, we define the class Character to assign some parameters like the name, the color text, the image side. I want to go forward with my character. I would like to create a child class of Character() to define more parameters like the HP, the Level, the strength etc.

Generally I define the class Character (name, color text, image side), and I create a Player class to add more features.

Code: Select all

init python:
	class Player:
        	def __init__(self, name, hp = 0, money = 0):
            		self.name = name
            		self.hp = hp

        	def addHP(self, points):
            		self.hp += points

        	def subHP(self, points):
            		if self.hp < 0:
                		self.hp = 0
            		else:
                		self.hp -= points
                		
define e = Character('Eileen', color="#c8ffc8")
define eileen = Player("Eileen", 10)

label start:

    e "Hello my name is [eileen.name] and I have [eileen.hp] HP " # I want to replace eileen.name by e.name 
    # I would like something like this: e "Hello my name is [e.name] and I have [e.hp] HP "

Must create 2 variables for each character, and I want to simplify the task and put together all the parameters in only one variable.
Is it possible to customize the Character class or create a child class ?

Thanks :D

User avatar
Alex
Lemma-Class Veteran
Posts: 2981
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Is it possible to customize the Character class ?

#2 Post by Alex » Sun Mar 01, 2020 2:59 pm


User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Is it possible to customize the Character class ?

#3 Post by renardjap » Sun Mar 01, 2020 4:51 pm

Oh.. looks difficult...

Thank you, I will try it

rames44
Veteran
Posts: 232
Joined: Sun May 29, 2016 4:38 pm
Contact:

Re: Is it possible to customize the Character class ?

#4 Post by rames44 » Mon Mar 02, 2020 1:09 pm

There is another possibility, which may or may not be easier. The thing to understand is that when you construct a dialog line in your script, the speaker doesn’t HAVE to be a Character object. All it has to do is obey the protocol. See https://www.renpy.org/doc/html/dialogue ... quivalents - basically, the object gets “called” with parameters.

What that means is that out could create your own class, have it have a Character object as a member, use the instance of your own class where you’d normally use a Character, and then delegate say statements down to the Character object by implementing __call__(). Something like

Code: Select all

class MyOwnCharacterClass:
    def __init__(self, a_character):
        self.the_wrapped_character = a_character

    def __call__(self, *args, **kwargs)
        self.the_wrapped_character(*args, **kwargs)
        
        
define emily_char = Character(“Emily”)
define emily = MyOwnCharacterClass(emily_char)
...
    emily “Hello world”

Post Reply

Who is online

Users browsing this forum: Bing [Bot], span4ev