Conditional Statements Not Working? [Solved]

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
User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Conditional Statements Not Working? [Solved]

#1 Post by yon »

Me again. I have a feeling I'll be making a lot of posts here since I can't seem to understand the documentation (that is, I can't take the information from it and use it to solve my problems, which is probably an issue on my end and not the documentation's.)

I'm trying to jump to a separate file when you reach another chapter in the game, and then define what route you're on based on the points of the first chapter. But it always defaults to one specific route, even if I didn't do anything to activate it.

Essentially, it's like this:

Code: Select all

label start:

    $ an_pre = 0
    $ az_pre = 0
    $ mi_pre = 0
    $ s_pre = 0
    $ y_pre = 0
    $ an_r = False
    $ az_r = False
    $ mi_r = False
    $ s_r = False
    $ y_r = False

label Chapter_01:

    menu:
        "Wrong answer":
            pass
        "Right answer":
            $ y_pre += 1
In which the x_pre variables are preliminary points. Repeat for all characters.
By the time you finish chapter 01, you'll have X amount of points, and whichever route has the most is the one you'll be on for the rest of the game. But even if I specifically avoid choosing any options that would increase the amount of an_pre points, it always proceeds as though I did. I think this is most likely because the conditional statement regarding the an_pre points when it decides what route you're on is on top.

So, I think there are potentially two things I'm doing wrong here. The conditional statements (most likely), or somehow I messed up the jump procedure. I've tried a few things, so I don't THINK it's the latter, but I'll show what I did anyway.

At the end of script.rpy:

Code: Select all

    "End of Chapter 01."

    jump Chapter_02
And then the beginning of Chapter_02.rpy:

Code: Select all

label Chapter_02: 
    
    "Let's see if it worked...!"
    
    if an_pre >= az_pre and mi_pre and s_pre and y_pre:
        $ an_r = True
    elif az_pre >= an_pre and mi_pre and s_pre and y_pre:
        $ az_r = True
    elif mi_pre >= an_pre and az_pre and s_pre and y_pre:
        $ mi_r = True
    elif s_pre >= an_pre and az_pre and mi_pre and y_pre:
        $ s_r = True
    elif y_pre >= an_pre and az_pre and mi_pre and s_pre:
        $ y = True
    
    if an_r:
        "Anisha's route!"
    elif az_r:
        "Azami's route!"    
    elif mi_r:
        "Mori's route!"
    elif s_r:
        "Snake's route!"  
    elif y_r:
        "Yui's route!"
                
return
So I know I did something wrong, and I have a good idea of what I did wrong, but I'm not sure how to fix it. Thanks in advance for dealing with my wreck of a code.
Last edited by yon on Tue Oct 21, 2014 9:24 pm, edited 1 time in total.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Conditional Statements Not Working?

#2 Post by PyTom »

The condition:

Code: Select all

an_pre >= az_pre and mi_pre and s_pre and y_pre:
Is equivalent to:

Code: Select all

if (an_pre >= az_pre) and (mi_pre != 0) and (s_pre != 0) and (y_pre != 0):
Which doesn't seem what you want. To find the max, you probably want:

Code: Select all

$ max_ pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)

if an_pre == max_pre:
    $ an_r = True
elif az_pre == max_pre:
    $ az_r = True
# ...
In the case of a tie, the first condition wins.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#3 Post by yon »

Would that only choose the route with the maximum amount of points, or just which one has most?

For example, it's possible to end Chapter 01 with

an_pre = 3
az_pre = 0
mi_pre = 0
s_pre = 1
y_pre = 0

The maximum amount of points each one can get is 4, in that there are only 4 opportunities to get a point for each character. If I used your code, would it choose the an route even though it doesn't have the maximum number of points?

Also, I assume I would plug that

Code: Select all

$ max_ pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)

part into the area below the start label? Or would I need to plug that into the beginning of Chapter_02.rpy

Thank you for helping out.

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#4 Post by yon »

I tried putting the code in and it gave me this.

Code: Select all

I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 111: invalid syntax
    max_ pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)
            ^
    

Ren'Py Version: Ren'Py 6.16.1.409
 
I also tried putting it in Chapter_02.rpy. Same error. Any ideas?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Conditional Statements Not Working?

#5 Post by PyTom »

You left out the $ at the start of it. Also, I apparently typed an extra space - see the corrected version below.

The code I gave should be placed right before when it's needed. The line:

Code: Select all

$ max_pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)
is a computation that sets max_pre to the current maximum out of the 5 pre variables. The if statement then checks to find one of the variables that's equal to this maximum.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#6 Post by yon »

It isn't giving me errors anymore, but it still doesn't work quite right.

Even though I make sure to avoid any and all menu choices that would give me +1 for an_pre or mi_pre, now it shows me that I've gotten onto both of them, somehow.

I'll paste the full code for Chapter_02.rpy, which is when things get tripped up, it seems.

Code: Select all

label Chapter_02: 
    
    "Let's see if it worked...!"

    if an_pre == max_pre:
        $ an_r = True
    elif az_pre == max_pre:
        $ az_r = True
    if mi_pre == max_pre:
        $ mi_r = True
    elif s_pre == max_pre:
        $ s_r = True
    if y_pre == max_pre:
        $ y_r = True

    if an_r == True:
        an "Dog"
    if az_r == True:
        az "Dog"    
    if mi_r == True:
        mi "Dog"    
    if s_r == True:
        s "Dog"
    if y_r == True:
        y "Dog"

return
For testing purposes, I decided to add in that part at the end so I could see what route it put me on. (I just wrote the first thing that came to mind)
As I said above, I didn't choose any menu choices that would've given me +1 for an_pre. In fact, the events where mi_pre is modified at all aren't even available right now. But it still proceeds as though an_r and mi_r are true, when they should not be.

Did I do something wrong?

[EDIT]

I just realized I only put elif on a few of the lines in Chapter_02.rpy, for some reason. I'll fix that now.

[EDIT 2]

It still has an_r as true, for some reason, but now it doesn't think mi_r is true. It should have y_r as true, but it doesn't, and just proceeds as if an_r is no matter what. Is it because it's in a separate .rpy file that's called?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Conditional Statements Not Working?

#7 Post by PyTom »

Three things:

- You want to use elif, rather than if, for all but the first condition.
- You probably want to specify what happens when all variables are zero.
- You can inline the response code, and lose the _r variables. (You can keep them if you want, but why?)

How about:

Code: Select all

label Chapter_02: 
    
    "Let's see if it worked...!"

    $ max_pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)
    
    if max_pre == 0:
        "Cat"
    elif an_pre == max_pre:
        an "Dog"
    elif az_pre == max_pre:
        az "Dog"
    elif mi_pre == max_pre:
        mi "Dog"
    elif s_pre == max_pre:
        s "Dog"
    elif y_pre == max_pre:
        y "Dog"
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: Conditional Statements Not Working?

#8 Post by Milkymalk »

Try adding

Code: Select all

"[an_pre] [az_pre] [mi_pre] [s_pre] [y_pre]"
before the if statements to check the actual values. That way you can see if the values are what you expect them to be. If they aren't, you have to go bug hunting.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#9 Post by yon »

- You probably want to specify what happens when all variables are zero.
I've actually set it up so that by the end of Chapter 01, there should be at least 1 point towards one route. Do you think it's necessary even at that?
- You can inline the response code, and lose the _r variables. (You can keep them if you want, but why?)
The r was supposed to be short for route, unless that's what you meant and I'm not getting it? I mean, the preliminary points were supposed to be just that, preliminary. I'd like to code it so that when one route has enough prelim points, that route is then switched on. Again, sorry if you meant that and I'm not getting it.

Would that code you suggested do what I have in mind?
Try adding

Code: Select all

"[an_pre] [az_pre] [mi_pre] [s_pre] [y_pre]"
before the if statements to check the actual values. That way you can see if the values are what you expect them to be. If they aren't, you have to go bug hunting.
I'll try that. I assume you mean to add it into the Chapter_02.rpy file, right? That's what I'll put it in.

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#10 Post by yon »

I tried putting in the code Milkymalk suggested and it turned out well! I didn't quite get what you meant at first, but I get it now. It literally tells you how many points each has.

The points are as follows:

an - 0
az - 1
mi - 0
s - 0
y - 4

Therefore, the an_r should definitely NOT be true, even though it proceeds as though it is. Something very not good is happening in my code.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Conditional Statements Not Working?

#11 Post by PyTom »

What's your current version of the code look like?

And could you put this after the line that sets max_pre?

Code: Select all

"an=[an_pre] az=[az_pre] mi=[mi_pre] s=[s_pre] y=[y_pre] max=[max_pre]"
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#12 Post by yon »

Would you like me to post all of script.rpy? It may be kind of long, but I could just cut to the chase and focus on the parts where lines of code are involved.

I'll post a screenshot as soon as I put it in.

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#13 Post by yon »

I put this:

Code: Select all

"an=[an_pre] az=[az_pre] mi=[mi_pre] s=[s_pre] y=[y_pre] max=[max_pre]"
part in and I got this out:

Image

(I haven't put any graphics or anything in yet, so it's just a black screen for right now.)

For some reason it has max=0? Is that why I'm getting this weird issue?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Conditional Statements Not Working?

#14 Post by PyTom »

Yes, it's very likely you have the

Code: Select all

    $ max_pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)
It should be right before the first if block (the one that compares this to max_pre).
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
yon
Regular
Posts: 174
Joined: Tue Sep 09, 2014 5:09 pm
Projects: YDSP
Soundcloud: tetra-yon
itch: v-ual
Location: United States
Contact:

Re: Conditional Statements Not Working?

#15 Post by yon »

Alright, I'll put in the relevant lines of code from script.rpy.

Code: Select all

label start:

    $ an_pre = 0
    $ az_pre = 0
    $ mi_pre = 0
    $ s_pre = 0
    $ y_pre = 0
    $ max_pre = max(an_pre, az_pre, mi_pre, s_pre, y_pre)
    $ an_r = False
    $ az_r = False
    $ mi_r = False
    $ s_r = False
    $ y_r = False    
  
[...]

    menu:
        "\(Help her down.\)":
            [...]
        "\(Take a photo.\)":
            $ y_pre += 1
            [...]

[...]

    menu:
        "\(Leave that place.\)":
                [...]
        "\(Enter the room.\)":
                $ an_pre += 1
                [...]

[...]

    menu:
        "Grades don't matter that much.":
                $ az_pre += 1
                [...]
        "Yeah, sort of.":
                [...]

[...]

    menu:
        "Basically.":
                [...]
        "I thought I'd look at it just in case I get called on.":
                $ mi_pre += 1
                [...]
[...]

    menu:
        "Yeah.":
            [...]
        "I could've dealt with them myself.":
            $ s_pre += 1
            [...]

[...]

    menu:
        "Yeah.":
                $ mi_pre += 1
                [...]
        "Nah.":
                [...]

[...]

    menu:
        "Because they didn't have any body.":
                $ y_pre += 1
                [...]
        "I don't know, why.":
                [...]

[...]

    menu:
        "\(Give respect.\)":
                $ an_pre += 1
                [...]
        "\(Deny respect.\)":
                [...]

[...]

    menu:
        "Fine.":
                $ an_pre += 1
                [...]
        "I'm busy.":
                [...]

[...]

    menu:
        "I'd be no use.":
                [...]
        "Sort of.":
                $ az_pre += 1
                [...]

[...]

    menu:
        "I'm looking for a fight.":
                [...]
        "I was thinking about napping there":
                $ s_pre += 1
                [...]

[...]

    menu:
        "We're outnumbered, Yui.":
                $ s_pre += 1
                [...]
        "I bet you could kick her ass, Yui.":
                $ y_pre += 1
                [...]

[...]

    menu:
        "Because boys are gross.":
                $ mi_pre += 1
                [...]
        "Because we work harder.":
                [...]

[...]

    menu:
        "Sure.":
                [...]
        "It's not that important.":
                $ az_pre += 1
                [...]

[...]

    menu:
        "Being an adult sounds like it sucks.":
                [...]
        "I don't think that's right.":
                $ mi_pre += 1
                [...]

    menu:
        "I was just leaving.":
                [...]
        "...I'd like to get in on this.":
                $ s_pre += 1
               [...]

[...]

    menu:
        "To be even more powerful than you.":
                $ y_pre += 1
                [...]
        "To try and be happy.":
               [...]

[...]

    menu:
        "Azami's right.":
                $ az_pre += 1
                [...]
        "Anisha's right.":
                $ an_pre += 1
                [...]

    jump Chapter_02
return
And here's all of Chapter_02.rpy:

Code: Select all

label Chapter_02: 
    
    "Let's see if it worked...!"
    
    "an=[an_pre] az=[az_pre] mi=[mi_pre] s=[s_pre] y=[y_pre] max=[max_pre]"
    
    if an_pre == max_pre:
        $ an_r = True
    elif az_pre == max_pre:
        $ az_r = True
    elif mi_pre == max_pre:
        $ mi_r = True
    elif s_pre == max_pre:
        $ s_r = True
    elif y_pre == max_pre:
        $ y_r = True

    if an_r == True:
        an "Dog"
    if az_r == True:
        az "Dog"    
    if mi_r == True:
        mi "Dog"    
    if s_r == True:
        s "Dog"
    if y_r == True:
        y "Dog"

return

Post Reply

Who is online

Users browsing this forum: Andredron