Help with understanding variables in conversation

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
jQ_theeNHK
Regular
Posts: 25
Joined: Thu Mar 24, 2016 2:23 pm
Projects: theNetwork
Location: Sin City, Las Vegas, NV
Contact:

Help with understanding variables in conversation

#1 Post by jQ_theeNHK »

Hello all! I'm having a bit of trouble with my coding and would love another set of eyes to see where I went wrong. I'm working on adding variables into conversation, and this is the code I've input in ren'py:

menu:

" Tell him about the person in the closet.":

$ snitch = "yes"
jump imsayingsomething

" Don't tell him.":

jump notsayinganything


(and further down when the snitch variable comes into play:)


if snitch == "yes":

show bg freighter
with fade

i " A new message? What's this...? {w=0.5} Unknown sender..."
NN " Hello Friend." (then the rest of the convo..)

else:

NN " How is your new life treating you? Dreadfully dull I'm sure.." (then the rest of the convo)


but when I'm playing and get into that part of the story, I keep coming up with this screen:


I'm sorry, but an uncaught exception occurred.

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

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

Full traceback:
File "game/script.rpy", line 4486, in script
if snitch == "yes":
File "E:\Ren'py\renpy-6.99.8-sdk\renpy-6.99.8-sdk\renpy\ast.py", line 1648, in execute
if renpy.python.py_eval(condition):
File "E:\Ren'py\renpy-6.99.8-sdk\renpy-6.99.8-sdk\renpy\python.py", line 1490, in py_eval
return py_eval_bytecode(code, globals, locals)
File "E:\Ren'py\renpy-6.99.8-sdk\renpy-6.99.8-sdk\renpy\python.py", line 1485, in py_eval_bytecode
return eval(bytecode, globals, locals)
File "game/script.rpy", line 4486, in <module>
if snitch == "yes":
NameError: name 'snitch' is not defined

Windows-8-6.2.9200
Ren'Py 6.99.9.1168
theNetwork 0.0


It's saying 'snitch' is not defined but I'm pretty sure I am defining it correctly...? I can post more lines if necessary, but it's a couple hundred lines between when I added the variable and when it comes into play, so I didn't want to post everything.. If I need to add more, please let me know.

And thanks in advance for any help you can send my way! Cheers!

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: Help with understanding variables in conversation

#2 Post by jQ_theeNHK »

It's not showing my indenting when I copy/pasted on the forum for some reason, but I have all the text after Menu: indented, then indented again after my IF and ELSE statements..

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: Help with understanding variables in conversation

#3 Post by jQ_theeNHK »

I was able to figure it out, so I'll post what I did, just in case someone else comes into this same scenario.. I created a "default snitch = False" (must use capital F) command before the "label start:" command at the top, then changed my "$ snitch = yes" to "$ snitch = True", then my IF statement to read "if snitch == True" (don't use quotations in your code) and it works perfectly now. Hope I helped someone. Cheers!

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Help with understanding variables in conversation

#4 Post by kivik »

Just to further explain what happened:

In your original code you only defined the snitch variable within the menu statement. I'm not sure whether it crashed for you when you chose the alternative path only, if so, that would be because your script never reached the snitch define statement, and so it crashed.

If it always crashed regardless of the choice, then it means the scope of variables when defined within the menu statement remains "local" to the menu statement, and therefore it ceases to exist outside of the menu scope.

Variables defined in the main game flow are "global", which means they're accessible anywhere - and since you defined it before reaching that part of the game, it will play nicely.


FYI you can shorten your code by using:

Code: Select all

if snitch:
    #do something
It's the same as if snitch == True, as most if not all programming languages evaluates the variable or statement to be true or false: (if variable > 0) is saying if the statement variable larger than 0 is true; (if not variable > 0) is saying if the statement variable not larger than 0 is true. etc.. etc...


Also if you want to paste codes in the future, use the forum's

Code: Select all

 tag, it'll keep all the indentations :)

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: Help with understanding variables in conversation

#5 Post by jQ_theeNHK »

Thank you Kivik, I appreciate the added explanation. I went back and checked and you are correct in that it only crashed when I chose the alternate path.

Thank you as well for the tutorial on the use of the

Code: Select all

 tag! ;)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]