Page 1 of 1

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

Posted: Mon Jul 26, 2021 12:05 pm
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. :)

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

Posted: Mon Jul 26, 2021 3:30 pm
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

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

Posted: Mon Jul 26, 2021 11:39 pm
by strayerror
Sounds like you're looking for the menu set functionality.

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