Ren'Py - Loop through If statement

bobby bates

New Member
Sep 16, 2018
14
2
Is there a way to loop through an if statement?

I'm using imagebuttons because in my game the user navigates by interacting with objects on screen like doors etc.

I have a situation where I have set the scene, and there's 5 doors to choose from. 4 doors are useless and basically say no answer. The 5th door jumps to another label.

My issue is this, after the first door is clicked, it displays the text …. no answer and then continues on with the next block.

There is a lot more lines of code above this. and I don't want the user moving on until the right door is clicked.

after every wrong door I would like the user to be returned to "screen HallwayIntro" part in the code below.

Code Snippit:

Code:
while True:
      call screen HallwayIntro
    
    if _return == "LivingRoom":
        scene RentedHouse1_LivingRoom
        
        if SBI == 0:
            "So, this is the living room, Sam said Grey door right in front of me"
        elif SBI == 1:
            "Oh my god, more doors!!, this is what happens when you act like a cock"
            jump Renting1_LivingRoom
    elif _return == "S_room":
        "Knock Knock"
        ".........."
        "Hmmm, no answer"
    elif _return == "mc_room":
        "Boom Boom"
        "Hmm... that sounds empty."
    elif _return == "j_room":
        "Knock Knock"
        "........."
        "Hmmm, no answer"

and the screen HallwayIntro is this:

Code:
screen HallwayIntro:
    imagebutton auto "rentedhome1/hallway/door4_morning_%s.png" xpos 1660 ypos 0 focus_mask True action Return("LivingRoom")
    
    if SBI == 1:
        imagebutton auto "rentedhome1/hallway/door1_morning_%s.png" xpos 178 ypos 166 focus_mask True action Return("S_room")
        imagebutton auto "rentedhome1/hallway/door2_morning_%s.png" xpos 641 ypos 225 focus_mask True action Return("mc_room")
        imagebutton auto "rentedhome1/hallway/door3_morning_%s.png" xpos 1280 ypos 200 focus_mask True action Return("j_room")
[\CODE]

Any suggestions are welcome.

Thanks
 
Oct 30, 2018
395
1,169
My python is rustier than 20 year old nails so forgive me if I'm wrong here, but if all you want to do is return to the beginning of the while loop cant you use the "continue" statement?
 
  • Like
Reactions: bobby bates

bobby bates

New Member
Sep 16, 2018
14
2
My python is rustier than 20 year old nails so forgive me if I'm wrong here, but if all you want to do is return to the beginning of the while loop cant you use the "continue" statement?
I thought that too, but Ren'Py doesn't quite understand it.

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


File "game/welcome.rpy", line 244: expected statement.
    continue
            ^

File "game/welcome.rpy", line 248: expected statement.
    continue
            ^

File "game/welcome.rpy", line 253: expected statement.
    continue
            ^

Ren'Py Version: Ren'Py 7.1.1.929
Thu Dec 13 21:39:38 2018
 
Oct 30, 2018
395
1,169
Right, so I did a little research and the continue statement won't work because as you have said Ren'Py doesn't understand it. Ren'Py doesn't actually support any of the traditional loop control statements (no continue, break or for).

Instead they suggest that you write a statement that jumps to a label created before or after the loop takes place.
So in your case I suppose write a label just before the loop and jump back to it in lieu of a continue.
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
Exactly. All you have to do is:

Code:
label offer_door_choice:          # Change "while" to a label
      call screen HallwayIntro
   
    if _return == "LivingRoom":
        scene RentedHouse1_LivingRoom
       
        if SBI == 0:
            "So, this is the living room, Sam said Grey door right in front of me"
        elif SBI == 1:
            "Oh my god, more doors!!, this is what happens when you act like a cock"
            jump Renting1_LivingRoom
    elif _return == "S_room":
        "Knock Knock"
        ".........."
        "Hmmm, no answer"
    elif _return == "mc_room":
        "Boom Boom"
        "Hmm... that sounds empty."
        jump offer_door_choice
    elif _return == "j_room":
        "Knock Knock"
        "........."
        "Hmmm, no answer"
    jump offer_door_choice

label Renting1_LivingRoom:
    ....
If you don't hit the case where you jump down to Renting1_LivingRoom, you'll end up at the "jump offer_door_choice" statement and start over again.

Think of it as an old-fashioned loop created with "goto" statements. (If you're old enough to remember "goto" statements... LOL)
 
  • Like
Reactions: bobby bates

bobby bates

New Member
Sep 16, 2018
14
2
that's crap :( would have been easier lol

Instead they suggest that you write a statement that jumps to a label created before or after the loop takes place.
So in your case I suppose write a label just before the loop and jump back to it in lieu of a continue.
Yeah I found this as well, I hoping to do this without having to leave the code block.

And as I type this, I think I just might have achieved it!, only been at it for the past 2 hours lol.

In case anyone is intrested this is how managed to loop through a while / if without having to leave the code block.

Code:
$ Enter = False

    while Enter == False:
        call screen HallwayIntro
  
        if _return == "LivingRoom":
            scene RentedHouse1_LivingRoom
            if SBI == 0:
                "So, this is the living room, Sam said Grey door right in front of me"
            elif SBI == 1:
                "Oh my god, more doors!!, this is what happens when you act like a cock"
                $ Enter = True
        elif _return == "S_room":
            "Knock Knock"
            ".........."
            "Hmmm, no answer"
        elif _return == "mc_room":
            "Boom Boom"
            "Hmm... that sounds empty."
        elif _return == "j_room":
            "Knock Knock"
            "........."
            "Hmmm, no answer"
  
    "Story goes on"
thanks for replying Dicks McKenzie much obliged :)
 

bobby bates

New Member
Sep 16, 2018
14
2
Think of it as an old-fashioned loop created with "goto" statements. (If you're old enough to remember "goto" statements... LOL)
haha, unfortunately I am lol

thanks for your reply :) I think I just been at it for a few hours and had a brain fart, just couldn't work it out lol
 

Rich

Old Fart
Modder
Donor
Respected User
Game Developer
Jun 25, 2017
2,566
7,382
haha, unfortunately I am lol

thanks for your reply :) I think I just been at it for a few hours and had a brain fart, just couldn't work it out lol
Been there, done that. And thank you for being someone other than me who uses the term "brain fart..." LOL
 
  • Like
Reactions: bobby bates

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,968
16,222
would have been easier lol
I agree :
Code:
    while True:
You don't need more since you jump out of the label if the door is right.

Edit: My bad, you jump only on one branch of the second if. Sorry.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,608
2,256
You already have multiple solutions... so this isn't really needed...

but, would this also do what you were looking for?
Python:
    $ _return = ""

    while not(_return == "LivingRoom" and SBI == 1)
        call screen HallwayIntro
     
        if _return == "LivingRoom":
            scene RentedHouse1_LivingRoom
     
            if SBI == 0:
                "So, this is the living room, Sam said Grey door right in front of me"
            elif SBI == 1:
                "Oh my god, more doors!!, this is what happens when you act like a cock"
        elif _return == "mc_room":
            "Boom Boom"
            "Hmm... that sounds empty."
        else:
            "Knock Knock"
            "........."
            "Hmmm, no answer"

label Renting1_LivingRoom:
    {more code}
You'd have to ensure both "_return" and "SBI" couldn't ever be those values before reaching the "while" statement. Hence the setting of _return before the while. (assuming RenPy doesn't object to setting internal variables.)

I've also simplified your multiple rooms with the same answer into a single "else" statement. Though my brain is yelling something at me about mixing "elif" and "else" statements... my fingers however carried on typing.

Personally, I'd have done the label and "goto" solution for sheer simplicity. But if you absolutely must find a reason to use "while", this might have worked. (obviously I haven't actually tested this code)