Issue with textbuttons in menu executing clicked action

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
kareni
Newbie
Posts: 5
Joined: Sun Mar 01, 2015 1:53 pm
Contact:

Issue with textbuttons in menu executing clicked action

#1 Post by kareni »

Hello all,

I'm working on a menu screen ("closet") for customizing my character's outfit, but I almost immediately ran into an issue. I'm doing the menu in python because I am going to need to produce the textbuttons conditionally based on what clothing options the player has unlocked, but for now it looks like this:

Code: Select all

init -2 python:
    #function for changing the character's shirt
    def change_shirt(num):
        global shirt
        shirt = num
        return None, None

init -1 python:
    #the closet screen, with buttons for changing the character's shirt
    def closet_screen(**kwargs):
        # This ensures that any other menu screen is replaced.
        ui.tag("menu")

        ui.hbox(id="closet_hbox")
        
        ui.text("Shirt", id="closet_text")
        ui.textbutton("Orange", clicked=change_shirt(0), id="closet_textbutton0")
        ui.textbutton("Green", clicked=change_shirt(1), id="closet_textbutton1")
        ui.textbutton("Red", clicked=change_shirt(2), id="closet_textbutton2")
        
        ui.textbutton("Resume", clicked=ui.returns(0), id="closet_resume")
        
        ui.close()
    renpy.define_screen("closet_screen", closet_screen)

# call this label from the game's execution to show the closet screen
label closet_screen_label:
    call screen closet_screen
    return
The issue is that when I click the button that calls the closet_screen_label and thus the closet_screen, it changes the shirt variable to 2, thus making the player's shirt red without them clicking any of the options. I suspect it's executing "change_shirt(0), change_shirt(1), change_shirt(2)" when the screen is called, for some reason? Aside from this main issue, the buttons don't work, either (I can't use the buttons to change the players shirt). The shirt is set as a DynamicDisplayable and changing the shirt variable does work to change the shirt, so that's not the issue. I think I'm just doing the textbuttons clicked action wrong.

Can anyone help me with this? Thanks so much!

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Issue with textbuttons in menu executing clicked action

#2 Post by trooper6 »

Why can't you use screen language for this? I just created a test version of this in screen language with conditional buttons.

Here is a summary of what I did:
*There are lots of ways to do access your closet, I created a Closet button that will open and close the closet and put that in the bottom-ish right corner of the screen, which makes the Resume button a bit pointless. However, I also didn't make the closet screen modal...but I could see you wanting to do that. If you did, then you would want that resume screen and you could slim down the Closet button's code. I can see a case for both options.
*I didn't see the need for the change_shirt function when you can just set the shirt variable directly, so I got rid of that function.
*For your conditionals to work, I set up unlock flags, you'll see them after the start label. The way the If action works is (Condition, What to do if True, What to do if False), by having "None" as the what to do if False, the button is greyed out and insensate if the item is not unlocked.
*I also thought it might be nice, in the absence of your Dynamic Displayable graphics, to see that color is actually changing (i.e. that the variable change works), so I made it so that the shirt label changes color based on what button you've pushed.
*The code also unlocks each button in order so you can see that that works.

This is the code (this is the complete script.rpy file:

Code: Select all

screen closet_screen():
    tag menu
    #modal True 
    vbox:
        if shirt == 0:
            label "{color=#FF9900}Shirt" align (0.5, 0.1)
        elif shirt == 1:
            label "{color=#006600}Shirt" align (0.5, 0.1)
        elif shirt == 2:
            label "{color=#FF0000}Shirt" align (0.5, 0.1)
        else:
            label "Shirt" align (0.5, 0.1)
        hbox:
            textbutton "Orange" action If(unlock_orange_shirt, SetVariable ("shirt", 0), None)
            textbutton "Green" action If(unlock_green_shirt, SetVariable ("shirt", 1), None)
            textbutton "Red" action If(unlock_red_shirt, SetVariable ("shirt", 2), None)
            textbutton "Resume" action Hide("closet_screen")

screen control():
    frame:
        xalign 0.95
        yalign 0.7
        textbutton "Closet" action If(renpy.get_screen("closet_screen"), Hide("closet_screen"), Show("closet_screen"))

    
# The game starts here.
label start:
    $shirt = None
    $unlock_orange_shirt = False
    $unlock_green_shirt = False
    $unlock_red_shirt = False
    
    show screen control()
    
    "Nothing is unlocked--Orange: [unlock_orange_shirt], Green: [unlock_green_shirt], Red: [unlock_red_shirt]"
    
    $unlock_orange_shirt = True
    "Unlocking Orange--Orange: [unlock_orange_shirt], Green: [unlock_green_shirt], Red: [unlock_red_shirt]"
    
    $unlock_green_shirt = True   
    "Unlocking Green--Orange: [unlock_orange_shirt], Green: [unlock_green_shirt], Red: [unlock_red_shirt]"
     
    $unlock_red_shirt = True
    "Unlocking Red--Orange: [unlock_orange_shirt], Green: [unlock_green_shirt], Red: [unlock_red_shirt]"
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

kareni
Newbie
Posts: 5
Joined: Sun Mar 01, 2015 1:53 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#3 Post by kareni »

Oh my goodness this is amazing! Thank you so much this works perfectly. I didn't know you could do that much with the textbutton action! Is there a good reference you'd recommend for that specifically? And yes, I intend to make the closet screen modal, I actually only want the closet to be accessible when the player is in their apartment. Thanks again!

kareni
Newbie
Posts: 5
Joined: Sun Mar 01, 2015 1:53 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#4 Post by kareni »

Actually, now that I think about it, what would I do if I wanted to conditionally hide a button? As opposed to showing the button inactive by having the action's if condition return none, hide the shirt option entirely?

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#5 Post by philat »

Code: Select all

if unlock_orange_shirt:
    textbutton "Orange" action SetVariable("shirt",0)
The button won't show up if unlock_orange_shirt is False.

kareni
Newbie
Posts: 5
Joined: Sun Mar 01, 2015 1:53 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#6 Post by kareni »

Ah I didn't even think to try that. Thank you!

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Issue with textbuttons in menu executing clicked action

#7 Post by trooper6 »

There are a couple of questions here!
1. Reference to what sort of actions you can do is on the "Screen Actions, Values, and Functions" page of the Renpy Documentation:
http://www.renpy.org/doc/html/screen_actions.html

2. About only having the closet available if the player is in their apartment, that would be easy. In my version there was a control screen with the closet button. You only show that screen when you are in the apartment. Without the screen, they can't access the closet.

3. I don't know if there is a better way of doing it, but if I wanted to conditionally show a button, I'd just make an if statement.

Code: Select all

        hbox:
            textbutton "Orange" action If(unlock_orange_shirt, SetVariable ("shirt", 0), None)
            textbutton "Green" action If(unlock_green_shirt, SetVariable ("shirt", 1), None)
            textbutton "Red" action If(unlock_red_shirt, SetVariable ("shirt", 2), None)
            if show_resume:
                textbutton "Resume" action Hide("closet_screen")
With this the resume button will not be shown unless the show_resume variable is true.

ETA: Ninja'd!
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#8 Post by philat »

Don't worry, trooper, you provided more thorough and helpful answers. :P I was just skimming through and randomly responding to the last question only.

kareni
Newbie
Posts: 5
Joined: Sun Mar 01, 2015 1:53 pm
Contact:

Re: Issue with textbuttons in menu executing clicked action

#9 Post by kareni »

Thank you both! I appreciate it a lot!!

Post Reply

Who is online

Users browsing this forum: Google [Bot]