Getting error "instance has no attribute".

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
allanon
Newbie
Posts: 15
Joined: Wed Apr 15, 2015 4:33 pm
Contact:

Getting error "instance has no attribute".

#1 Post by allanon »

Hello, everyone! Sorry for russian symbols in code at first, and let me explain the problem:

I have a class, that displays current time (gettime) and can increase time (changetime):

Code: Select all

init -1 python:
    #Start time
    minute = 0
    check_minute = 0
    hour = 8
    ptime = 0
    weekday = 1
    month = 5
    number = 1
    year = 2010

    class Ctime:
            
        def gettime(self):
            #Дни недели
            if weekday == 1: _weekday = 'Понедельник'
            if weekday == 2: _weekday = 'Вторник'
            if weekday == 3: _weekday = 'Среда'
            if weekday == 4: _weekday = 'Четверг'
            if weekday == 5: _weekday = 'Пятница'
            if weekday == 6: _weekday = 'Суббота'
            if weekday == 7: _weekday = 'Воскресенье'


            #Месяца
            if month == 1: _month = 'Января'
            if month == 2: _month = 'Февраля'
            if month == 3: _month = 'Марта'
            if month == 4: _month = 'Апреля'
            if month == 5: _month = 'Мая'
            if month == 6: _month = 'Июня'
            if month == 7: _month = 'Июля'
            if month == 8: _month = 'Августа'
            if month == 9: _month = 'Сентября'
            if month == 10: _month = 'Октября'
            if month == 11: _month = 'Ноября'
            if month == 12: _month = 'Декабря'
            
            if minute < 10:
                output = '%d %s %d года, %s. %s:0%s' % (number, _month, year, _weekday, hour, minute)
            else:
                output = '%d %s %d года, %s. %s:%s' % (number, _month, year, _weekday, hour, minute)
            
            return output
            
        def changetime(self,change):
            global minute, check_minute, hour, ptime, weekday, number, year, month
            minute += change
            check_minute += change
            
            while minute >= 60:
                minute -= 60
                hour += 1
                ptime += 1
                if hour >= 24:
                    hour -= 24
                    weekday += 1
                    if weekday >=8: weekday -=7
                    number += 1
                    if number >= 31:
                        number = 30
                        month += 1
                        if month == 13:
                            month -=12
                            year += 1
            return
Initialsation in start:

Code: Select all

init:
    $ currtime = Ctime()
Using in screens:

Code: Select all

screen stats_screen:
    frame xalign 1.0 yalign 0.0:
        vbox xmaximum config.screen_width/5:
            text "[currtime.gettime()]" style style.my_text
Error:
File "game/screens.rpy", line 567, in execute
text "[currtime.gettime()]" style style.my_text
AttributeError: Ctime instance has no attribute 'gettime()'

Question is why? How do I have to initialise this attribute?

User avatar
allanon
Newbie
Posts: 15
Joined: Wed Apr 15, 2015 4:33 pm
Contact:

Re: Getting error "instance has no attribute".

#2 Post by allanon »

And much simplier example:

Code: Select all

init python:
    class MyClass:

        def f(self):
            output = 'hello world'
            return output

    tmp = MyClass()

label start:
    "tmp = [tmp.f()]"
Why this does not work?? Same error as above, class MyClass has no attribute. How do I call function from class?

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Getting error "instance has no attribute".

#3 Post by trooper6 »

Here is how I got your second example to work:

Code: Select all

init -1 python:
    class MyClass():
        def f(self):
            output = "hello world"
            return output

# The game starts here.
label start:
    $ tmp = MyClass()
    
    $ txt = tmp.f()
    "tmp = [txt]"
Somethings to note. I tried defining the class with and without the parenthesis. Both ways work...though I always see class definitions use parentheses...so...I always use them. But that isn't your problem.

I also declared my instance of MyClass inside the start label. This well mean that the object is saved properly and will participate in rollback properly.

But your problem is "tmp = [tmp.f()]"
It doesn't look like you can all a function inside of interpolation brackets like you want to. The program seems to think that using dot notation there means that f is an attribute of the class, when it is a function instead. So to fix the problem do what I did above: call the function and store the result in a variable and then put that variable inside the interpolation brackets.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
allanon
Newbie
Posts: 15
Joined: Wed Apr 15, 2015 4:33 pm
Contact:

Re: Getting error "instance has no attribute".

#4 Post by allanon »

Thank you so much!

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Getting error "instance has no attribute".

#5 Post by trooper6 »

Hi, I know this is late, but I was dealing a bit more with interpolation when I was working on a test program experimenting with data sets, and I came up with another way to solve your problem:

Code: Select all

init -1 python:
    class MyClass():
        def f(self):
            output = "hello world"
            return output

# The game starts here.
label start:
    $ tmp = MyClass()
    $narrator("{0}".format(tmp.f()))
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot], Ocelot, snotwurm