[solved] Show a screen on key press?

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
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

[solved] Show a screen on key press?

#1 Post by Imperf3kt »

I'm in need of making the "h" button change from hiding the interface, to opening a screen (history) since there's no room in the GUI I'm working on for a button.

I read through the documentation and tried this, but it obviously doesn't work because the keymap doesn't know what "ShowMenu" means.

Code: Select all

## experimental
init python:
    config.keymap['hide_windows'].remove('h')
    config.keymap['ShowMenu("history")'].append('h')
    
Can anyone help point out what I should be doing?
Last edited by Imperf3kt on Sat Mar 16, 2019 8:42 am, edited 3 times in total.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter


User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Show a screen on keypress?

#3 Post by Remix »

Code: Select all

init python:

    config.keymap['hide_windows'].remove('h')

    config.underlay.append(
        renpy.Keymap( 
            K_h = lambda: renpy.run( ShowMenu("history") ) ) )
Frameworks & Scriptlets:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Show a screen on keypress?

#4 Post by Imperf3kt »

Remix wrote: Thu Mar 14, 2019 4:51 pm

Code: Select all

init python:

    config.keymap['hide_windows'].remove('h')

    config.underlay.append(
        renpy.Keymap( 
            K_h = lambda: renpy.run( ShowMenu("history") ) ) )
Thanks, I'll try this later.

Out of curiosity, where did you get lambda from. I looked in the 00keymap file and found renpy.run, but never saw anything about lambda.

Andredron, thank you, I'll give it a try, but I think I prefer not to use a screen for this.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Show a screen on keypress?

#5 Post by Remix »

Lambda is just Python's way of doing anonymous functions. The Keymap arguments basically do
(set) var = (call) function
we cannot just do K_h = renpy.run as that would not pass our required parameters into it.

This leaves us a few options:

Declare a function...

Code: Select all

init python:

    def open_history():
        renpy.run( ShowMenu("history") )
    
    config.underlay.append(
        renpy.Keymap( 
            alt_K_h = open_history  ) )
Same as lambda except we have to actually define the function

or

Partial or Curry the call...

Code: Select all

init python:

    config.underlay.append(
        renpy.Keymap( 
            alt_K_h = renpy.curry(renpy.run)( ShowMenu("history") ) ) )
So, to answer you - there are options, just lambda (for that scenario) seemed most apt simply by being shortest
Frameworks & Scriptlets:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Show a screen on keypress?

#6 Post by Imperf3kt »

Remix wrote: Thu Mar 14, 2019 7:20 pm Lambda is just Python's way of doing anonymous functions. The Keymap arguments basically do
(set) var = (call) function
we cannot just do K_h = renpy.run as that would not pass our required parameters into it.

This leaves us a few options:

Declare a function...

Code: Select all

init python:

    def open_history():
        renpy.run( ShowMenu("history") )
    
    config.underlay.append(
        renpy.Keymap( 
            alt_K_h = open_history  ) )
Same as lambda except we have to actually define the function

or

Partial or Curry the call...

Code: Select all

init python:

    config.underlay.append(
        renpy.Keymap( 
            alt_K_h = renpy.curry(renpy.run)( ShowMenu("history") ) ) )
So, to answer you - there are options, just lambda (for that scenario) seemed most apt simply by being shortest
Many thanks for the explanation. I looked up lambda but couldn't make sense of it.

I had tried declaring a function similar to how you did it here

Code: Select all

init python:

    def open_history():
        renpy.run( ShowMenu("history") )
    
    config.underlay.append(
        renpy.Keymap( 
            alt_K_h = open_history  ) )
But it had the same issue where it didn't know what showmenu was.

I tried to place it in an init -1600 python block, maybe that was part of the problem.

I'll be home in an hour or so to test.
I really appreciate your guidance.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Show a screen on keypress?

#7 Post by Imperf3kt »

I gave it a test, but I figured I'd change it to ToggleScreen so a player could close the screen without having to move the mouse to the return button and this presented an issue where the quick menu isn't hidden.
I can fix this by adding the history screen to the quick_menu variable check, so that is not really any kind of issue though.

Code: Select all

if quick_menu and renpy.get_screen("say") and not renpy.get_screen("history"):
But here's the issue I can't work out how to fix.
While at the main menu, if a player presses the button it still shows the history screen.
My history screen uses the game_menu like a default Ren'Py project (its the only screen that does) and so its showing the main_menu background and a return button and nothing else.

I don't want this screen to be able to be shown from the main menu, is there a way to block it until a player is in the game and not at the main menu?

Edit:
ToggleScreen causes a lot of issues more than I listed, I'm going to use ShowMenu instead.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Show a screen on keypress?

#8 Post by Remix »

Perhaps the logical approach might be to key the history screen so 'h' there hides it...

Code: Select all

screen history():
    key "h" action Hide("history")
... etc
Untested...
Frameworks & Scriptlets:

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Show a screen on keypress?

#9 Post by Imperf3kt »

Remix wrote: Fri Mar 15, 2019 8:40 pm Perhaps the logical approach might be to key the history screen so 'h' there hides it...

Code: Select all

screen history():
    key "h" action Hide("history")
... etc
Untested...
Ah, didn't realise I could do that.
I forgot about this suggestion by Andredron, thanks!
By the way, I was asked to change the key from H, to L, but overall it makes no difference. Thank you for the help.

I suppose if I key the button in the main menu, to do nothing, that might fix my issue.

Edit:
No, unfortunately, Show works, but Hide does nothing, so I ended up doing this:

In screen main_menu, I added

Code: Select all

key "l" action Jump("before_main_menu")
Note I do have a label before_main_menu:
It won't work without one.


And I placed this in a file I call inits.rpy

Code: Select all

init python:

    config.underlay.append(
        renpy.Keymap( 
            K_l = lambda: renpy.run( ShowMenu("history") ) ) )
    
    
Pressing "L" at the main menu does nothing and works as expected elsewhere.
I must manually press the "return" button, but at least all the odd bugs I was seeing are gone.

Gah, nevermind, pressing l on any other screen, while in the main menu context, still breaks things.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Show a screen on key press?

#10 Post by Alex »

Since you show your history screen using ShowMenu("history") action, try to hide it using Return() action. Like all the other menus do...

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [solved] Show a screen on key press?

#11 Post by Imperf3kt »

Alex wrote: Sat Mar 16, 2019 8:19 am Since you show your history screen using ShowMenu("history") action, try to hide it using Return() action. Like all the other menus do...
Thankyou so much! This fixed all the weird bugs.

Here's exactly what I did to fix it:

In the quick menu screen, I added:

Code: Select all

    key "l" action ShowMenu("history")
    
In the inits section of screens.rpy, I added

Code: Select all

init python:

    config.underlay.append(
        renpy.Keymap( 
            K_l = lambda: renpy.run( NullAction() ) ) )
And in the history screen, I added

Code: Select all

    key "l" action Return()
    
This has things working perfectly, thanks to all who helped!
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: Google [Bot]