Small Set of Random Options in Choice Menu [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
A Hazard
Regular
Posts: 25
Joined: Sun Jun 29, 2014 4:02 pm
Completed: Gilded - The Lily & the Cage
Projects: Fragment - A Broken Fairy Tale
Tumblr: fragmentfairytale
Deviantart: fragmentfairytale
Contact:

Small Set of Random Options in Choice Menu [SOLVED]

#1 Post by A Hazard » Sat Feb 21, 2015 2:26 pm

I've been dithering around with the various renpy.random.[ETC] for awhile now, but I can't figure out how to get this to work how I want. (And searching variations of "random" "option" "menu" "choice" "randomization" on the board is just showing me what I'm not trying to do)

Basically I want to choose a small random subset of options for a choice menu from a larger set without duplicating any options. A simple sample would be trying to get a choice menu "You're being attacked, what should you do?" to show a random assortment of four of the following seven options ("Jump" jump jump1, "Dodge" jump dodge, "Charge" jump charge, "Duck" jump duck, "Wait" jump wait, "Attack Magic" jump attack_magic, "Defense Magic" jump defense_magic)

renpy.random.sample can choose the subset (ie renpy.random.sample(battle1, 4) will pick four out of the seven options without duplicating them), but I can't figure out how to get random.sample's output to appear as separate options in the menu. The closest I've gotten so far is by brute forcing...

Code: Select all

 
    $ battle1 = renpy.random.choice(['a', 'b', 'etc']) 
    if  battle1 == 'a':
        menu:
            pro "You're being attacked, what should you do?"
            "Jump":
                jump jump1
            "Dodge":
                jump dodge
            "Charge":
                jump charge
            "Duck":
                jump duck
           
    elif  battle1 == 'b':
        menu:
            pro "You're being attacked, what should you do?"
            "Jump":
                jump jump1
            "Dodge":
                jump dodge
            "Charge":
                jump charge
            "Wait":
                jump wait

[[Repeat for every variation of 4 out of 7 options]]
That would be a hideous-looking amount of repetitive coding especially for sets that have more than seven total options. Any help here would be appreciated.
Last edited by A Hazard on Sat Feb 21, 2015 6:09 pm, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1458
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Choice Menu - Small Set of Random Options from Larger Se

#2 Post by xavimat » Sat Feb 21, 2015 4:24 pm

I'd do this:
In the beginning, define your list of menu options as a list of tuples with two elements. The first element is the name that the player will see, the second element is the name of the label we'll jump to.

Code: Select all

label start:
    $ battle_options = [
        ("Jump", "jump1"),
        ("Dodge", "dodge"),
        ("Charge", "charge"),
        ("Duck", "duck"),
        ("Wait", "wait"),
        ("Attack Magic", "attack_magic"),
        ("Defense Magic", "defense_magic")
        ]
In a label "battle", do this three actions:
1. with random.sample take 4 random elements from the list
2. present to the player a menu with this 4 options
3. jump to the chosen label
I've made this firstly with 3 lines (more readable):

Code: Select all

label battle:
    pro "You're being attacked, what should you do?"
    $ four_options = renpy.random.sample(battle_options, 4)
    $ chosen_action = menu(four_options)
    $ renpy.jump(chosen_action)
But then, I've realized that it's possible to do the same thing with two lines, nesting the functions (less readable):

Code: Select all

label battle:
    pro "You're being attacked, what should you do?"
    $ renpy.random.shuffle(battle_options)
    $ renpy.jump(menu(battle_options[:4]))
[/s]
EDIT: And, finally, the same thing using a simple line, nesting the functions (less readable):

Code: Select all

label battle:
    pro "You're being attacked, what should you do?"
    $ renpy.jump(menu(renpy.random.sample(battle_options, 4)))
Obviously, you need the seven labels with the actions:

Code: Select all

label jump1:
    "jump"
    jump battle
label dodge:
    "dodge"
    jump battle
label charge:
    "charge"
    jump battle
label duck:
    "duck"
    jump battle
label wait:
    "wait"
    jump battle
label attack_magic:
    "Attack Magic"
    jump battle
label defense_magic:
    "Defense Magic"
    jump battle
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
A Hazard
Regular
Posts: 25
Joined: Sun Jun 29, 2014 4:02 pm
Completed: Gilded - The Lily & the Cage
Projects: Fragment - A Broken Fairy Tale
Tumblr: fragmentfairytale
Deviantart: fragmentfairytale
Contact:

Re: Choice Menu - Small Set of Random Options from Larger Se

#3 Post by A Hazard » Sat Feb 21, 2015 6:06 pm

I just plugged the 1 line version into the actual code of my game (a bit more complicated than just dodge/duck/etc) and it's working exactly as I hoped. This is such a time saver over renpy.random.choice, thanks so much!

Post Reply

Who is online

Users browsing this forum: Bing [Bot]