loop labels and menus [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
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

loop labels and menus [solved]

#1 Post by pucedragonlord » Wed Oct 31, 2012 7:52 pm

So I've got more than a couple labels full of menus that loop back on themselves. Remembering each choice and changing the dialog to avoid players going through different menu options this way is nice and all, but I wonder if there's a way for RenPy to remember which menu choice was picked last and automatically route to that one without defining labels and variables all over the place. Just something I'm curious about. It'd be a nice feature.
Last edited by pucedragonlord on Thu Nov 08, 2012 4:39 pm, edited 1 time in total.
The more you know

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: loop labels and menus

#2 Post by PyTom » Wed Oct 31, 2012 9:08 pm

Do you want to avoid showing them, or automatically select them? I'm not clear on what you want.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: loop labels and menus

#3 Post by pucedragonlord » Thu Nov 01, 2012 1:23 pm

automatically select them. Sorry about that, guess it wasn't that clear.
The more you know

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: loop labels and menus

#4 Post by Kinsman » Thu Nov 01, 2012 5:42 pm

You can use Python code to show menus using renpy.display_menu(), and then wrap some code around that to get the effect you want.

Is this what you're looking for?

Code: Select all

label start:

    # These variables are used by the menu system. Initialize them at game start.
    $menuspicked = {}
    $choice = ""

    call test
    return

# Here's a wrapper around renpy.display_menu. It will display a menu if the ID for that menu seems new, or else just return what the player chose if that menu has been shown before.
label smartmenu(*items):
    python:
        choice = ""
        if items[0] in menuspicked:
            choice = menuspicked[items[0]]
        elif len(items) == 1:
            pass
        else:
            # construct a menu
            menuitems = []
            for i in range(1,len(items)):
                menuitems.append((items[i],items[i]))
            choice = renpy.display_menu(menuitems)
            menuspicked[items[0]] = choice
    return

# Here's an example of how to use it.
label inforequest:
    call smartmenu("INFO","Neo-Africa","World War III","Zombie Outbreak")
    if choice == "Neo-Africa":
        "Neo Africa was the confederation of most African nations in 2024."
    if choice == "World War III":
        "World War III was a conventional war between Russia and China. NATO chose to stay out of it."
    if choice == "Zombie Outbreak":
        "America had its own problems during World War III, what with the zombies and all."
    return

label test:
    "OK, what part of the future would you like to know about? One choice only, please."
    call inforequest
    "And let me repeat that, to make sure you remember:"
    call inforequest
    return

Flash To Ren'Py Exporter
See the Cookbook thread

User avatar
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: loop labels and menus

#5 Post by pucedragonlord » Mon Nov 05, 2012 6:16 pm

that works great. Can I still have an information line showing with this menu like normal ones?

for example:

Code: Select all

menu:
    "what do?"
    "item1":
        jump item1
    "item2":
        pass
or is that not possible? If not that's fine, just curious if it's possible. When I ran it the first "INFO" line didn't show up. I assumed that's what it would do.
The more you know

Kinsman
Regular
Posts: 130
Joined: Sun Jul 26, 2009 7:07 pm
Location: Fredericton, NB, Canada
Contact:

Re: loop labels and menus

#6 Post by Kinsman » Tue Nov 06, 2012 4:15 pm

pucedragonlord wrote:that works great. Can I still have an information line showing with this menu like normal ones?

for example:

Code: Select all

menu:
    "what do?"
    "item1":
        jump item1
    "item2":
        pass
or is that not possible? If not that's fine, just curious if it's possible. When I ran it the first "INFO" line didn't show up. I assumed that's what it would do.
The "INFO" part is meant as an ID-code, so that the script knows how to keep track of which menu has been shown or not.

Here, let me improve the code a little.

Code: Select all

label smartmenu(*items):
    python:
        choice = ""
        menukey = ""
        for i in range(0,len(items)):
            menukey = menukey + items[i]
        if menukey in menuspicked:
            choice = menuspicked[menukey]
        elif len(items) == 1:
            pass
        else:
            # construct a menu
            menuitems = []
            for i in range(1,len(items)):
                menuitems.append((items[i],items[i]))
            renpy.say(None,items[0],interact=False)
            choice = renpy.display_menu(menuitems)
            menuspicked[menukey] = choice
    return
Now, the first item will be what appears as the question that goes with the menu. The ID code is now auto-generated based on what's in the menu, so you won't need to worry about it unless your menus are very similar to each other.
Flash To Ren'Py Exporter
See the Cookbook thread

Post Reply

Who is online

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