Alternate Menu (using Python/Class code)

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Alternate Menu (using Python/Class code)

#1 Post by jw2pfd » Fri Feb 28, 2014 10:08 pm

The code currently only displays as an adv style menu. I created this code with two ideas in mind: one was seeing people ask how to have a menu with grayed out options instead of hidden, the other to have more a little bit more control over the menu without straying too far from the default choice screen. Download both files attached and you can copy them into a new project and launch it. It displays like the default menu. The second menu in the attached example script shows how an option can be grayed out.

Here is how to define a menu using my code and then I will show you the rough Ren'Py equivalent:

Code: Select all

    $ choose = Menu("First Menu")
    $ choose.option("1", "lbl")
    $ choose.option("2", "lbl2")
    $ choose.option("3", "lbl3", "a==5")
    $ choose.display()
Before I dissect my code, let me give you the Ren'Py equivalent:

Code: Select all

    menu:
        "First Menu"
        "1":
            jump lbl
        "2":
            jump lbl2
        "3" if a == 5:
            jump lbl3
In the first line of my code, I use the variable 'choose'. You can name your variable whatever you want. In this example, the menu's prompt text is "First Menu" with no speaker. You can create the menu without prompt text by declaring it like this:

Code: Select all

    $ choose = Menu()
And you can also declare it with a character so that it will be displayed with the name of the character. Example:

Code: Select all

    $ choose = Menu("Pick an Option!", e)
In that example, e is the character Eileen and the prompt will be spoken by her. Calling choose.option adds options to the menu and will be displayed in the order they are added. When you call the function, you will always call it with the text to display on the button and the label it will jump to both as strings. It can also be called like the third option in the example:

Code: Select all

    $ choose.option("3", "lbl3", "a==5")
The optional third argument "a==5" is a string that is evaluated to check whether or not it should display the button as grayed out. You can use any conditional statement as a string for that argument for whatever purposes you need. The line choose.display() is self-explanatory as all it does is display the menu. The screen code in menuclass.rpy is only a template and based directly off of the default choice menu. You can alter the screen language of screen display in menuclass.rpy to suit your purposes just like you can the screen choice in the screens.rpy file. When it comes time to show another menu, you can re-use the variable 'choose' if you have no other purpose for it. You can also declare a new menu with a different variable name if you have a purpose for doing so.

There are drawbacks to using this code as it forces you to have a label to jump to for every option which isn't always necessary in the default Ren'Py menu syntax. It can also unnecessarily complicate things if you don't have a specific purpose for using this code. I figure that a few people might like it because of its ability to handle grayed out options. It's also worth noting that you can use this code and still use regular Ren'Py menus.

EDIT: Updated rpy files to include the addition of storing tooltip information as well as a default way of displaying it. Check this post for a different method that relies on default menu syntax and works with ADV and NVL mode.
Attachments
menuclass.rpy
class code
(3.01 KiB) Downloaded 278 times
script.rpy
example script
(1003 Bytes) Downloaded 230 times
Last edited by jw2pfd on Sun Mar 02, 2014 9:16 pm, edited 5 times in total.

User avatar
15nick
Regular
Posts: 33
Joined: Thu Oct 17, 2013 2:46 am
Contact:

Re: Alternate Menu (using Python/Class code)

#2 Post by 15nick » Sat Mar 01, 2014 9:22 pm

Wow, this is really nice. I tried this out for both adv style and nvl mode though, and I had a question. For nvl it cleared the screen for the menu and I was wondering what I'd have to change in the display screen to change that.
Also I was wondering if it was feasible to have the adv style (and nvl mode I guess) allow the user to hover over an insensitive/inactive option and have a tooltip or something like that pop up with what you're missing. I know it would probably need hovered tt.Action("") in the button part of display as well as a string variable to hold the popup message but I wouldn't know how to add that to the code you've provided. Just wondering if you had any advice or help for doing that.

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: Alternate Menu (using Python/Class code)

#3 Post by jw2pfd » Sun Mar 02, 2014 5:33 pm

@15nick, read my next post after this one for some code that may help out. It is different that the code in my original post; it uses default menu syntax and works with ADV and NVL.
15nick wrote:Wow, this is really nice. I tried this out for both adv style and nvl mode though, and I had a question. For nvl it cleared the screen for the menu and I was wondering what I'd have to change in the display screen to change that.
Whoops, I completely forgot about nvl mode as I haven't messed with nvl mode much. I am going to toy with some code and see if I can make it also work with nvl the way that default nvl menus work.
15nick wrote:Also I was wondering if it was feasible to have the adv style (and nvl mode I guess) allow the user to hover over an insensitive/inactive option and have a tooltip or something like that pop up with what you're missing. I know it would probably need hovered tt.Action("") in the button part of display as well as a string variable to hold the popup message but I wouldn't know how to add that to the code you've provided. Just wondering if you had any advice or help for doing that.
I haven't messed with the tooltip class before. At least for adv mode, your suggestion is possible. I was relying on a default button without an action appearing grayed out; however, a button without an action doesn't seem to have its hovered action trigger. This means it'd be better to create a button that has an action that doesn't cause the interaction to end and set its properties so it appears grayed out whether or not it is hovered. This way it can trigger the tooltip action.

Anyway, I went ahead and messed with the code and added a way for the class to store some tooltip information. By default, I have it display the tooltip as extra information directly on the button only when it's grayed out. When creating an option, the tooltip is the fourth argument which is entirely optional. For example:

Code: Select all

    $ choose.option("Lift rock", "lift", "str>=10", "(need 10 strength)")
I'll edit the original post's attachment with the updated code.
Last edited by jw2pfd on Sun Mar 02, 2014 9:18 pm, edited 3 times in total.

jw2pfd
Regular
Posts: 87
Joined: Tue Sep 18, 2012 9:55 pm
Location: DFW, TX, USA
Contact:

Re: Alternate Menu (using Python/Class code)

#4 Post by jw2pfd » Sun Mar 02, 2014 9:06 pm

I have some code that will suit more people's needs that is simple and can work for both ADV and NVL mode. It also uses the default menu syntax. Here is example code from my script.rpy file.

Code: Select all

define e = Character('Eileen', color="#c8ffc8")

label start:
    
    $ conditions = Conditions()
    
    $ b = 0
    
    $ conditions.addcondition("No", "b==1", "(tooltip)")
        
    menu:
        e "This is test menu."
        
        "Yes":
            e "Chose 'Yes'"
        "No":
            e "Chose 'No'"

    return
It is required that you declare a variable named conditions after the start label as it is done in the example code:

Code: Select all

    $ conditions = Conditions()
A function call to add conditions looks like this:

Code: Select all

    $ conditions.addcondition("No", "b==1", "(tooltip)")
The first argument is the same exact value as the caption of the menu item on which you want a condition placed. In the example, I place a condition on the second menu option which has the caption 'No'. The second argument is a condition as a string that will be checked. The third argument is an optional string that contains tooltip information. It is displayed on the grayed out button by default. You keep using the conditions variable, but it is a good practice to clear its data before each menu:

Code: Select all

    $ conditions.clear()
    #add conditions on the following lines
    #call a new menu
You can declare other variables with the conditions class, but the data must be in the conditions variable as this is the variable that is checked when the menu is displayed. There are two required files, cscreens.rpy and conditions.rpy. The code in cscreens contains modified screen code for the choice and nvl screens. This means that you must delete the screen code for these screens in your screens.rpy file. Alternatively, you can copy and overwrite the choice and nvl code in the screens.rpy file with the code in cscreens.rpy.
Attachments
cscreens.rpy
required screen code (replaces code for the screens choice and nvl in screens.rpy)
(2.39 KiB) Downloaded 180 times
conditions.rpy
required class code
(970 Bytes) Downloaded 173 times
script.rpy
example code
(336 Bytes) Downloaded 159 times

User avatar
Arowana
Miko-Class Veteran
Posts: 531
Joined: Thu May 31, 2012 11:17 pm
Completed: a2 ~a due~
Projects: AXIOM.01, The Pirate Mermaid
Organization: Variable X, Navigame
Tumblr: navigame-media
itch: navigame
Contact:

Re: Alternate Menu (using Python/Class code)

#5 Post by Arowana » Mon Mar 03, 2014 12:09 am

This looks very handy! I was just thinking I would have to code something like this, so I'm super happy to see that you've already done it, and I'm sure many other people will be using it too. Thanks a bunch! :D
Complete: a2 ~a due~ (music, language, love)
In progress: The Pirate Mermaid (fairytale otome)
On hold: AXIOM.01 (girl detective game)

Image

Post Reply

Who is online

Users browsing this forum: No registered users