[Solved] Taking out random items from 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
Doeny
Regular
Posts: 26
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

[Solved] Taking out random items from list?

#1 Post by Doeny » Thu Oct 06, 2022 5:45 am

I was thinking about creating a mini game about pulling out random items from a container. Let's say I have a list:

default container = [*stuff possible to pick up*]

What is the easiest way to write a code randomizing and then picking out single items from that list?
Last edited by Doeny on Thu Oct 06, 2022 6:37 pm, edited 2 times in total.

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

Re: Taking out random items from list?

#2 Post by Ocelot » Thu Oct 06, 2022 5:57 am

Code: Select all

# Do you want to pick up a random item and leave it in container?
$ item = renpy.random.choice(container)

# Do you want to pick random item and remove it?
$ renpy.random.shuffle(container) # Once at the beginning
# . . . 
$ item = container.pop()
< < insert Rick Cook quote here > >

Doeny
Regular
Posts: 26
Joined: Wed Sep 07, 2022 8:28 pm
Contact:

Re: Taking out random items from list?

#3 Post by Doeny » Thu Oct 06, 2022 4:55 pm

Ocelot wrote:
Thu Oct 06, 2022 5:57 am

Code: Select all

# Do you want to pick up a random item and leave it in container?
$ item = renpy.random.choice(container)

# Do you want to pick random item and remove it?
$ renpy.random.shuffle(container) # Once at the beginning
# . . . 
$ item = container.pop()
It works perfectly, thank you very much, Ocelot!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]