Buttons and Flyout 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
QotC
Regular
Posts: 54
Joined: Thu Feb 17, 2011 8:53 pm
Contact:

Buttons and Flyout Menus [SOLVED]

#1 Post by QotC »

Okay, I'm trying to go the Princess Maker route, creating a day-planner that lets you choose working, studying, etc., with sub-menus for what job/class/thingy to do that week. Unfortunately, despite scrutinizing the DSE files and demo_ui.rpy from the tutorial, and scouring the wiki and stuff for clues, I just can't get how to make it work. Here's what I got so far:

Code: Select all

init python:
    style.window.left_padding = 100
    
    show_main_plan_menu = True
    selecting_plan = False
    show_job_menu = False
    show_class_menu = False
    
    def show_jobs():
        selecting_plan = True
        show_job_menu = True  
        job_menu()
        
    def planner():
        
        def job_menu_button():
            if selecting_plan:
                ui.textbutton("Work", clicked= None, xminimum=150)
            else:
                ui.textbutton("Work", clicked= show_jobs, xminimum=150)
                
        if show_main_plan_menu:
            ui.frame(xpos=0, ypos=0, xanchor='left', yanchor='top', yfill = True)
            ui.vbox(xpos=0, ypos=0, xanchor='left', yanchor='top')
            job_menu_button()
            ui.textbutton("Class", clicked=None, xminimum=150)
            ui.textbutton("Rest", clicked=None, xminimum=150)
            ui.textbutton("Adventure", clicked=None, xminimum=150)
            ui.close()

    def job_menu():
            if show_job_menu:
                ui.frame(xpos=0.5, ypos=0.5, xanchor='center', yanchor='center')
                ui.vbox()
                ui.textbutton("Done", clicked = None, xminimum = 80)
                ui.close()            
When I click on the Job button, nothing happens.

I don't understand; is it the way the button is implemented? Is it techinically working, but needs to re-draw the screen in order for the menu to show up?

What's really aggravating is that this isn't the first time I've gotten stuck on buttons; the manual just says "call a function with no arguments!", but no matter what I try, either the button does nothing when clicked, or the function is called *without* waiting for the button to be clicked on... Sorry, I'm just frustrated. Can anyone help? :(
Last edited by QotC on Thu Jul 07, 2011 2:23 pm, edited 5 times in total.

QotC
Regular
Posts: 54
Joined: Thu Feb 17, 2011 8:53 pm
Contact:

Re: Buttons and Flyout Menus

#2 Post by QotC »

Okay, after going over ui.returns and ui.interact a few more times, I finally figured out what to do. I had been confusing "ui.returns" as a rephrase of the "return" statement, and since I didn't want to leave the screen, I was trying to avoid using it. Now I've got something functional based on ui.returns, with a hacked up version of the dse's "day_planner_repeat" function to redraw based on the results:

Code: Select all

init python:
    style.window.left_padding = 100
    
    show_main_plan_menu = True
    selecting_plan = False
        
    def job_menu_button():
        if selecting_plan:
            ui.textbutton("Work", clicked= None, xminimum=150)
        else:
            ui.textbutton("Work", clicked= ui.returns(1), xminimum=150)
                
        
    def main_plan_menu():
        
        if show_main_plan_menu:
            ui.frame(xpos=0, ypos=0, xanchor='left', yanchor='top', yfill = True)
            ui.vbox(xpos=0, ypos=0, xanchor='left', yanchor='top')
            job_menu_button()
            ui.textbutton("Class", clicked=None, xminimum=150)
            ui.textbutton("Rest", clicked=None, xminimum=150)
            ui.textbutton("Adventure", clicked=None, xminimum=150)
            ui.close()
            
    def job_menu():
                ui.frame(xpos=0.5, ypos=0.5, xanchor='center', yanchor='center')
                ui.vbox()
                ui.textbutton("Back", clicked = ui.jumps('planner'), xminimum = 80)
                ui.close()            
                
                
label planner:
    python hide:
        main_plan_menu()
        renpy.choice_for_skipping()

    $ result = ui.interact()
        
label planner_repeat:
    python:
        main_plan_menu()
        if result == 1:
            job_menu()
    if ui.interact():
        jump planner_repeat
    else:
        return
    
If there's a better/cleaner way of doing it, though, I'm all ears; my coding style seems to tend towards "If I type enough code, some of it will turn out to work," AKA The Infinite Monkies technique.

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: Buttons and Flyout Menus [Solved, probably]

#3 Post by Showsni »

Well, this isn't particularly elegant or anything, but you could do something like this:

First, defining a screen as our planner.

Code: Select all

    screen planner:
        hbox:
            frame:
                vbox:
                    text "What will she do for the first part of the month?"
                    textbutton (act1 + job1) action SetVariable("decideact", 1)
                    if decideact == 1:
                        hbox:
                            frame:
                                vbox:
                                    textbutton "Work" action SetVariable("act1", "Work")
                                    textbutton "Study" action SetVariable("act1", "Study")
                                    textbutton "Explore" action SetVariable("act1", "Explore")
                                    textbutton "Rest" action SetVariable("act1", "Rest")
                            frame:
                                vbox:
                                    if act1 == "Work":
                                        textbutton "Church":
                                            action (SetVariable("job1", " at the Church"), SetVariable("decideact", 0))
                                        textbutton "Farm":
                                            action (SetVariable("job1", " at the Farm"), SetVariable("decideact", 0))
                                        textbutton "Graveyard":
                                            action (SetVariable("job1", " at the Graveyard"), SetVariable("decideact", 0))
                                        textbutton "Seedy Bar":
                                            action (SetVariable("job1", " at the Seedy Bar"), SetVariable("decideact", 0))
                                    if act1 == "Study":
                                        textbutton "Dance":
                                            action (SetVariable("job1", " Dance"), SetVariable("decideact", 0))
                                        textbutton "Rhetoric":
                                            action (SetVariable("job1", " Rhetoric"), SetVariable("decideact", 0))
                                        textbutton "Logic":
                                             action (SetVariable("job1", " Logic"), SetVariable("decideact", 0))
                                        textbutton "Science":
                                            action (SetVariable("job1", " Science"), SetVariable("decideact", 0))
                                    if act1 == "Explore":
                                        textbutton "Lake":
                                            action (SetVariable("job1", " the Lake"), SetVariable("decideact", 0))
                                        textbutton "Mountain":
                                            action (SetVariable("job1", " the Mountain"), SetVariable("decideact", 0))
                                        textbutton "Forest":
                                            action (SetVariable("job1", " the Forest"), SetVariable("decideact", 0))
                                        textbutton "Swamp":
                                            action (SetVariable("job1", " the Swamp"), SetVariable("decideact", 0))
                                    if act1 == "Rest":
                                        textbutton "At home":
                                            action (SetVariable("job1", " at home"), SetVariable("decideact", 0))
                                        textbutton "On holiday":
                                            action (SetVariable("job1", " on holiday"), SetVariable("decideact", 0))
                    text "What will she do for the second part of the month?"
                    textbutton (act2 + job2) action SetVariable("decideact", 2)
                    if decideact == 2:
                        hbox:
                            frame:
                                vbox:
                                    textbutton "Work" action SetVariable("act2", "Work")
                                    textbutton "Study" action SetVariable("act2", "Study")
                                    textbutton "Explore" action SetVariable("act2", "Explore")
                                    textbutton "Rest" action SetVariable("act2", "Rest")
                            frame:
                                vbox:
                                    if act2 == "Work":
                                        textbutton "Church":
                                            action (SetVariable("job2", " at the Church"), SetVariable("decideact", 0))
                                        textbutton "Farm":
                                            action (SetVariable("job2", " at the Farm"), SetVariable("decideact", 0))
                                        textbutton "Graveyard":
                                            action (SetVariable("job2", " at the Graveyard"), SetVariable("decideact", 0))
                                        textbutton "Seedy Bar":
                                            action (SetVariable("job2", " at the Seedy Bar"), SetVariable("decideact", 0))
                                    if act2 == "Study":
                                        textbutton "Dance":
                                            action (SetVariable("job2", " Dance"), SetVariable("decideact", 0))
                                        textbutton "Rhetoric":
                                            action (SetVariable("job2", " Rhetoric"), SetVariable("decideact", 0))
                                        textbutton "Logic":
                                             action (SetVariable("job2", " Logic"), SetVariable("decideact", 0))
                                        textbutton "Science":
                                            action (SetVariable("job2", " Science"), SetVariable("decideact", 0))
                                    if act2 == "Explore":
                                        textbutton "Lake":
                                            action (SetVariable("job2", " the Lake"), SetVariable("decideact", 0))
                                        textbutton "Mountain":
                                            action (SetVariable("job2", " the Mountain"), SetVariable("decideact", 0))
                                        textbutton "Forest":
                                            action (SetVariable("job2", " the Forest"), SetVariable("decideact", 0))
                                        textbutton "Swamp":
                                            action (SetVariable("job2", " the Swamp"), SetVariable("decideact", 0))
                                    if act2 == "Rest":
                                        textbutton "At home":
                                            action (SetVariable("job2", " at home"), SetVariable("decideact", 0))
                                        textbutton "On holiday":
                                            action (SetVariable("job2", " on holiday"), SetVariable("decideact", 0))
                    text "What will she do for the third part of the month?"
                    textbutton (act3 + job3) action SetVariable("decideact", 3)
                    if decideact == 3:
                        hbox:
                            frame:
                                vbox:
                                    textbutton "Work" action SetVariable("act3", "Work")
                                    textbutton "Study" action SetVariable("act3", "Study")
                                    textbutton "Explore" action SetVariable("act3", "Explore")
                                    textbutton "Rest" action SetVariable("act3", "Rest")
                            frame:
                                vbox:
                                    if act3 == "Work":
                                        textbutton "Church":
                                            action (SetVariable("job3", " at the Church"), SetVariable("decideact", 0))
                                        textbutton "Farm":
                                            action (SetVariable("job3", " at the Farm"), SetVariable("decideact", 0))
                                        textbutton "Graveyard":
                                            action (SetVariable("job3", " at the Graveyard"), SetVariable("decideact", 0))
                                        textbutton "Seedy Bar":
                                            action (SetVariable("job3", " at the Seedy Bar"), SetVariable("decideact", 0))
                                    if act3 == "Study":
                                        textbutton "Dance":
                                            action (SetVariable("job3", " Dance"), SetVariable("decideact", 0))
                                        textbutton "Rhetoric":
                                            action (SetVariable("job3", " Rhetoric"), SetVariable("decideact", 0))
                                        textbutton "Logic":
                                             action (SetVariable("job3", " Logic"), SetVariable("decideact", 0))
                                        textbutton "Science":
                                            action (SetVariable("job3", " Science"), SetVariable("decideact", 0))
                                    if act3 == "Explore":
                                        textbutton "Lake":
                                            action (SetVariable("job3", " the Lake"), SetVariable("decideact", 0))
                                        textbutton "Mountain":
                                            action (SetVariable("job3", " the Mountain"), SetVariable("decideact", 0))
                                        textbutton "Forest":
                                            action (SetVariable("job3", " the Forest"), SetVariable("decideact", 0))
                                        textbutton "Swamp":
                                            action (SetVariable("job3", " the Swamp"), SetVariable("decideact", 0))
                                    if act3 == "Rest":
                                        textbutton "At home":
                                            action (SetVariable("job3", " at home"), SetVariable("decideact", 0))
                                        textbutton "On holiday":
                                            action (SetVariable("job3", " on holiday"), SetVariable("decideact", 0))
                    if not job1 == " where?" and not job2 == " where?" and not job3 == " where?":
                        textbutton "Finished Deciding" action Jump("dotasks")
Now, remembering to initialise variables somewhere... Probably just after the start label?

Code: Select all

    $ act1 = "Go"
    $ job1 = " where?"
    $ act2 = "Go"
    $ job2 = " where?"
    $ act3 = "Go"
    $ job3 = " where?"
    $ decideact = 0
Now, showing the screen...

Code: Select all

    show screen planner
    $ ui.interact()
Then you need to set the variables back to their default values before you show the planner again.

You can of course mess around with the positioning and everything... And if you want your choices stored in a more useful variable/format, that's easy enough to add in.

You could probably save a lot of typing by defining stuff better, but I kind of just went ahead on the spur of the moment, so this is pretty unpolished.

QotC
Regular
Posts: 54
Joined: Thu Feb 17, 2011 8:53 pm
Contact:

Re: Buttons and Flyout Menus [Solved, probably]

#4 Post by QotC »

Woah. Thanks, Showsni! :)

QotC
Regular
Posts: 54
Joined: Thu Feb 17, 2011 8:53 pm
Contact:

Re: Buttons and Flyout Menus [Solved, probably]

#5 Post by QotC »

Okay, I'm trying to define a custom button that will save me some typing, but I seem to have hit a snag...

Code: Select all

    def choicebutton(name, clicked):
        ui.textbutton(name = name, clicked = clicked, hovered = SetVariable("highlighted", 1),   unhovered = SetVariable("highlighted", 0))

When I run my game, I get an error that "textbutton() takes at least 1 non-keyword argument (0 given). I can't figure out what it's talking about. :?

denzil
Veteran
Posts: 293
Joined: Wed Apr 20, 2005 4:01 pm
Contact:

Re: Buttons and Flyout Menus [Solved, probably]

#6 Post by denzil »

QotC wrote:When I run my game, I get an error that "textbutton() takes at least 1 non-keyword argument (0 given). I can't figure out what it's talking about. :?
In this case it means you got the name of the first parameter wrong and because the parameter is mandatory you get error message about missing parameter. The correct name for the first parameter is text and not name (see the ui.textbutton documentation):

Code: Select all

    def choicebutton(name, clicked):
        ui.textbutton( text=name, clicked = clicked, hovered = SetVariable("highlighted", 1),   unhovered = SetVariable("highlighted", 0))

Practice makes purrrfect.
Finished projects: Broken sky .:. colorless day .:. and few more...

QotC
Regular
Posts: 54
Joined: Thu Feb 17, 2011 8:53 pm
Contact:

Re: Buttons and Flyout Menus [Solved, probably]

#7 Post by QotC »

denzil wrote: In this case it means you got the name of the first parameter wrong and because the parameter is mandatory you get error message about missing parameter. The correct name for the first parameter is text and not name (see the ui.textbutton documentation):
Aha! Thanks. :)

ETA: Oddly, I still get the error when I define text = name. o_O However, if I omit the "text/name =" part entirely and just have

Code: Select all

    def choicebutton(name, clicked):
        ui.textbutton( name, clicked = clicked, hovered = SetVariable("highlighted", 1),   unhovered = SetVariable("highlighted", 0))

...it works fine. :?

More pressing at the moment, though, is a difficulty I'm having in handling all the chores that a button has to run upon being clicked:

Code: Select all

$ choicebutton("Farm", SetVariable("job1", " at the Farm") and SetVariable("decideact", 0) and SetVariable("highlighted", 0))
I.e.: "Set job, close the window, and you're not highlighted anymore."
Apparently, clicked will bypass all except for the last function. If I click that button as-is, it'll de-highlight, but nothing else happens. If I omit the part about highlighting, it'll only close the window. I have to delete the de-highlighting *and* the window-closing if I want it to set the job. :shock:

I'm guessing from this that clicked does not take And, but that doesn't make much sense to me: why wouldn't it?

ETAA:

Got it. :D I had to change it into a list. It works now, but I'm still curious as to the whys and hows of the whole thing.

Code: Select all

init python:       
    def choicebutton(name, clicked = []):
        ui.textbutton(name, clicked = clicked, hovered = SetVariable("highlighted", 1),   unhovered = SetVariable("highlighted", 0))
...
 $ choicebutton("Farm", [SetVariable("job1", " at the Farm"), SetVariable("decideact", 0), SetVariable("highlighted", 0)])

Post Reply

Who is online

Users browsing this forum: camzgr8game, munni