Python function claims a defined name is not defined, refuses to work

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
Yone28
Newbie
Posts: 12
Joined: Sat Feb 25, 2023 4:11 pm
Contact:

Python function claims a defined name is not defined, refuses to work

#1 Post by Yone28 »

Hellooo! I am yet again asking for help with my pseudo-conversation screen.

I currently have this python function defined:

Code: Select all

init python:
    def texting_animation(post):
        if post.int_name == "kaw1":
            renpy.pause(1.0)
            renpy.show_text("[kawa_mess1.content1]", xalign=0.5, yalign=0.3)
            renpy.show("profilepic/kawapfp.webp", xalign=0.4, yalign=0.3)
            renpy.pause(1.0)
            renpy.show_text("[kawa_mess1.content2]", xalign=0.5, yalign=0.2)
            renpy.show("profilepic/kawapfp.webp", xalign=0.4, yalign=0.2)
            renpy.pause(1.0)
            renpy.show_text("[kawa_mess1.content3]", xalign=0.5, yalign=0.1)
            renpy.show("profilepic/kawapfp.webp", xalign=0.4, yalign=0.1)
        else:
            pass
What it SHOULD do is check if the post's (posts being a python class previously defined) int_name value is "kaw1", and if it is, play a crude animation I cooked up. The plan is to edit it down the line so that it works universally for all the other "post(s)" the player can see.

Now, this function is called as such in a screen:

Code: Select all

screen kawa_mes1:
    add "hud/convo_bg.webp"
    modal True
    key "e" action [Hide("kawa_mes1"), Show("phone_trigger"), With(dissolve)]

    python:
        if kfirstmess_seenflag:
            texting_animation(post)
        else:
            pass
This, in theory, should add the "convo_bg" image, THEN check if the kfirstmess_seenflag is True, and THEN either call the function or do nothing. So far, the only thing it does is give me this error:

I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/script.rpy", line 34, in script
e "if you are reading this, kawa sent you smt funny."
File "game/screensnscripts/screenss.rpy", line 91, in execute
screen kawa_mes1:
File "game/screensnscripts/screenss.rpy", line 91, in execute
screen kawa_mes1:
File "game/screensnscripts/screenss.rpy", line 96, in execute
python:
File "game/screensnscripts/screenss.rpy", line 100, in <module>
texting_animation(post)
NameError: name 'post' is not defined
I'm kind of lost? What's exactly the issue here? If it helps, here is the Post class definition:

Code: Select all

init python:
    class Post:
        def __init__(self, int_name, attachment, topic, content1, content2, content3):
            self.int_name = int_name
            self.attachment = attachment
            self.topic = topic
            self.content1 = content1
            self.content2 = content2
            self.content3 = content3
There is also a post object that has "kaw1" as the int_name value, defined ABOVE the function that triggers the error. I've tried changing the P capitalization in both the function and the screen that calls it, both to no avail. Any help is appreciated.
I am losing it

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

Re: Python function claims a defined name is not defined, refuses to work

#2 Post by _ticlock_ »

Yone28 wrote: Sat Apr 08, 2023 9:09 pm There is also a post object that has "kaw1" as the int_name value, defined ABOVE the function that triggers the error. I've tried changing the P capitalization in both the function and the screen that calls it, both to no avail. Any help is appreciated.
I didn't get the idea, but talking about the error:

defined ABOVE is not very clear (outside any labels; inside a label using python statement, before showing the screen, something else)

Anyway, check if you delete the object `post` at some point. You can also check if the `post` defined right before showing the screen:

Code: Select all

    "[post]"
    show screen kawa_mes1
Would you have the error, if you set the variable `post` right before showing the screen:

Code: Select all

    $ post = Post(...)
    show screen kawa_mes1

User avatar
Yone28
Newbie
Posts: 12
Joined: Sat Feb 25, 2023 4:11 pm
Contact:

Re: Python function claims a defined name is not defined, refuses to work

#3 Post by Yone28 »

_ticlock_ wrote: Sat Apr 08, 2023 11:47 pm
Yone28 wrote: Sat Apr 08, 2023 9:09 pm There is also a post object that has "kaw1" as the int_name value, defined ABOVE the function that triggers the error. I've tried changing the P capitalization in both the function and the screen that calls it, both to no avail. Any help is appreciated.
I didn't get the idea, but talking about the error:

defined ABOVE is not very clear (outside any labels; inside a label using python statement, before showing the screen, something else)

Anyway, check if you delete the object `post` at some point. You can also check if the `post` defined right before showing the screen:

Code: Select all

    "[post]"
    show screen kawa_mes1
Would you have the error, if you set the variable `post` right before showing the screen:

Code: Select all

    $ post = Post(...)
    show screen kawa_mes1
What I meant to say that there is an object for the "post" python class that has an "int_name" value "kaw1", meaning the error is not being caused by me forgetting to create the post object lol. The object itself is defined right above the function, so the function should have no problem getting its info. Both of these, alongside the Post class, are defined in a different screen than the screen meant to call the function, but afaik this shouldn't be a problem and I have another function that worked just fine.

About the fix you suggested:

I tried, it gives back the same error. Even if it didn't, I think it would completely break because the previous screen code wouldn't have anything to work with.

The problem here is that RenPy claims the "post" class is not defined, when it is. The post object i'm trying to get info from works fine, as I have tested it already
I am losing it

User avatar
Yone28
Newbie
Posts: 12
Joined: Sat Feb 25, 2023 4:11 pm
Contact:

Re: Python function claims a defined name is not defined, refuses to work

#4 Post by Yone28 »

So this is a bit embarassing, but I found the issue lol.

This is the original code for the screen:

Code: Select all

screen kawa_mes1:
    add "hud/convo_bg.webp"
    modal True
    key "e" action [Hide("kawa_mes1"), Show("phone_trigger"), With(dissolve)]

    python:
        if kfirstmess_seenflag:
            texting_animation(post)
        else:
            pass
Here is the updated, working one:

Code: Select all

screen kawa_mes1:
    add "hud/convo_bg.webp"
    modal True
    key "e" action [Hide("kawa_mes1"), Show("phone_trigger"), With(dissolve)]

    for post in kawa_txt:
        python:
            if kfirstmess_seenflag:
                pass
            else:
                texting_animation(post)
In short, I forgot to tell the screen to check the appropiate list with the "for X in Y" line. All's good now... well, the function is poorly written, that's besides the point tho.
I am losing it

NicholasWegner
Newbie
Posts: 1
Joined: Mon Apr 10, 2023 7:14 am
Contact:

Re: Python function claims a defined name is not defined, refuses to work

#5 Post by NicholasWegner »

Thanks for helping me as well. If you're looking for the best online essay writers, click this over here now, you may browse top websites with reviews to help you find the greatest essay writer for you.

Post Reply

Who is online

Users browsing this forum: No registered users