[solved] can't find an argument for an object that should be defined within the 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
HochElf
Newbie
Posts: 12
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

[solved] can't find an argument for an object that should be defined within the class.

#1 Post by HochElf »

Hello, I goofed up again somewhere down the line.
I created a class that has 4 arguments given when the object is created and two further attributes that are defined inside the class itself.

Code: Select all

class NPerson(object):
        def __init__(self, s_forename, s_surname, s_location, i_love):

            self.s_forename = s_forename
            self.s_surname = s_surname
            self.s_location = s_location
            self.i_love = i_love

            @property
            def s_name(self):
                return "{} {}".format(self.s_forename, self.surname)

            @property
            def s_avatar(self):
                s_mood = "_neutral.png"
                if 10 >= self.i_love < 20:
                    s_mood = "_neutral.png"
                elif 10 < self.i_love >= 0:
                    s_mood = "_unhappy.png"
                elif 20 >= self.i_love:
                    s_mood = "_happy.png"                
                return "ui/avatars/" + self.s_forename + "/" + self.s_forename + s_mood


#object of the Class NPerson
    l_npc = []
    l_npc.append(NPerson("Eileen", "Smith", "Library", 10))
Now I want to use the s_avatar as a String that helps me to design an image button for a character screen that will show every character at a given location.

Code: Select all

screen scr_character():
    $ i_charPos_x = 200
    for q in l_npc:
        if q.s_location == s_location:
            imagebutton:
                hover q.s_avatar
                idle q.s_avatar
                xpos i_charPos_x
                yalign 1.0
                focus_mask True
                action NullAction()
Now when I test the screen inside the VN it crashes and gives me an error Message:
AttributeError: 'NPerson' object has no attribute 's_avatar'


I have no clue where I dropped the ball here. And the more I look for it, the less I understand is. Maybe someone with fresh eyes could check what it is?
Last edited by HochElf on Sat Jan 09, 2021 6:11 pm, edited 1 time in total.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: can't find an argument for an object that should be defined within the class.

#2 Post by _ticlock_ »

Hi, HochElf,

You have mismatched indentation for s_name and s_avatar methods. As a result, you are actually defining this class methods inside __init__ method, thus they only exist there:

Code: Select all

class NPerson(object):
        def __init__(self, s_forename, s_surname, s_location, i_love):

            self.s_forename = s_forename
            self.s_surname = s_surname
            self.s_location = s_location
            self.i_love = i_love

        @property
        def s_name(self):
            return "{} {}".format(self.s_forename, self.surname)

        @property
        def s_avatar(self):
            s_mood = "_neutral.png"
            if 10 >= self.i_love < 20:
                s_mood = "_neutral.png"
            elif 10 < self.i_love >= 0:
                s_mood = "_unhappy.png"
            elif 20 >= self.i_love:
                s_mood = "_happy.png"                
            return "ui/avatars/" + self.s_forename + "/" + self.s_forename + s_mood
EDIT:
PS: You probably don't want to have l_npc as a class object, since it will be reinitialized when renpy initialize the class.

Code: Select all

default l_npc = [NPerson("Eileen", "Smith", "Library", 10)]

HochElf
Newbie
Posts: 12
Joined: Tue Apr 02, 2019 4:16 am
Deviantart: HochElf
Contact:

Re: can't find an argument for an object that should be defined within the class.

#3 Post by HochElf »

Ouch. Thank you!
I knew I was too deep in the mind quicksand to find this kind of mistake on my own.
I actually plan to expand the number of NPCs later down the line. So I thought it would be a good idea to have a list of objects. Like I'm used to, from my Java days. But I see what you mean. Again thank you. :)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]