The 'If' Statement

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.
Message
Author
Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

The 'If' Statement

#1 Post by Requiem »

Ahoy ^_^

I'm having a little trouble figuring out how to use the 'if' statement. Earlier in my script, I wrote:

Code: Select all

menu:
        "Yes.":
            jump Yes
        "No.":
            jump No
And then later in the story, I wrote:

Code: Select all

$ if Yes == True:
         This happens... blah blah etc

But it's not working at all? Instead of launching the game, I get the traceback:

Code: Select all

Exception: Syntax error on line 882 of H:\Falling Cherry Blossoms/game/script.rpy:
    if yes == True:

While compiling python block starting at line 882 of H:\Falling Cherry Blossoms/game/script.rpy.
Can anyone help me, or run through how to actually use the 'if' statement? I'm really lost :cry:

CaesMRaenes
Regular
Posts: 132
Joined: Wed Aug 19, 2009 12:42 am
Projects: 軽い夏, The Aces
Contact:

Re: The 'If' Statement

#2 Post by CaesMRaenes »

Is your "yes" in the IF statement the same "Yes" in the initiated variable? The problem could be with the capital letter.

Remember: the code is CAP sensitive so "Yes" is not "yes", "yEs", or "yeS". "Yes" can only be "Yes". I hope that makes sense. :P
a.k.a. DragonfaeryYume on dA
The Vault in the Sky (Dev Blog)

Projects:
WIP The Aces
WIP (Head Artist) 軽い夏: The Sun Will Shine Through (tentative title)

fortaat
Regular
Posts: 183
Joined: Tue May 18, 2010 1:16 pm
Contact:

Re: The 'If' Statement

#3 Post by fortaat »

You need to use flags:

http://www.renpy.org/wiki/renpy/doc/tut ... Statements

Read it.
Ask again later if you still need help.

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: The 'If' Statement

#4 Post by Aleema »

You need to set a variable to True or False for you to check it! Jumping to label Yes does not create a variable called Yes that defaults to True. You must do this for Ren'Py.

Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

Re: The 'If' Statement

#5 Post by Requiem »

Ah, okay!

Thanks to all 3 of you, I'll re-read the documentation again and ask again :3

Thanks again ^_^

Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

Re: The 'If' Statement

#6 Post by Requiem »

Bleh... I'm still having trouble.

I initialized the flag at the beginning of the label:

Code: Select all

label start:
    
    $ Yes = False
Then after the user clicks on the option, there's this code:

Code: Select all

label Yes:
    
    $ Yes = True
Then I use the if statement like so:

Code: Select all

if Yes:
    scene Black with fade
But I'm getting the Traceback error of:

Code: Select all

On line 898 of H:\Falling Cherry Blossoms/game/script.rpy: if statement expects a non-empty block.
if Yes:
       ^
Image
A work in progress

vaanknight
Regular
Posts: 118
Joined: Sun Mar 28, 2010 6:01 pm
Completed: Happy Weird-Day! How a freckled fairy can save your world (Or get more freckles in the process...)"
Projects: Nameless 2D Fighting Game
Contact:

Re: The 'If' Statement

#7 Post by vaanknight »

It all looks like it should work... maybe it's an indentation problem. If you put the "if Yes:" code inside the Yes label block, and the "scene black with fade" inside the "if Yes:" statement, it might do the trick. What I mean is:

Code: Select all

label start:
    
    $ Yes = False

label Yes:
    
    $ Yes = True

    if Yes:
        scene Black with fade
bb_lass_full.zip
bb_lass_full.zip
Last edited by vaanknight on Tue Sep 28, 2010 2:24 pm, edited 7 times in total.
Image

Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

Re: The 'If' Statement

#8 Post by Requiem »

Aha, I see what you mean.
I'll give it a shot in just a moment and edit this post with the result. But the if statement is used much further down from where the actual $ Yes = True is, in a totally different label. Is that the problem?
Image
A work in progress

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: The 'If' Statement

#9 Post by Jake »

Requiem wrote:Aha, I see what you mean.
I'll give it a shot in just a moment and edit this post with the result. But the if statement is used much further down from where the actual $ Yes = True is, in a totally different label. Is that the problem?
Honestly, while all the stuff about using variables and case-sensitivity is worth knowing, and would have caused you problems, they wouldn't have caused the actual error you got. A syntax error means that Ren'Py/Python couldn't even read a valid statement out of the line, and happens when the script is first read in by the program... while the problems you'd get from case-sensitivity and not having initialised variables would happen after Ren'Py has made sense of the line, and only actually come out when Ren'Py tries to run that particular line of your script.


Unfortunately, I can't see anything in the quoted line in your first post that would be a syntax error, and - as vaanknight notes - there's nothing wrong with your indentation if the text you showed is verbatim from your script file.

Two things come to mind:
- Firstly, Python depends quite heavily on indentation, so if you have any funny characters (carriage-returns without newlines, maybe, that your editor isn't showing? Odd non-normal space characters?) then they might potentially mess up the parsing. Did you copy and paste any of your script from a website or anything like that? I know I've had trouble in the past when I've copied examples from the web and it's turned out that they had weird invisible layout/formatting characters in. If you have, it might be worth typing a copy of the lines out by hand, and deleting the originals
- Secondly, there's a chance that something on a different line could cause a problem that isn't obvious just from the traceback. With respect to the error you're having now, could you post a chunk of the script maybe ten lines either side of the line with the error? As it is, there's no reason you should have a problem with the small snippet you posted, so long as all the other lines are fine.

(I'm not actually entirely sure it's possible to have a syntax error in Python caused by a problem on another line, but I know it's possible in other languages, so it's worth a look.)
Server error: user 'Jake' not found

chunderbunny
Regular
Posts: 52
Joined: Sat Feb 09, 2008 1:50 pm
Projects: SSS:Renaissance, Canadense (WIP)
Contact:

Re: The 'If' Statement

#10 Post by chunderbunny »

Jake wrote: Unfortunately, I can't see anything in the quoted line in your first post that would be a syntax error, and - as vaanknight notes - there's nothing wrong with your indentation if the text you showed is verbatim from your script file.
...
- Secondly, there's a chance that something on a different line could cause a problem that isn't obvious just from the traceback. With respect to the error you're having now, could you post a chunk of the script maybe ten lines either side of the line with the error? As it is, there's no reason you should have a problem with the small snippet you posted, so long as all the other lines are fine.

(I'm not actually entirely sure it's possible to have a syntax error in Python caused by a problem on another line, but I know it's possible in other languages, so it's worth a look.)
It most certainly is possible, the classic example being an unclosed parenthesis.

Code: Select all

a = 3 * (1 +2
if a == True:
	print 'hello'
Trying to run this example in a test python script will raise the following error:

Code: Select all

  File "./test.py", line 4
    if a == True:
                ^
SyntaxError: invalid syntax
So python reports a problem on line 4, but the actual error occurs on line 3, the unclosed parentheses. It is unlikely that this kind of error will occur more than 2 lines prior to the one it is reported on, so you don't have too much to check.

Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

Re: The 'If' Statement

#11 Post by Requiem »

Jake wrote: Two things come to mind:
- Firstly, Python depends quite heavily on indentation, so if you have any funny characters (carriage-returns without newlines, maybe, that your editor isn't showing? Odd non-normal space characters?) then they might potentially mess up the parsing. Did you copy and paste any of your script from a website or anything like that? I know I've had trouble in the past when I've copied examples from the web and it's turned out that they had weird invisible layout/formatting characters in. If you have, it might be worth typing a copy of the lines out by hand, and deleting the originals
- Secondly, there's a chance that something on a different line could cause a problem that isn't obvious just from the traceback. With respect to the error you're having now, could you post a chunk of the script maybe ten lines either side of the line with the error? As it is, there's no reason you should have a problem with the small snippet you posted, so long as all the other lines are fine.

(I'm not actually entirely sure it's possible to have a syntax error in Python caused by a problem on another line, but I know it's possible in other languages, so it's worth a look.)
Nope, I've completely written the script by hand. And sure, I'll post 10 lines back and forth between the statements.
Here you go ^_^

Flag where it is still false

Code: Select all

$ M = Character('Mai', color="#dfdfdf", show_two_window=True)
    
    # Transitions
    $ longdissolve = Dissolve(3)
    $ fade = Fade(0.5, 0, 0.5)
    

# The game starts here.
label start:

    $ Yes = False
    
    "It was just... a normal day..."
    "Who would've thought it..."
    "That so much could be lost from something so little..."
    
    scene Yurushi Overlook with dissolve
    
    "The sun shone brightly on the town of Yurushi."
    "It was a typical day in this small town."
    "But before I go into the story, I should introduce myself."
Flag where it becomes true

Code: Select all

stop music fadeout 3.0
    
    menu:
        "Yes.":
            jump Yes
        "No.":
            jump No
            
label Yes:
    
    $ Yes = True
    
    show AlbionSpeak1 at right with dissolve
    A "Yeah, sure. I'll help you at lunch."
    show KatSpeak1 with dissolve
    K "Awesome! Thanks for being a {i}proper{/i} friend."
    "Maybe I shouldn't have done that... Shion looks quite mad..."
    scene Black with fade
    stop music fadeout 1.0
    "Sometime later, after Shion had cooled off from me betraying her judgment, we arrived at school. It was all good though, because we had all forgotten about the argument, and Kat was speaking to Shion like normal."
    jump firstdayoutsideschool
Actual If Statement

Code: Select all

show ShionAngry2 at left with dissolve
    S "Ever..."
    show ShionAngry1 at left with dissolve
    hide ShionSad
    S "SPEAK TO ME AGAIN!"
    stop music fadeout 3.0
    hide ShionAngry2
    show ShionAngry1 at right with move
    hide ShionAngry1
    "Shion starts crying again as she pushes past me and leaves the park. I return home, laughing under my breath but still in pain with my nose."
    if Yes:
    scene Black with fade
    "...but as it turns out..."
    show Albion at right with dissolve
    play music "mus/Precious.mp3"
    "Shion moved towns. She was so hurt by Kat, then by me, for being so unkind to her that she asked to leave. It was even worse as Shion's mother was adamant on leaving the town, and this was her excuse to move."
    "I ended up distancing myself from Kat and Mai, because Shion acted like my link to the two."
    "Very soon, I lose my friendship with the two women, and never speak to them again..."
    "...the rest of college life is lonely."
    centered "Bad Ending - 1/??? - {color=CE0700}'The Lonely World'{/color}"
    jump credit
Image
A work in progress

Jake
Support Hero
Posts: 3826
Joined: Sat Jun 17, 2006 7:28 pm
Contact:

Re: The 'If' Statement

#12 Post by Jake »

Requiem wrote: Actual If Statement

Code: Select all

    if Yes:
    scene Black with fade
This isn't the same as the snippet you posted before, and the problem is clearer: you need to indent the lines which depend on the 'if'. I guess when you quoted it previously you'd selected from the beginning of the 'if' instead of the beginning of the line...?

In Python and Ren'Py indentation is important for indicating which block of code is dependent on control statements such as 'if'. If the condition is met, then it will execute the immediately-following indented line/s, and after those, continue with the lines which are indented to the same degree as the 'if' itself. If the condition is not met, then it will skip all the more-indented lines and continue with the next line which is indented to the same degree as the 'if'.

For example:

Code: Select all

  if condition:
    # This line will only occur if the condition is true
    # This line also
  # This line will occur regardless of the condition
Because the 'if' expects there to be at least one line immediately following it which is indented further, if the next line is only indented the same distance, it doesn't know what to do and you get the error you saw.




On a more technical note:
chunderbunny wrote: It most certainly is possible, the classic example being an unclosed parenthesis.

Code: Select all

a = 3 * (1 +2
if a == True:
	print 'hello'
Trying to run this example in a test python script will raise the following error:

Code: Select all

  File "./test.py", line 4
    if a == True:
                ^
SyntaxError: invalid syntax
That isn't the error you get from python code run within Ren'Py, either on a line beginning with '$' or a python block. Instead, you get "On line X of filename.rpy: is not terminated with a newline. (Check strings and parenthesis.)". So put it this way: since it's Ren'Py code that's causing the syntax error, we can discount that possibility, because it would have given a different error.

I can get a Syntax Error out if I have one too many left-brackets on a preceeding line and one too many right-brackets on a following line... but Ren'Py outputs all three lines (the whole logical line, I guess) in the error, so it's obvious that's what's happened.

(But yes, that's the kind of structure I was thinking of; it's certainly quite possible to get a syntax error in most languages I know like that.)
Server error: user 'Jake' not found

Requiem
Newbie
Posts: 13
Joined: Fri Jul 09, 2010 11:45 am
Projects: Falling Cherry Blossoms
Contact:

Re: The 'If' Statement

#13 Post by Requiem »

My god! It actually works!

Thank you so much Jake! :3
Image
A work in progress

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: The 'If' Statement

#14 Post by Showsni »

There are a few other places in the code you posted where it looks like the indentations are wrong,

(namely:

Code: Select all

$ M = Character('Mai', color="#dfdfdf", show_two_window=True)
    
    # Transitions
    $ longdissolve = Dissolve(3)
    $ fade = Fade(0.5, 0, 0.5)
,

Code: Select all

stop music fadeout 3.0
    
    menu:
        "Yes.":
            jump Yes
        "No.":
            jump No
and

Code: Select all

show ShionAngry2 at left with dissolve
    S "Ever..."
) but I'm guessing they're also because you didn't copy from the start of the line, since they all occur at the first line. Make sure to copy the whole line when posting the code!

User avatar
The Monkey Ninja
Regular
Posts: 34
Joined: Mon Jul 05, 2010 5:06 pm
Projects: 3^07, OneTimeValentine, OtomeDesign
Organization: MooseCake Productions
Deviantart: the-monkey-ninja
Location: South Africa
Contact:

Re: The 'If' Statement

#15 Post by The Monkey Ninja »

Again, um, I'm posting a question on here even though I didn't start the topic...^^; I hope that's okay...
um, anyway, my question is also linked to the If statement which is why I am posting here.

okay, so:

Code: Select all

label end:
    
    e "Your result is...!"
    
    if Jin_points >= 5:
        jump good
    
    elif Jin_points >= -5:
        jump bad

    Jin_points >= 0:
        jump oops
I was wondering if there was a third if statement part (elif is the second, and then else is the last but in the case of points being used to determine the game's outcome else can't really be used, yes?)
I tried:

Code: Select all

    if Jin_points >= 5:
        jump good
    
    elif Jin_points >= -5:
        jump bad
        
    else:
        jump oops
But if I do that, then when you should go to the 'oops' label, you go to the 'bad' label and get that ending.
So I wanted to put in a third if command instead of 'else' to stop that.
Is there one? And any idea what it is?
Or maybe a alternate solution?

Thanks ^_^
--
Cloaks are wonderful things~ <3

My DA page for all my art:
http://the-monkey-ninja.deviantart.com/

Ex-Nanoreno2017 game One Time Valentine has a (very rough) demo out now!
viewtopic.php?f=43&t=43314

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot