Renpy random, but for multiple items/numbers? [SOLVED]

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
User avatar
AsHLeX
Miko-Class Veteran
Posts: 556
Joined: Wed Dec 25, 2013 1:09 pm
Completed: Starlight Dreamers, Mysterious Melody, Town of Memories, Marked, To Fly, The Change, Him From The Past, A Forgotten Memory
Projects: Cafe Mysteria
Location: Malaysia
Contact:

Renpy random, but for multiple items/numbers? [SOLVED]

#1 Post by AsHLeX »

Hello! Thanks for taking your time for reading this.

I am familiar with the renpy random.randit and random.choice mode, but is there an option where I can get renpy to select multiple answers out of them?

Code: Select all

# return a random float between 0 and 1
$ randfloat = renpy.random.random()

# return a random integer between 1 and 20
$ d20roll = renpy.random.randint(1, 20)

# return a random element from a list
$ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])
For example, $ randfruit = renpy.random.choice(['apple', 'orange', 'plum']), but I want renpy to choose a list of 2:
and then followed by something like
if randfruit contains apple: show image1 on left hand side
if randfruit contains orange: show image3 on right hand side
if randfruit contains plum: show image2 at middle

I suspect I'll need to use a list function or something but I'm unsure of where to start. If someone can point me in the right direction or give me tips on how to achieve what I need, that would be very much appreciated. Thanks
Last edited by AsHLeX on Sat Feb 19, 2022 3:27 am, edited 1 time in total.
Image
New demo out 24/12/23!!

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: Renpy random, but for multiple items/numbers?

#2 Post by IrinaLazareva »

Code: Select all

init python:
    from random import sample 

label start:
    "Start"

    $ randfruit = sample(['apple', 'orange', 'plum'], 2)

    if 'apple' in randfruit:
        show image1 at left

    if 'orange' in randfruit:
        show image2 at center

    if 'plum' in randfruit:
        show image3 at right
Or.. (if you need a tie-in to a Ren'py's preservation and roll-back system)

Code: Select all

## in this case the declaration in init stage is not required

label start:
    e "Start"
    $ randfruit = renpy.random.sample(['apple', 'orange', 'plum'], 2)
    
    if 'apple' in randfruit:
        show image1 at left
        
    if 'orange' in randfruit:
        show image2 at center
        
    if 'plum' in randfruit:
        show image3 at right
https://docs.python.org/2.7/library/ran ... dom.sample

User avatar
AsHLeX
Miko-Class Veteran
Posts: 556
Joined: Wed Dec 25, 2013 1:09 pm
Completed: Starlight Dreamers, Mysterious Melody, Town of Memories, Marked, To Fly, The Change, Him From The Past, A Forgotten Memory
Projects: Cafe Mysteria
Location: Malaysia
Contact:

Re: Renpy random, but for multiple items/numbers? [SOLVED]

#3 Post by AsHLeX »

Thank you!!!
Image
New demo out 24/12/23!!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]