[SOLVED]One Class : differents ways to init attributes

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
GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

[SOLVED]One Class : differents ways to init attributes

#1 Post by GoldenD »

Hi everybody,
i'm sure it's a simple thing for you, but don't find how to.
Have a common caracter class:

Code: Select all

class Characters(object):
        def __init__(self, id, name, strength):
            self.id                     = id
            self.name                   = name
            self.strength               = strength
Goal is to create an instance Hero and some Enemies instances.
For Enemies, I init Strength with a value passed when create class, but for the Hero, Strength is an automatic random value.

So do I have to remove Strength from my common class and create 2 sub classes, or is there a way to implement 2 ways of intialize the same attribute ?

Waouh, I read my question and I'm so sorry for your brain !!!!
Last edited by GoldenD on Sat Jan 30, 2021 2:56 am, edited 1 time in total.

User avatar
midgethetree
Regular
Posts: 39
Joined: Wed Dec 30, 2020 3:51 pm
Completed: Back When, The Mother of Pearls, various jam games
Projects: When Everyone's Watching, Deliberation
Github: midgethetree
itch: midge-the-tree
Discord: rainafc#3353
Contact:

Re: One Class : differents ways to init attributes

#2 Post by midgethetree »

One option is to make Hero a subclass of Characters that initializes strength differently, like so:

Code: Select all

class Hero(Characters):
    def __init__(self, id, name):
        strength = random.randint(5, 10)
        super(Hero, self).__init__(id, name, strength)
This could be useful if you want to make many heroes (which it sounds like you don't) or want to give the hero class other features too. However, if the Hero class is identical to the Characters class in every other way, you could get the same results by just using:

Code: Select all

hero = Characters(0, 'myname', random.randint(5,10))

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

Re: One Class : differents ways to init attributes

#3 Post by Alex »

GoldenD wrote: Fri Jan 29, 2021 12:39 pm ...For Enemies, I init Strength with a value passed when create class, but for the Hero, Strength is an automatic random value. ...
What if create a Hero instance with any value of 'strength' and at the beginning of the game change hero.strength to random value?

ketolow
Newbie
Posts: 2
Joined: Thu Jan 28, 2021 2:37 pm
Contact:

Re: One Class : differents ways to init attributes

#4 Post by ketolow »

You can simply use the if statement, like:

Code: Select all

class Characters(object):
        def __init__(self, id, name, strength):
            self.id = id
            self.name = name
            if (char == hero):
            	self.strength = random()
            else:
            self.strength = strength

GoldenD
Regular
Posts: 43
Joined: Thu Jan 16, 2020 2:01 am
Contact:

Re: One Class : differents ways to init attributes

#5 Post by GoldenD »

Ok guys,
I notice your answers. Thought there was something else but it's ok, I just have to choice.

Thanks a lot

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], LemonaBittera