Unique password in the game

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Unique password in the game

#1 Post by Andredron »

Suppose a character in your story has to choose from several options, and only one of them is correct. The simplest option: they ask him for the password. You can, of course, set the password hard, but then after the first pass it will be known to everyone. I want the password to be new every time! Well, or almost new. Nothing is easier!

Simple password

Code: Select all

init:
    $ door_pass = "12345"
label start:
    "In order to open the door you need to know the password."
    $ user_pass = renpy.input(u"Enter the password (5 characters)", "00000", length = 5)
    if user_pass == door_pass:
        jump open_door
    else:
        "Mistake"
        jump start
1. First and foremost, you need to decide what passwords can be. One of the most obvious ways is to simply list the options, one of which will be correct. For example:

Code: Select all

$ available_passwords = [Oak, Aspen, Poplar, Birch, Spruce, Pine, Cedar, Hornbeam]
But it is possible and, for example, to generate ten random numbers from 1000 to 9999:

Code: Select all

$ available_passwords = [str(renpy.random.randint(1000, 9999)) for i in range(10)]
renpy.random.randint (1000, 9999) - in fact, a random four-digit number

str (blabla) - a function to convert a number to a string. The fact is that the value of a menu item can only be a string.

The number 1000 is an inappropriate option, but the string "1000" is a suitable

range (10) - a list of numbers from 0 to 9

for i in something is a cycle in which all the values from this “something” are written alternately in variable i. In our case, the numbers 0, 1, 2, ..., 8, 9 will be alternately written into the variable i.

2. Now we randomly select one of our passwords, which will be correct in this game launch.

Code: Select all

$ correct_password = renpy.random.choice(available_passwords)
3. Let's do something, let the user know the password. For this you need somewhere in the text to bring it. For example:

Code: Select all


label start:
    "In the cracked safe there was only a small piece of paper. "
    "It was written that the password for today is [correct_password]. "
    "Well, that will be very useful for me!"
4. Now we will show the user the menu and remember the result of his choice. There will again have to use the magic of Python, but do not worry

Code: Select all

$ answer = renpy.display_menu([(x,x) for x in passwords])
4. Voila! Now in the answer variable you keep the value that the player chose. Then you can have fun with it as you like:

Code: Select all

    if answer == correct_password:
        "The guard smiled and opened the door for me."
Autor - Ivan Ivanov(https://vk.com/id142868)

User avatar
nerupuff
Veteran
Posts: 211
Joined: Sat Dec 02, 2017 2:24 am
Contact:

Re: Unique password in the game

#2 Post by nerupuff »

Andredron wrote: Tue Nov 13, 2018 1:30 am Suppose a character in your story has to choose from several options, and only one of them is correct. The simplest option: they ask him for the password. You can, of course, set the password hard, but then after the first pass it will be known to everyone. I want the password to be new every time! Well, or almost new. Nothing is easier!
Hi! Thank you for sharing this cookbook code! I was able to utilize this code for a project I am making. I used the random number choice and it does generate random numbers each launch of the game (meaning to close and re-open, but not when player starts a new game though, so I'll figure that out on my own)

My only problem is when I decide to use a non-number code, I copied your example (Oak, Aspen, etc) to try it out, but it comes back with an error. I added the view of the base code.

Code: Select all


define e = Character("Eileen")

init:
    $ available_passwords = [Oak, Aspen, Poplar, Birch, Spruce, Pine, Cedar, Hornbeam]
    $ correct_password = renpy.random.choice(available_passwords)

label start:
    "In the cracked safe there was only a small piece of paper. "
    "It was written that the password for today is [correct_password]. "
    "Well, that will be very useful for me!"
    "In order to open the door you need to know the password."
    $ user_pass = renpy.input(u"Enter the password (5 characters)", "Input here", length = 10)
    if user_pass == correct_password:
        jump open_door
    else:
        "Mistake"
        jump start

label open_door:
    "Hello and welcome."
    
    return
With this exception notice:

Code: Select all

```
I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 10, in script
    $ available_passwords = [Oak, Aspen, Poplar, Birch, Spruce, Pine, Cedar, Hornbeam]
  File "game/script.rpy", line 10, in <module>
    $ available_passwords = [Oak, Aspen, Poplar, Birch, Spruce, Pine, Cedar, Hornbeam]
NameError: name 'Oak' is not defined

Windows-8-6.2.9200
Ren'Py 7.2.0.424
Cookbook Trials 1.0
Sat Mar 09 13:47:51 2019
```
Help with how to define? I don't have a problem using random number codes, but I plan to use this password system using my own defined passwords for random choice picking for an unlockable menu screen.
ImageImage
Hire me for proofreading, editing, and (maybe) writing! ♡ Check here!

User avatar
ameliori
Veteran
Posts: 201
Joined: Sun Apr 06, 2014 9:20 am
Completed: Who is Mike?, Cupid
Projects: The Black Beast [Fairytale]
Organization: FERVENT
Tumblr: ameliori
itch: fervent
Contact:

Re: Unique password in the game

#3 Post by ameliori »

I got it to work. Just needed a few edits!

Step 1:

I put this in a separate rpy file. Put quotes in the passwords so renpy will read them as separate variables.

Code: Select all

init:
    $ available_passwords = ["Oak", "Aspen", "Poplar", "Birch", "Pine"]
Step 2:

Put this before label start

Code: Select all

init:
    $ correct_password = renpy.random.choice(available_passwords)
Step 3:

Code: Select all

label start:

    scene bg black

    "In the cracked safe there was only a small piece of paper. "
    "It was written that the password for today is [correct_password]. "
    "Well, that will be very useful for me!"
    $ answer = renpy.display_menu([(x,x) for x in available_passwords])
    
    if answer == correct_password:
        "The guard smiled and opened the door for me."

    return
Such a useful code. Thanks so much for this!
ameliori
TwitterPatreon

User avatar
nerupuff
Veteran
Posts: 211
Joined: Sat Dec 02, 2017 2:24 am
Contact:

Re: Unique password in the game

#4 Post by nerupuff »

ameliori, thanks for sharing your edits! I tried them out earlier today and they work like a charm. Really helpful! ^^
ImageImage
Hire me for proofreading, editing, and (maybe) writing! ♡ Check here!

Post Reply

Who is online

Users browsing this forum: No registered users