[Solved] How to Shuffle a 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
Archer
Newbie
Posts: 3
Joined: Thu Mar 23, 2017 5:00 pm
Contact:

[Solved] How to Shuffle a List

#1 Post by Archer »

Hi, I just started using the Renpy engine and am moving over a project I had started in Twine. I've mostly figured out how to adjust the code, but I have one problem at the moment.

How do I shuffle a list (array)?

Longer explanation (only necessary if the above doesn't have a simple command)

I want to have a list of events, that an event gets pulled randomly from and removed from the list. I also want there to be more than one possibility of a certain event within the list, and I do not want all instances to be removed. I cannot find a renpy command that does that, though I found that the 'pop' command removes a set item in the list.

On the code side here is a simplified example

Code: Select all

label start:
    $ forest = ["Orc", "Orc", "Orc", "Treasure", "Nothing"]
    # this is where I'd want the shuffle
jump forest_encounter
    
label forest_encounter:

        $ encounter = forest.pop()
        if encounter == "Orc":
            "Danger, Orc"
            jump forest_encounter
        elif encounter == "Treasure":
            "Yay, Treasure"
            jump forest_encounter
        elif encounter == "Nothing":
            "The forest is quiet"
            jump forest_encounter
Right now that just cycles through from the back to the end, I'd the list to shuffle itself and then do that. Thanks for any help.
Last edited by Archer on Thu Mar 23, 2017 7:37 pm, edited 1 time in total.

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

Re: How to Shuffle a List

#2 Post by IrinaLazareva »

for "true" shuffle (without removal from the list) you can use

Code: Select all

    $ renpy.random.shuffle(["song1.mp3", "song2.mp3", "song3.mp3"])  
if you need randomly removing of item from the list:

Code: Select all

    $ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])
https://www.renpy.org/doc/html/other.html#renpy-random
See also https://docs.python.org/2/library/random.html

Archer
Newbie
Posts: 3
Joined: Thu Mar 23, 2017 5:00 pm
Contact:

Re: How to Shuffle a List

#3 Post by Archer »

Hi, sorry if I'm missing something obvious, but I don't think that answers the question.

For your first example: how would I set that shuffled list to its own variable. I've tried things like:

Code: Select all

    $ mp3 = renpy.random.shuffle(["song1.mp3", "song2.mp3", "song3.mp3"])"
To get the list of the songs saved to a variable, but can't find the command that does that.

For your second example: wouldn't that just do random one time? What I'd like is to be able to come back to the same list and pick again, but this time with no choice of the same option being drawn.

Thank you for your response.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to Shuffle a List

#4 Post by Alex »


Archer
Newbie
Posts: 3
Joined: Thu Mar 23, 2017 5:00 pm
Contact:

Re: How to Shuffle a List

#5 Post by Archer »

Thank you Alex, that's what I wanted. I was putting brackets where they didn't need to be.

Post Reply

Who is online

Users browsing this forum: bilmem, Ocelot