"NameError: name not defined" when trying to use a user defined function

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
Yuzumin
Newbie
Posts: 2
Joined: Thu Jun 21, 2018 5:09 am
Contact:

"NameError: name not defined" when trying to use a user defined function

#1 Post by Yuzumin »

Here is what I've written

Code: Select all

#rp for K
default rp_k = 0
#rp for P
default rp_p = 0
#rp for S
default rp_s = 0
#rp for Y
default rp_y = 0

python:
    def inc():
        nonlocal [rp_y]
        rp_y += 1
    def dec():
        nonlocal [rp_y]
        rp_y -= 1
label start:

    menu:
        "This should lower rp for Y":
            $ dec()

        "This should increase your rp for Y":
            $ inc()

    "Y's RP for you is now [rp_y]"
Here I'm trying to define the function of inc() to increase the variable rp_y by 1 through the use of a user defined function in a menu choice instead of using
$ rp_y = 1+rp_y
I'm a pretty big beginner in Python but I want to try to learn how to make this quick and efficient since I want many menu choices to either increase or decrease rp_y without having to write a long string.

https://i.imgur.com/403UNgC.png
here I went on Jupyter to test out what i wanted to do (I used the global variable instead of nonlocal but both weren't working in renpy) and it worked fine so i'm not really sure what i'm really doing wrong.

error message:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 35, in script
    
  File "game/script.rpy", line 35, in <module>
    
NameError: name 'inc' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 35, in script
    
  File "C:\Users\xxx\Documents\renpy-7.0.0-sdk\renpy\ast.py", line 862, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\xxx\Documents\renpy-7.0.0-sdk\renpy\python.py", line 1912, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 35, in <module>
    
NameError: name 'inc' is not defined

Windows-8-6.2.9200
Ren'Py 7.0.0.196
Testing Area 1.0
Sat Jun 30 10:43:44 2018

Yuzumin
Newbie
Posts: 2
Joined: Thu Jun 21, 2018 5:09 am
Contact:

Re: "NameError: name not defined" when trying to use a user defined function

#2 Post by Yuzumin »

Nevermind I figured it out after a while and reading through a bunch of the documentation. For anyone else that encounters this the main thing I got wrong was writing python init and instead wrote just python. As for efficiency I should've added an argument in the brackets of the function I was defining so I could easily manipulate how much was added or subtracted from the variable rp_y instead of strictly just 1. I also changed the code inside the defined function from nonlocal to global

Code: Select all

#rp for Y
default rp_y = 0

init python:
    def inc(x):
        global rp_y
        rp_y += x

    def dec(x):
        global rp_y
        rp_y -= x

label start:

    menu:
    
        "This should lower rp for Y":
            $ dec(1)

        "This should increase your rp for Y":
            $ inc(1)

    "Y's RP for you is now [rp_y]"

Post Reply

Who is online

Users browsing this forum: No registered users