Issue with "if" statements [CLOSED]

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
cirnoe
Newbie
Posts: 23
Joined: Fri Sep 04, 2015 1:28 pm
Contact:

Issue with "if" statements [CLOSED]

#1 Post by cirnoe » Sat Sep 05, 2015 11:00 am

Hi! I feel like I'm asking too many questions within too short a time, but I hope someone can help me again :)
I'm having trouble with a particular "if" statement.
if transport == "subway":
if where == "corridor":
jump o80

if where == "waiting rm":
jump o80

if where == "lounge":
jump o81

if where == "office":
jump o81

As you can see, I added even more separations after the first if statement to fit it better with the story. I had other "if" statements other than this one, for example:
if transport == "bus":
j n "redacted"
scene bg bus
with dissolve
s "redacted"
show s5

These worked perfectly fine, and I was wondering if maybe it wasn't possible to add even more "if" statements below. Thanks in advance! :(

Here's the traceback:
I'm sorry, but an uncaught exception occurred.

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

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

Full traceback:
File "game/script.rpy", line 1252, in script
if transport == "rail":
File "/Users/rgs/Desktop/applications/RENPY/renpy/ast.py", line 1612, in execute
if renpy.python.py_eval(condition):
File "/Users/rgs/Desktop/applications/RENPY/renpy/python.py", line 1482, in py_eval
return eval(py_compile(source, 'eval'), globals, locals)
File "game/script.rpy", line 1252, in <module>
if transport == "rail":
NameError: name 'transport' is not defined

Darwin-14.5.0-x86_64-i386-64bit
Ren'Py 6.99.5.602
Joelle BD 0.0
Last edited by cirnoe on Sun Sep 06, 2015 3:53 am, edited 1 time in total.

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: Issue with "if" statements

#2 Post by trooper6 » Sat Sep 05, 2015 11:25 am

You should put your code inside [ code][ /code] tags so we can see your indentation (take out the extra spaces in the tags).

Also, quick question, do you initialize your variables, like transport, at the beginning of the game?
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

User avatar
Darim
Regular
Posts: 67
Joined: Sun Jun 21, 2015 4:17 pm
Organization: Setsuna Ken
Github: SetsunaKen
Location: Germany
Contact:

Re: Issue with "if" statements

#3 Post by Darim » Sat Sep 05, 2015 11:27 am

Looks like your variable "transport" is missing or not defined correctly.

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: Issue with "if" statements

#4 Post by trooper6 » Sat Sep 05, 2015 11:30 am

Darim wrote:Looks like your variable "transport" is missing or not defined correctly.
Yeah, that looks like the primary problem...but I'm also concerned about the indentation of those nested if lines.

Also, could you post the code where the problem is actually occurring: the error code is pointing to a problem with the line that reads

Code: Select all

if transport == "rail"
on line 1252
You didn't post those lines. You should post those lines (as well as the place where you initially define transport.
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

User avatar
Darim
Regular
Posts: 67
Joined: Sun Jun 21, 2015 4:17 pm
Organization: Setsuna Ken
Github: SetsunaKen
Location: Germany
Contact:

Re: Issue with "if" statements

#5 Post by Darim » Sat Sep 05, 2015 11:53 am

You can add If Statements as much as you want. Also the elif statement allows you to check multiple expressions.

Code: Select all

if transport == "bus":
    e "That's cool!"

elif where == "coridor":
    jump o80

else:
    jump end
Example 2

Code: Select all

if transport == "bus":
    if where == "coridor":
        jump o80

else:
    jump end

Code: Select all

if transport == "bus" and where == "coridor":
    jump o80

else:
    jump end

User avatar
cirnoe
Newbie
Posts: 23
Joined: Fri Sep 04, 2015 1:28 pm
Contact:

Re: Issue with "if" statements

#6 Post by cirnoe » Sun Sep 06, 2015 1:45 am

This is the code where there actually is a problem in the game

Code: Select all

  if transport == "subway":
            j n "I'm gonna take the subway..."
            if where == "corridor":
                jump o80

            if where == "waiting rm":
                jump o80
         
            if where == "lounge":
                jump o81

            if where == "office":
                jump o81
                
        label o80:
            j n "redacted."
            scene bg mrt
            with dissolve
            g "redacted"
            show f3 at left 
            with dissolve
            j happy "redacted"
            show geng at right
            with dissolve
            j happy "redacted
            scene black 
            with fade
            jump lala
This is the code with the rail (which works when i test out the game..)

Code: Select all

   if transport == "rail":
            j n "redacted."
            scene bg rail
            with dissolve
            show t2
            with dissolve
            t "redacted"
            j happy "redacted"
            t "redacted"
Lastly, here is where I defined it.

Code: Select all

    menu:
        "Rail.":
            $ transport = "rail"
            jump o1931
            
        "Taxi.":
            $ transport = "taxi"
            jump o2222
                
        "Bus.":
            $ transport = "bus"
            jump o3333
            
        "Subway.":
            $ transport = "subway"
            jump o4444
    
I hope this makes it clearer! :D

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: Issue with "if" statements

#7 Post by trooper6 » Sun Sep 06, 2015 3:17 am

So a couple of things.

According to you, you never initialize your variable. You should always initialize your variable. Since you are using the most recent Renpy, I'd use this by adding:

Code: Select all

default transport = None
Right before your start label...or whatever you would like the default value to be.

Next. I noticed in your problem code that your label o80 was indented, but it shouldn't be.
All items that are at the same level organizationally should be indented the same amount. And labels are at 0 indentation. Notice that your start label isn't indented at all. Your other labels should be indented either.

So something more like:

Code: Select all

default transport = None

label start:
    if transport == "subway":
        j n "I'm gonna take the subway..."
        if where == "corridor":
            jump o80

        if where == "waiting rm":
            jump o80
         
        if where == "lounge":
            jump o81

        if where == "office":
            jump o81
                
label o80:
    j n "redacted."
    scene bg mrt
    with dissolve
    g "redacted"
    show f3 at left 
    with dissolve
    j happy "redacted"
    show geng at right
    with dissolve
    j happy "redacted
    scene black 
    with fade
    jump lala
Also, just check to make sure your indents are right. It looks, from your code posted, that some things are indented twice when they should only be indented once.
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

User avatar
cirnoe
Newbie
Posts: 23
Joined: Fri Sep 04, 2015 1:28 pm
Contact:

Re: Issue with "if" statements

#8 Post by cirnoe » Sun Sep 06, 2015 3:53 am

Thank you very much! I fixed the issues and it works perfectly now <3

Post Reply

Who is online

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