Keymap's in Ren'py

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
Wildmask
Newbie
Posts: 15
Joined: Sun Oct 02, 2016 1:18 pm
Contact:

Keymap's in Ren'py

#1 Post by Wildmask »

Hello there. I'm trying to program a menu into my game. The menu in the game is going to have many clickable buttons, that display save options, the character profiles, items, and other miscellaneous things that involve images, text, and buttons. My my problem is, however, I want the player to be able to press a button on the key board (say A or Q) and open this menu any point during the game after they receive a specific item that will allow them access to the menu.

I have defined the menu as just an imagemap, however would it be best to define it as a screen as well? As different buttons do different things, I'm sure I'm going to have to throw a few labels in there. Also, how do I define it as a keymap with an if statement? I have a basic understanding of keymaps, however all the codes I have tried don't work out for me. They don't crash my game, they just do nothing. I may be placing them in the wrong areas, but I'm not too sure.

Sorry if this is really newbish or a bit much, it's just the keymap programming that's giving me a hard time ^^;

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Keymap's in Ren'py

#2 Post by Divona »

I want the player to be able to press a button on the key board (say A or Q) and open this menu any point during the game after they receive a specific item that will allow them access to the menu.

To add the key to be functional permanently:

Code: Select all

define player_has_item = True

init python:

    # Define function to open the menu
    def show_pause_menu():
        if player_has_item == True:
            renpy.call_screen("my_pause_menu")

    # Add key to 'open_pause_menu', this case is 'a' on keyboard
    config.keymap["open_pause_menu"] = ["a"]
    
    # When key is pressed at anytime, open custom screen.
    config.underlay.append(renpy.Keymap(open_pause_menu=show_pause_menu))

......

screen my_pause_menu():
......
I have written the code above from my memory, and have not check it in Ren'Py, but it's something like that.

Have you look at using 'key' in screens?
https://www.renpy.org/doc/html/screens.html#key

There also 'Customizing the Keymap' in documentation if you want to add custom keys to the list.
https://www.renpy.org/doc/html/keymap.html#keymap
Completed:
Image

User avatar
Wildmask
Newbie
Posts: 15
Joined: Sun Oct 02, 2016 1:18 pm
Contact:

Re: Keymap's in Ren'py

#3 Post by Wildmask »

Thank you for the speedy response!
Have you look at using 'key' in screens?
https://www.renpy.org/doc/html/screens.html#key

There also 'Customizing the Keymap' in documentation if you want to add custom keys to the list.
https://www.renpy.org/doc/html/keymap.html#keymap
I have looked at both pages. I've tried using these documents as examples for my coding, but they they produced no results.

I tried the code you provided, and it worked, thank you so much! But, if you don't mind, I have two questions about it.

How do I make it so the menu (Which I have defined as a screen) covers the text box? If that is possible.
The second question being, there seems to be a delay as I bring up the screen. I press "a" then I have to click the screen (or press Enter) in order for the menu to show up, which messes with the dialogue. Is there a way to make it that the instant I press "a" the menu comes up?

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Keymap's in Ren'py

#4 Post by Divona »

Wildmask wrote:How do I make it so the menu (Which I have defined as a screen) covers the text box? If that is possible.
You will need to set zorder. Probably set it to 100 or something.
https://www.renpy.org/doc/html/screens. ... -statement
Wildmask wrote:The second question being, there seems to be a delay as I bring up the screen. I press "a" then I have to click the screen (or press Enter) in order for the menu to show up, which messes with the dialogue. Is there a way to make it that the instant I press "a" the menu comes up?
Try set modal to True also?

Code: Select all

modal True
Alright, I actually tried out the code in Ren'Py this time, and make some changes to make sure things going well:

Code: Select all

define player_has_item = True

init python:

    # Define function to open the menu
    def show_pause_menu():
        if player_has_item == True:
            renpy.call_in_new_context("display_pause_menu")

    # Add key to 'open_pause_menu', this case is 'a' on keyboard
    config.keymap["open_pause_menu"] = ["a"]
    
    # When key is pressed at anytime, open custom screen.
    config.underlay.append(renpy.Keymap(open_pause_menu=show_pause_menu))
......
label display_pause_menu:
    call screen my_pause_menu
......
screen my_pause_menu():
......
It could also be because of the screen transition. Try set the following line in 'options.rpy' to 'None' if problem still exist:

Code: Select all

define config.enter_transition = None
define config.exit_transition = None
Completed:
Image

User avatar
Wildmask
Newbie
Posts: 15
Joined: Sun Oct 02, 2016 1:18 pm
Contact:

Re: Keymap's in Ren'py

#5 Post by Wildmask »

I added zorder and modal True to the code, and it worked! Thank you for the big help!

I just have one last small question, what command would I use to let it so when the player presses, for example, "q", it closes the menu? It sounds really simple but I've tried multiple codings of config.keymap and I can't seem to figure it out.

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Keymap's in Ren'py

#6 Post by Divona »

Would this work?:

Code: Select all

screen my_pause_menu():
    key "q" action renpy.hide_screen
......
Completed:
Image

User avatar
Wildmask
Newbie
Posts: 15
Joined: Sun Oct 02, 2016 1:18 pm
Contact:

Re: Keymap's in Ren'py

#7 Post by Wildmask »

Divona wrote:Would this work?:

Code: Select all

screen my_pause_menu():
    key "q" action renpy.hide_screen
......
Apparently not. When inputting this, the game works fine until I go to press "q" to exit the menu. At which point I'm given this error:

Code: Select all

    store._return = renpy.call_screen(name, *args, **kwargs)
TypeError: hide_screen() takes at least 1 argument (0 given)
I tried changing the code to "key "q" action renpy.hide_screen_MyScreen" or "key "q" action hide_screen(MyScreen)" but instead it seems to invalidate the function of opening the menu, and gives me this error once I press "a":

Code: Select all

AttributeError: 'module' object has no attribute 'hide_screen_MyScreen'

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Keymap's in Ren'py

#8 Post by Divona »

Code: Select all

screen my_pause_menu():
    tag custom_menu

    key "q" action Function(renpy.hide_screen, "custom_menu")
Completed:
Image

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: Keymap's in Ren'py

#9 Post by trooper6 »

Wildmask. You are using screen actions. All the screen actions are documented here: https://www.renpy.org/doc/html/screen_actions.html
The action you want is not Function, but Hide

Code: Select all

screen my_pause_menu():
    key "q" action Hide("custom_menu")
You may want to look at that page in general to see the different options you have available
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

User avatar
Wildmask
Newbie
Posts: 15
Joined: Sun Oct 02, 2016 1:18 pm
Contact:

Re: Keymap's in Ren'py

#10 Post by Wildmask »

trooper6 wrote:Wildmask. You are using screen actions. All the screen actions are documented here: https://www.renpy.org/doc/html/screen_actions.html
The action you want is not Function, but Hide

Code: Select all

screen my_pause_menu():
    key "q" action Hide("custom_menu")
You may want to look at that page in general to see the different options you have available
Divona wrote:

Code: Select all

screen my_pause_menu():
    tag custom_menu

    key "q" action Function(renpy.hide_screen, "custom_menu")
I tested them and they both work, thank you so much for the help!

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]