If X does not equal Y, go back to question.

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
Jengo
Newbie
Posts: 7
Joined: Sun Aug 18, 2013 2:58 am
Contact:

If X does not equal Y, go back to question.

#1 Post by Jengo » Mon Oct 21, 2013 6:40 pm

((That's the best way I could describe it in a title.))
I'm trying to figure out how to write a code that will only progress the story if you type in the right answer (for example, the combination for a safe to open it).
I figure this is going to end up being really simple.

User avatar
jesusalva
Regular
Posts: 85
Joined: Mon Jul 22, 2013 5:05 pm
IRC Nick: jesusalva
Github: pazkero
Location: Brazil
Contact:

Re: If X does not equal Y, go back to question.

#2 Post by jesusalva » Mon Oct 21, 2013 7:25 pm

Code: Select all

    $ yourcode = "Null"  #Start the name variable.
    e "Fast, put the password! otherwise, us are doomed!"
    $ yourcode = renpy.input("But... what is the password?") or "Null"  # Call renpy.input to ask you.
    if yourcode != "0xc4efe35":   # Here, 0xc4efe35 is the password.
      e "Wrong!"  # If you failed.
      show nuke
      "Game Over."
      return
    else:     # if the code is right
      e "Right! us are safe!"
      show joy
      "Good ending."
      return
    return
There is a simple code.
the "!=" means different, while "==" means equal, in the "if" statement.

In your case, us should place in a label:

Code: Select all

    $ yourcode = "Null"  #Start the name variable.
    e "Fast, put the password! otherwise, us are doomed!"

label password_hour:
    $ yourcode = renpy.input("But... what is the password?") or "Null"  # Call renpy.input to ask you.
    if yourcode != "0xc4efe35":   # Here, 0xc4efe35 is the password.
      e "Wrong!"  # If you failed.
      e "Try again!"
      jump password_hour    # jump back.
      return    # just if it fail.
    else:     # if the code is right
      e "Right! us are safe!"
      pass    # as it says... continue the label. In this case, it jump to "continuation" label, because it is under the code.
      
label continuation:
    # History continues...
☆ ♔ Jesusaves/Jesusalva ♚ ★

Jengo
Newbie
Posts: 7
Joined: Sun Aug 18, 2013 2:58 am
Contact:

Re: If X does not equal Y, go back to question.

#3 Post by Jengo » Mon Oct 21, 2013 8:53 pm

Thank you kindly, Jesusalva!

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: If X does not equal Y, go back to question.

#4 Post by Elmiwisa » Tue Oct 22, 2013 4:51 am

Uh, I think the OP is asking for go back to question, not quit the game.
Just use while statement.
For example:

Code: Select all

$safe_password="password"
while safe_password!=renpy.input("Looks like it needs a password...",default=""):
    "Nope, that one doesn't do it..."
That say, it's more polite to the player if you let them a way out rather than force a perpetual loop on those who didn't remember the password.

User avatar
jesusalva
Regular
Posts: 85
Joined: Mon Jul 22, 2013 5:05 pm
IRC Nick: jesusalva
Github: pazkero
Location: Brazil
Contact:

Re: If X does not equal Y, go back to question.

#5 Post by jesusalva » Tue Oct 22, 2013 12:15 pm

Uh, I think the OP is asking for go back to question, not quit the game.
Sorry, Elmiwisa, but I've included one that don't quit the game. It is the last example.

Yeah, using a while statement is easier than using my example. It take less lines. How could I forgot about while loop? :o

I'm using so much the “if” statement in my project (because WHILE is not working and FOR is too hard to I use it (In my case, yeah, it is. Maybe because I'm just doing the last thing that you would do with renpy?)) That the first think that come in my mind is the if statement. (and sorry for not putting it into past.)Just some deadly explanation about why I used if statement instead while.
☆ ♔ Jesusaves/Jesusalva ♚ ★

Elmiwisa
Veteran
Posts: 476
Joined: Sun Jul 21, 2013 8:08 am
Contact:

Re: If X does not equal Y, go back to question.

#6 Post by Elmiwisa » Wed Oct 23, 2013 1:45 am

Oh right I am sorry, I did not read it carefully. But then, why would you put return in there if it will always jump before it get to that point? Also, why do you have to set yourcode to "Null"? I guess it's harmless, but it doesn't do anything either.

User avatar
jesusalva
Regular
Posts: 85
Joined: Mon Jul 22, 2013 5:05 pm
IRC Nick: jesusalva
Github: pazkero
Location: Brazil
Contact:

Re: If X does not equal Y, go back to question.

#7 Post by jesusalva » Tue Oct 29, 2013 2:27 pm

Sorry for not replying earlier, but, as you've said, them aren't nedeed.
I've put the 'return' command just-in-the-case that something goes wrong.
And, yes, setting 'yourcode' to 'Null' is not needed either. I've just placed it just-in-case that ren'py raise an error. I don't trust too much in ren'py, so, I always put 'safe-guards' in my script, to ensure that no error can happen while running the script code.

This is a improved revision without the 'safe-guards' (I doubt that someone would use that, but...)

Code: Select all

    e "Fast, put the password! otherwise, us are doomed!"

label password_hour:
    $ yourcode = renpy.input("But... what is the password?") or "Null"
    if yourcode != "0xc4efe35":
      e "Wrong!"
      e "Try again!"
      jump password_hour
    else:
      e "Right! us are safe!"
      pass
     
label continuation:
    # History continues...
☆ ♔ Jesusaves/Jesusalva ♚ ★

Post Reply

Who is online

Users browsing this forum: Google [Bot]