NameError traceback occuring on if statements.

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
Gollier
Newbie
Posts: 2
Joined: Tue Mar 08, 2011 9:46 am
Contact:

NameError traceback occuring on if statements.

#1 Post by Gollier »

So, I'm only a week into using Renpy, and one of the things I'm working on features a system where the game is supposed to remember choices you make to influence the game. However, I've been having trouble in getting the game to recognize the python script I set up earlier. Here's the traceback text:
I'm sorry, but an uncaught exception occurred.

NameError: name 'choice5' is not defined

While running game code:
- script at line 220 of C:\Users\Desktop\Ryan/game/script.rpy
- python at line 220 of C:\Users\Desktop\Ryan/game/script.rpy.

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

File "C:\Users\Desktop\renpy-6.12.0\renpy\bootstrap.py", line 279, in bootstrap
File "C:\Users\Desktop\renpy-6.12.0\renpy\main.py", line 320, in main
File "C:\Users\Desktop\renpy-6.12.0\renpy\main.py", line 93, in run
File "C:\Users\Desktop\renpy-6.12.0\renpy\execution.py", line 259, in run
File "C:\Users\Desktop\renpy-6.12.0\renpy\ast.py", line 1321, in execute
File "C:\Users\Desktop\renpy-6.12.0\renpy\python.py", line 994, in py_eval
File "C:\Users\Desktop\Ryan/game/script.rpy", line 220, in <module>
NameError: name 'choice5' is not defined

While running game code:
- script at line 220 of C:\Users\Desktop\Ryan/game/script.rpy
- python at line 220 of C:\Users\Desktop\Ryan/game/script.rpy.

Ren'Py Version: Ren'Py 6.12.0e
And here is the code for the section where the game itself runs into the traceback:

Code: Select all

label choice6_done:
    
    r "Joey gets to school like normal, and sees someone hanging out with his friends..."
    
    if choice5 == "friendly":
    
        r "Arthur is busy chatting up Joey's friends, and talks with Joey about bodybuilding."
        
        r "Joey sort of likes the positive attention, but inwardly knows he wouldn't be able to keep up the lie for long."
        
        jump choice7_start
        
    else:
        
        r "Molly, who is good friends with Sue, mentions casually how she saw Joey pigging out at the snack machines yesterday."
        
        if choice3 == "give":
            
            r "Sally tries to speak up for Joey, but Molly gets talked down by Molly."
        
        r "Joey finds himself at odds with the people around him, and has to make a choice."
        
        jump choice7_start
The game continues fine if I pick the options that get you the choice5 = "friendly" option, but it shuts down if this isn't the case. This also happens for some script later in the game. I don't understand how this could happen, because I managed to make code like this work earlier in the game, but it refuses to do it now without any noticable difference between this and the other code I wrote. Any help would be appreciated.

Megaman Z
Miko-Class Veteran
Posts: 829
Joined: Sun Feb 20, 2005 8:45 pm
Projects: NaNoRenO 2016, Ren'Py tutorial series
Location: USA
Contact:

Re: NameError traceback occuring on if statements.

#2 Post by Megaman Z »

Short Answer: slip this in somewhere in your script file, preferably at the top.

Code: Select all

init python:
    choice5 = ""
Long answer: you're not initializing the value of choice5, and because it's only declared mid-game (and only on certain routes), there's the chance you'll run into the situation in which you attempt to access a variable that wasn't defined anywhere along the route you took.
~Kitsune Zeta

Gollier
Newbie
Posts: 2
Joined: Tue Mar 08, 2011 9:46 am
Contact:

Re: NameError traceback occuring on if statements.

#3 Post by Gollier »

Megaman Z wrote:Long answer: you're not initializing the value of choice5, and because it's only declared mid-game (and only on certain routes), there's the chance you'll run into the situation in which you attempt to access a variable that wasn't defined anywhere along the route you took.
I thought it might've been something along those lines! Thank you, the game runs all the way through now.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], decocloud, Google [Bot]