[SOLVED] Name not defined (if statement error)

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
pukepup
Newbie
Posts: 3
Joined: Sat Oct 10, 2015 10:26 pm
Contact:

[SOLVED] Name not defined (if statement error)

#1 Post by pukepup »

Hey, I've been working on this for a hellish amount of time and I am at the end of my rope. I'm new to python and Ren'Py coding and so please be easy on my and make it as simple as possible?? I don't now, I'm really frustrated,,

Here's the error that I've been getting:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 184, in <module>
    if deer == "silent":
NameError: name 'deer' is not defined

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

Full traceback:
  File "lib/windows-i686/script.rpyc", line 184, in script
  File "C:\Users\Anais Verhoeven_2\Desktop\renpy-6.99.6-sdk\renpy\ast.py", line 1612, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\Anais Verhoeven_2\Desktop\renpy-6.99.6-sdk\renpy\python.py", line 1477, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\Anais Verhoeven_2\Desktop\renpy-6.99.6-sdk\renpy\python.py", line 1472, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 184, in <module>
    if deer == "silent":
NameError: name 'deer' is not defined

Windows-7-6.1.7601-SP1
Ren'Py 6.99.6.739
prince 0.0
It's REALLY irritating bc I have a line of coding that proves it DOES exist? Which is here:

Code: Select all

        "Stay Silent": 
            $ deer = "silent" #CHANGE THIS SOMEHOW???
            $ w_relationship += 2
            centered "The sound of foodsteps is persistant"
            centered "You causiously reach out a hand"
And when I finally use the if statement is here:

Code: Select all

        "Run!": #THREE
            label run:
            $ constitution -= 1
            stop music fadeout 1.0
            centered "Every branch slaps against your face"
            centered "There are no sounds"
            centered "Just the stinging of pain and the rush of blood in your ears"
            centered "Eventually"
            play music "music/mountain_stream.mp3" fadein 3.0
            centered "The sound of water becomes loud"
            centered "You come to a stop"
            if deer == "silent":
                jump wood_man
            else:        
                label you_are_alone:
                centered "What do you do?"
Honestly I'm really new to this and I sorta jumped in headfirst bc that's how I learn the best. But for all my researching and everything I just can't seem to find a solution to this. It will load everything up until that if statement and then an exception will occur...this is when the if statement isn't even being used (when it is, it works fine) If you need to see more of my coding, let me know

thank you thank you thank you
Last edited by pukepup on Fri Oct 23, 2015 2:00 pm, edited 1 time in total.

User avatar
Jae
Regular
Posts: 192
Joined: Sun Sep 13, 2015 5:41 pm
Projects: Pokémon Academy Life
Location: New York, NY
Contact:

Re: Name not defined (if statement error)

#2 Post by Jae »

Just put $ deer = "silent" in a init block in your script.rpy to start. If you put it somewhere else you risk it not triggering at one point or another (I don't know how you've laid everything out).

My guess is that while you're testing, you reach a point where $ deer = "silent" does not occur, which causes problems in later lines, resulting in the game telling you it doesn't know what "deer" means.

If I take an example script.rpy file I have, you can see the first few hundred lines are all variable definitions. That way they start off immediately defined and I can freely manipulate them how I want no matter at what stage of the game I'm in.

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: Name not defined (if statement error)

#3 Post by trooper6 »

Since you are using the most recent Renpy, don't put $deer = "silent" in an init block, doing that will sometimes cause problems with saving and rollback.

Instead, before your start block, somewhere at the beginning of your script.rpy file, put:
default deer = "silent"
(or whatever you want your default to be).

If you do it this way, it should solve your problem, and you shouldn't have any strange save or rollback errors.
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

pukepup
Newbie
Posts: 3
Joined: Sat Oct 10, 2015 10:26 pm
Contact:

Re: Name not defined (if statement error)

#4 Post by pukepup »

trooper6 wrote:Since you are using the most recent Renpy, don't put $deer = "silent" in an init block, doing that will sometimes cause problems with saving and rollback.

Instead, before your start block, somewhere at the beginning of your script.rpy file, put:
default deer = "silent"
Doing that fixes the error, but it still runs through the some dialogue that should be hidden unless you trigger it by picking certain previous questions. How would I go about using it as an if statement still? Sorry for all the questions!

for example theres three questions and two take you to the same place but only one of those two can see that certain dialogue. I hope I'm making sense,,

User avatar
Jae
Regular
Posts: 192
Joined: Sun Sep 13, 2015 5:41 pm
Projects: Pokémon Academy Life
Location: New York, NY
Contact:

Re: Name not defined (if statement error)

#5 Post by Jae »

You'll still need to change the variable prior to the dialogue. The solution posted above simply solves the defining issue that you got. Now your game for sure knows what "deer" is. You should be able to freely change it to whatever value you wish and base your if/else statements from there.

philat
Eileen-Class Veteran
Posts: 1909
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Name not defined (if statement error)

#6 Post by philat »

If I'm understanding you correctly, you should be doing the following (all properly indented for where it is, of course).

The key thing you have to understand is that if you want to check against a variable, that variable has to exist. What you were doing is, if the player chooses an option, deer was set to "silent", but otherwise, deer didn't exist. I can see how it's easy to assume that if you check against a nonexistent variable, it will return false, but in python, it doesn't return false, it just throws an error. So what you should do is default the variable to False or None, and then change it as you see fit in the flow of the game -- that way, when you check against the default variable, it returns false.

Code: Select all

default deer = None

menu:
    "Blah":
        "blah blah"
    "Stay silent":
        $ deer = "silent"

if deer == "silent":
    "unlockable dialogue"
else:
    "regular dialogue"

pukepup
Newbie
Posts: 3
Joined: Sat Oct 10, 2015 10:26 pm
Contact:

Re: Name not defined (if statement error)

#7 Post by pukepup »

philat wrote:

Code: Select all

default deer = None

menu:
    "Blah":
        "blah blah"
    "Stay silent":
        $ deer = "silent"

if deer == "silent":
    "unlockable dialogue"
else:
    "regular dialogue"
This worked PERFECTLY! Thank you so much!

User avatar
jQ_theeNHK
Regular
Posts: 25
Joined: Thu Mar 24, 2016 2:23 pm
Projects: theNetwork
Location: Sin City, Las Vegas, NV
Contact:

Re: [SOLVED] Name not defined (if statement error)

#8 Post by jQ_theeNHK »

I was stuck on this issue for about an hour before I found this post! Thank you!

User avatar
scorlight
Regular
Posts: 67
Joined: Fri Sep 13, 2013 7:02 am
Contact:

Re: [SOLVED] Name not defined (if statement error)

#9 Post by scorlight »

Me too!!! Love you guys!!!!! TTvTT)
Artist for rent, see my art here: http://scorlight.deviantart.com/gallery/

Post Reply

Who is online

Users browsing this forum: No registered users