Any way to have Renpy give the player a random question from a list, and then remove that question from the list?

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
skinnyfitgenes
Newbie
Posts: 12
Joined: Wed May 12, 2021 11:15 am
Contact:

Any way to have Renpy give the player a random question from a list, and then remove that question from the list?

#1 Post by skinnyfitgenes »

Edit: Really sorry, after a few hours playing with this I have managed to get it working! If any mods see this the thread can be deleted.

I’ll try my best to explain - English is not my first language so apologies if some of this comes out wrong or confusing.

So, in my game I have a character who poses the player with ethical questions at regular intervals. Rather than me choosing which questions are asked and in what order, I’d like to have a list or a pool of questions and have Renpy pick one from that pool at random to ask the player. The player can then make a choice on their answer using Renpy’s usual menu system. And then that question is removed from the pool for the rest of the game, so it doesn’t get asked again.

It is a bit more complicated than just having a normal list for Renpy to pick from at random, because I need to set each question out as a menu and then include a variable change as the consequence of each answer.

Is there any way to do this? Maybe I would need to set up labels before each question and have Renpy jump to a label at random? Also, although I have managed to get a random selection from a list working, I’m not sure how to remove list items that have been picked already.

Thank you so much for reading - any ideas or advice are greatly appreciated. :)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Any way to have Renpy give the player a random question from a list, and then remove that question from the list?

#2 Post by Ocelot »

Two approaches, one using labels, and the other storing all information in variables:

Code: Select all

default questions = ['q1', 'q2', 'q3', 'q4']
default score = 0

default questions_2 = [
    [
        ("What is the answer 1?", None),
        ('A1.', 0),
        ('A2.', 0),
        ('A3.', 1),
        ('A4.', 0),
    ],
    [
        ("What is the answer 2?", None),
        ('A1.', 1),
        ('A2.', 0),
        ('A3.', 0),
        ('A4.', 0),
    ],
    [
        ("What is the answer 3?", None),
        ('A1.', 0),
        ('A2.', 1),
        ('A3.', 0),
        ('A4.', 0),
    ],
    [
        ("What is the answer 4?", None),
        ('A1.', 0),
        ('A2.', 1),
        ('A3.', 0),
        ('A4.', 1),
    ],
]

label q1:
    menu:
        "What is the answer 1?"
        "A1.":
            pass
        "A2.":
            pass
        "A3.":
            $ score += 1
        "A4.":
            pass
    return

label q2:
    menu:
        "What is the answer 2?"
        "A1.":
            $ score += 1
        "A2.":
            pass
        "A3.":
            pass
        "A4.":
            pass
    return

label q3:
    menu:
        "What is the answer 3?"
        "A1.":
            pass
        "A2.":
            $ score += 1
        "A3.":
            pass
        "A4.":
            pass
    return

label q4:
    menu:
        "What is the answer 4?"
        "A1.":
            pass
        "A2.":
            pass
        "A3.":
            pass
        "A4.":
            $ score += 1
    return

label start:
    $ renpy.random.shuffle(questions)
    $ renpy.random.shuffle(questions_2)

    $ score = 0
    while (len(questions) > 0):
        $ question = questions.pop()
        call expression question
    'Your score: [score]'

    $ score = 0
    while (len(questions_2) > 0):
        $ question = questions_2.pop()
        $ score += renpy.display_menu(question)
    'Your score: [score]'
    return
< < insert Rick Cook quote here > >

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: Any way to have Renpy give the player a random question from a list, and then remove that question from the list?

#3 Post by strayerror »

Sounds like you're looking for the menu set functionality.

Appologies, misread the question. Glad you got it sussed. :)

Post Reply

Who is online

Users browsing this forum: Google [Bot]