Ren'Py A little help with key press inputs

Jun 21, 2020
67
86
Ok, so I actually have two main issues that I can't seem to get info about how to solve them.

I have a screen with two buttons (called left and right) that do stuff (doesn't really matter what they do), I want to achieve the same result of what they do, but using shift+left/shift+right AND shift+A/shift+D for left and right buttons respectively.
For now I managed to do this in separate lines, but:
  1. [SOLVED] Is there a way that I can have two keys for the same action in just the one line, instead of having duplicates? (it's not that important, but the tidier the code, the better for my ocd :LOL: )
  2. [SOLVED] Another thing: is there a way that the actions of these key-presses replicate a buttons selected style? ...to be more specific, I want to make each button seem selected (highlight a little bit) when the mentioned keys are pressed respectively. So that, for example, the left/A keys highlight the left button on a key-press. Is there a way to achieve that or similar?
  3. [SOLVED] Edit: also I added two lines (one in each button) with a comment that says that they don't work for some reason, don't know why (they are supposed to do the same the hover_background does but on selection). Also, I know for sure that they don't work because I tried removing hover, and leaving selected and it still wouldn't highlight it on click.
Here's my screen code for reference (just some styled frames, two buttons and the key statements):
Python:
screen room_selector():
    key "shift_K_LEFT" action [SetVariable("location", move_to("left", location)), Return()]
    key "shift_K_a" action [SetVariable("location", move_to("left", location)), Return()]

    key "shift_K_RIGHT" action [SetVariable("location", move_to("right", location)), Return()]
    key "shift_K_d" action [SetVariable("location", move_to("left", location)), Return()]

    hbox:
        xalign 0.5
        yalign 0.0
        xsize 192*2
        ysize 54
            
        frame:
            yoffset 27
            xfill True
            style 'minimap_container'

            button:
                xsize 108
                ysize 54
                xpos 0
                yalign 0.5
                text "Left" size 13 xalign 0.5 yalign 0.5
                                            
                background "#a43ac2"
                hover_background lighten_color("#a43ac2", 0.2)
                selected_background  lighten_color("#a43ac2", 0.3) # this line does not work for some reason


                action [SetVariable("location", move_to("left", location)), Return()]

            button:
                xsize 108
                ysize 54
                xalign 1.0
                yalign 0.5
                text "Right" size 13 xalign 0.5 yalign 0.5
                                                        
                background "#a43ac2"
                hover_background lighten_color("#a43ac2", 0.2)
                selected_background  lighten_color("#a43ac2", 0.3) # this line does not work for some reason

                action [SetVariable("location", move_to("right", location)), Return()]
 
Last edited:

GetOutOfMyLab

Conversation Conqueror
Modder
Aug 13, 2021
7,335
20,109
Try putting them in a list:
Python:
key ["shift_K_LEFT", "shift_K_a"] action [SetVariable("location", move_to("left", location)), Return()]
 
  • Heart
Reactions: The Lewd Gamer

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,963
16,201
Another thing: is there a way that the actions of these key-presses replicate a buttons selected style? ...to be more specific, I want to make each button seem selected (highlight a little bit) when the mentioned keys are pressed respectively. So that, for example, the left/A keys highlight the left button on a key-press. Is there a way to achieve that or similar?
Yes, but it absolutely don't worth the pain.

Python:
screen whatever():

    default left_selected = False

    key "keydown_K_a" action SetLocalVariable( "left_selected", True )    
    key "keyup_K_a" action [ SetLocalVariable( "left_selected", False ), SetVariable("location", move_to("left", location)), Return()]

    button:
       [...]
       selected left_selected
       [...]
All this for something that would last the fraction of second during which the player will press the key combination.


also I added two lines (one in each button) with a comment that says that they don't work for some reason, don't know why (they are supposed to do the same the hover_background does but on selection). Also, I know for sure that they don't work because I tried removing hover, and leaving selected and it still wouldn't highlight it on click.
It doesn't works because a button can only be in a selected state if all its action return a True value. And here, none of the actions return that True value.
SetVariable() would return True only if the value of the "location" variable correspond to the value returned by move_to, something that will never happen. As for Return(), it close the screen, therefore it will never return True ; a button can't be selected if the screen containing it isn't displayed.

And even forcing the selected state, by copying the key trick would be useless. Assuming that the state would change before Ren'Py would process all the actions, it would only stay in that state for during the millisecond (at most) that Ren'Py would need to process all the action and close the screen.

More globally, buttons like "next" and "prev" are never selected, because they do not represent a state. This whatever that state can be. A value is never "next", it's the next value ; here the next location.
 
Jun 21, 2020
67
86
Yes, but it absolutely don't worth the pain.

Python:
screen whatever():

    default left_selected = False

    key "keydown_K_a" action SetLocalVariable( "left_selected", True )   
    key "keyup_K_a" action [ SetLocalVariable( "left_selected", False ), SetVariable("location", move_to("left", location)), Return()]

    button:
       [...]
       selected left_selected
       [...]
All this for something that would last the fraction of second during which the player will press the key combination.




It doesn't works because a button can only be in a selected state if all its action return a True value. And here, none of the actions return that True value.
SetVariable() would return True only if the value of the "location" variable correspond to the value returned by move_to, something that will never happen. As for Return(), it close the screen, therefore it will never return True ; a button can't be selected if the screen containing it isn't displayed.

And even forcing the selected state, by copying the key trick would be useless. Assuming that the state would change before Ren'Py would process all the actions, it would only stay in that state for during the millisecond (at most) that Ren'Py would need to process all the action and close the screen.

More globally, buttons like "next" and "prev" are never selected, because they do not represent a state. This whatever that state can be. A value is never "next", it's the next value ; here the next location.
I think I get what you mean ann. If the screen reloads every game loop, even if I get this "selected state style" to work the player won't be able to see it because it would immediately disappear and be replaced with the new screen after returning in less than a second. It's a shame that those keys won't give any interaction feedback except for the actual location change.
Thanks again for your help anyways!