Trouble with inheritance

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
mtermik
Regular
Posts: 28
Joined: Fri Dec 23, 2022 2:26 pm
Contact:

Trouble with inheritance

#1 Post by mtermik »

I am having a bit of an issue when it comes to one class inheriting another. As a simple example I have a class called Person, which contains generic information about a person object. This class is contained in it's own file called person.rpy. I have another class called Male, which contains specific information about a male character. This class is contained in it's own file called male.rpy. Both files are located in the same sub-folder (Objects) with in the project structure. I've included an empty __init__.py file within the Objects folder as well.

The code I currently have is this:

Code: Select all

$ from person import Person

init python:
    class Male(Person):
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)
However, I receive the following error when launching the project:

Code: Select all

 "NameError: name 'Person' is not defined".
I realize that I can solve the problem by nesting the Person class within the same file as the Male class. But that is sub-optimal as I have a Female class which also inherits from Person, and having multiple classes contained within the same file is an abhorrent coding practice.

So my question is this, am I improperly referencing the import, missing a step, or some otherwise messing it up myself, or is it a limitation of renpy that it cannot inherit from a class in a different file?

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1042
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Trouble with inheritance

#2 Post by m_from_space »

It's important to know that Renpy tries to initialize Python "init" blocks according to their priority. Maybe that's your problem. You should also put your imports behind an init block, maybe that's the actual problem. Just throwing things here. :D

https://www.renpy.org/dev-doc/html/pyth ... -statement

Code: Select all

init python:
    from person import Person
    class Male(Person):
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)

mtermik
Regular
Posts: 28
Joined: Fri Dec 23, 2022 2:26 pm
Contact:

Re: Trouble with inheritance

#3 Post by mtermik »

m_from_space wrote: Mon May 15, 2023 3:14 pm It's important to know that Renpy tries to initialize Python "init" blocks according to their priority. Maybe that's your problem. You should also put your imports behind an init block, maybe that's the actual problem. Just throwing things here. :D

https://www.renpy.org/dev-doc/html/pyth ... -statement

Code: Select all

init python:
    from person import Person
    class Male(Person):
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)
Thanks for the suggestion but unfortunately I had already tried that. Putting the import inside of the init block has the engine attempting to load person as a module not an individual class. Which results in the following error:

Code: Select all

ModuleNotFoundError: No module named 'person'

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2438
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Trouble with inheritance

#4 Post by Ocelot »

You do not have to use import when referring to a class defined in another rpy file.

Because functionally all rpy files work as a single file, no need for import. Just make sure that init order is correct (you want Person be initialized before Male, for example).
< < insert Rick Cook quote here > >

mtermik
Regular
Posts: 28
Joined: Fri Dec 23, 2022 2:26 pm
Contact:

Re: Trouble with inheritance

#5 Post by mtermik »

Ocelot wrote: Mon May 15, 2023 3:30 pm You do not have to use import when referring to a class defined in another rpy file.

Because functionally all rpy files work as a single file, no need for import. Just make sure that init order is correct (you want Person be initialized before Male, for example).
I was wondering if order of initialization was a problem. How do I define the init order within the project?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2438
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Trouble with inheritance

#6 Post by Ocelot »

https://www.renpy.org/doc/html/python.h ... -statement

TL;DR: add a init priority number between init and python: init -123 python:
< < insert Rick Cook quote here > >

mtermik
Regular
Posts: 28
Joined: Fri Dec 23, 2022 2:26 pm
Contact:

Re: Trouble with inheritance

#7 Post by mtermik »

Ok so I think I have it all figured out now, as my project compiles and runs without any errors being thrown. However, I hate when someone figures something out, leaves the thread and never informs others about the solution. So for anyone else who runs into this issue, I had to adjust my files accordingly.

Code: Select all

init 1 python:
    class Person:
        def __init__(self):

Code: Select all

init 2 python:
    class Male(Person):
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)

Code: Select all

init 2 python:
    class Female(Person):
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)
No need for the importing of the class into the sub-classes.

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1042
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Trouble with inheritance

#8 Post by m_from_space »

mtermik wrote: Mon May 15, 2023 3:35 pm I was wondering if order of initialization was a problem. How do I define the init order within the project?
It's what I mentioned first and I also provided the link for it, make sure to properly read replies. :)

Post Reply

Who is online

Users browsing this forum: No registered users