[SOLVED] Random choices with conditional events

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
Kinmoku
Miko-Class Veteran
Posts: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

[SOLVED] Random choices with conditional events

#1 Post by Kinmoku » Sun Mar 20, 2016 9:50 am

Hi all,

I have some events which only happen if you find certain items in the game. E.g. You get 3 chances to find items. Some are significant, others not. If an item is significant, the player character will talk about it with another character later on. If you only pick 1 of significance, no problem. However, if by chance the player picks 3 items of significance, then there will either be 3 chunks of dialogue (which I don't want) or it will pick the one at the top.

Here's my code:

Code: Select all

    if "guitarstand" in found:
        y "That's a nice guitar you've got there!"

        # etc etc
        
    elif "booksshelf" in found:
        y "I see you've been reading up on English Literature?"
        
        # etc etc
        
    elif "vinylshelf" in found:
        y "Nice vinyl collection you have there."

        # etc etc
        
    elif "winebottlefloor" in found:
        y "You shouldn't have spoiled me with that fancy bottle of wine, you know."

        # etc etc
        
    else:
        y "..."
    
Ideally, I want there to be a random choice made depending on how many significant items you found. So if you only found 1 significant item, the chance of talking about it is 100%, unlike if you find 3 (where it would be 33%).

I hope I'm making sense! I know how to do simple random choices, but mixing it with which items may or may not have been found is confusing me!
Last edited by Kinmoku on Tue Mar 22, 2016 4:32 am, edited 1 time in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Random choices with conditional events

#2 Post by xela » Sun Mar 20, 2016 10:11 am

You can just add another condition to the code which will check if there has ever been a choice made and work with that. If you don't want the top choice to be the one in case of mupli items found scenario, use renpy.random.choice to get one option from the "found" list and check against that instead of checking for list membership.
Like what we're doing? Support us at:
Image

Onishion
Veteran
Posts: 295
Joined: Mon Apr 20, 2015 10:36 am
Contact:

Re: Random choices with conditional events

#3 Post by Onishion » Mon Mar 21, 2016 5:27 am

Here's a simple way to do it, "found" is a list, right? so just do this:

Code: Select all

$ renpy.random.shuffle(found)  #this randomizes the order of the "found" list

if "guitarstand" == found[0]: #this checks if the name is the FIRST item in the list, if not it moves on. 
        y "That's a nice guitar you've got there!"

        # etc etc
       
elif "booksshelf" == found[0]:
        y "I see you've been reading up on English Literature?"
       
        # etc etc

else:
     "You don't have any of those items" #or whatever.
That should give you a random result, based on the items within the "found" list.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Random choices with conditional events

#4 Post by xela » Mon Mar 21, 2016 6:40 am

Onishion wrote:...
It's more or less the same thing. In both approaches, you'll have to first sort out "minor" items from the found list and make sure it has at least one item in it or you'll get IndexErrors and a possibility that the code checks against minor item instead of a major one.
Like what we're doing? Support us at:
Image

User avatar
Kinmoku
Miko-Class Veteran
Posts: 560
Joined: Mon Aug 11, 2014 9:39 am
Completed: One Night Stand
Projects: Love IRL, Memories
Tumblr: gamesbykinmoku
itch: kinmoku
Location: Germany
Contact:

Re: Random choices with conditional events

#5 Post by Kinmoku » Tue Mar 22, 2016 4:32 am

Hi both, thanks for your help on this.

I tried the random shuffle with "if "guitarstand" == found[0]:" etc and it seems to work ok. I tested both selecting significant items and lesser ones. You always have to choose 3, before the game progresses, so I think it shouldn't cause any trouble later down the line.

Thanks! :D

Post Reply

Who is online

Users browsing this forum: Alex, Google [Bot], nyeowmi