renpy.random.choice and If statement questions

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
Bluberi_Lemonade
Newbie
Posts: 4
Joined: Tue Feb 18, 2014 8:14 pm
Contact:

renpy.random.choice and If statement questions

#1 Post by Bluberi_Lemonade »

Hi! I searched around a little bit and couldn't find any answers to these particular questions, so I thought I'd just ask here.

I just learned how to use renpy.random.choice to generate a random element from a list of them, for example:

Code: Select all

$ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])
"Shop Clerk" "Would you like a [randfruit]?"
I understand this, but I wanted to know if there was any way to use things like if statements with the random elements, or anything that could cause something to occur only if a specific element out of the list was chosen.
For example, whether the Shop Clerk says 'apple', 'orange' or 'plum' is totally random, but if she were to say 'apple', what she says after saying 'apple' is different than what she says after saying 'orange' or 'plum'. How would I make this happen? An example in code would be much appreciated, and if you have trouble understanding what I'm saying, sorry, I will try to elaborate better. Thank you so much!

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: renpy.random.choice and If statement questions

#2 Post by Asceai »

Sure.

Code: Select all

$ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])
"Shop Clerk" "Would you like a [randfruit]?"
if randfruit == 'apple':
  "Shop Clerk" "Apples are in season this time of year!"
elif randfruit == 'orange':
  "Shop Clerk" "They make a great garnish for cocktails!"
elif randfruit == 'plum':
  "Shop Clerk" "They're not really in season, but they're always delicious!"

User avatar
Donmai
Eileen-Class Veteran
Posts: 1960
Joined: Sun Jun 10, 2012 1:45 am
Completed: Toire No Hanako, Li'l Red [NaNoRenO 2013], The One in LOVE [NaNoRenO 2014], Running Blade [NaNoRenO 2016], The Other Question, To The Girl With Sunflowers
Projects: Slumberland
Location: Brazil
Contact:

Re: renpy.random.choice and If statement questions

#3 Post by Donmai »

Asceai beat me. That's exactly what I was going to suggest. Only, for the sake of good English, I would rewrite the variables this way: 'an apple', 'an orange' and 'a plum'. And the code slightly altered:

Code: Select all

$ randfruit = renpy.random.choice(['an apple', 'an orange', 'a plum'])
"Shop Clerk" "Would you like [randfruit]?"
if randfruit == 'an apple':
  "Shop Clerk" "Apples are in season this time of year!"
elif randfruit == 'an orange':
  "Shop Clerk" "They make a great garnish for cocktails!"
elif randfruit == 'a plum':
  "Shop Clerk" "They're not really in season, but they're always delicious!"
Image
No, sorry! You must be mistaking me for someone else.
TOIRE NO HANAKO (A Story About Fear)

User avatar
Bluberi_Lemonade
Newbie
Posts: 4
Joined: Tue Feb 18, 2014 8:14 pm
Contact:

Re: renpy.random.choice and If statement questions

#4 Post by Bluberi_Lemonade »

Asceai wrote:Sure.

Code: Select all

$ randfruit = renpy.random.choice(['apple', 'orange', 'plum'])
"Shop Clerk" "Would you like a [randfruit]?"
if randfruit == 'apple':
  "Shop Clerk" "Apples are in season this time of year!"
elif randfruit == 'orange':
  "Shop Clerk" "They make a great garnish for cocktails!"
elif randfruit == 'plum':
  "Shop Clerk" "They're not really in season, but they're always delicious!"
Ah...I had that exact set-up, but I didn't put in the double equal signs, aha...

Once I put that in, the code worked like magic <3

Thank you so much! This helped a lot!

@Donmai oh yeah, didn't notice that there. That was a random scenario I made up really fast for this example but now I see that that would make more sense. Thanks!

Post Reply

Who is online

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