Page 1 of 1

Is it possible to customize the Character class ?

Posted: Sun Mar 01, 2020 11:38 am
by renardjap
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

Re: Is it possible to customize the Character class ?

Posted: Sun Mar 01, 2020 2:59 pm
by Alex

Re: Is it possible to customize the Character class ?

Posted: Sun Mar 01, 2020 4:51 pm
by renardjap
Oh.. looks difficult...

Thank you, I will try it

Re: Is it possible to customize the Character class ?

Posted: Mon Mar 02, 2020 1:09 pm
by rames44
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”