renpy/doc/cookbook/Button Game Menu

From Ren'Py Visual Novel Engine

Jump to: navigation, search

Menu Buttons

This is a recipe that you can use to make buttons appear on the main screen. These buttons can act as shortcuts to the game menu, toggles for skipping or other user preferences, or in order to bring up an inventory menu.

# This file adds a number of buttons to the lower-right hand corner of
# the screen. Three of these buttons jump to the game menu, which
# giving quick access to Load, Save, and Prefs. The fourth button
# toggles skipping, to make that more convenient.

init python:

    # Give us some space on the right side of the screen.
    style.window.right_padding = 100

    def toggle_skipping():
        config.skipping = not config.skipping

    show_button_game_menu = True

    def button_game_menu():

        if show_button_game_menu:

            # to save typing
            ccinc = renpy.curried_call_in_new_context

            ui.vbox(xpos=0.99, ypos=0.98, xanchor='right', yanchor='bottom')
            ui.textbutton("Skip", clicked=toggle_skipping, xminimum=80)
            ui.textbutton("Save", clicked=ccinc("_game_menu_save"), xminimum=80)
            ui.textbutton("Load", clicked=ccinc("_game_menu_load"), xminimum=80)
            ui.textbutton("Prefs", clicked=ccinc("_game_menu_preferences"), xminimum=80)
            ui.close()


    config.overlay_functions.append(button_game_menu)

Examples

If you want the buttons to be shown on the screen all the time, you can simply copy the above code into a .rpy file.

If you want to disable the buttons, such as during a minigame, you can write, for example,

label minigame:

    e "Let's play Poker!"

    # I want to disable the buttons, so I use:
    $ show_button_game_menu = False
    # Now the button is no longer shown.

    # ... poker code ...

    # Now I want to re-activate the buttons, so I use:
    $ show_button_game_menu = True

    e "That was fun."
    jump ending

When "$ show_button_game_menu = False" is run, that tells the computer that you do not want to show the button until you change show_button_game_menu to True again.

Creating Your Own Buttons

If you want to create your own on-screen button, you need to first create a variable that can be used to change whether or not the button is shown on the screen, then create a overlay function, and then finally attach the function to the overlay function list.

All of this needs to go inside a "init python:" block, which you create by typing "init python:" into one of you script files.

init python:
    # Give us some space on the right side of the screen.
    style.window.right_padding = 100

To create a variable that determines whether or not the button is shown, you need to first decide on an appropriate name. A good name would be one that describes what the button does; for example, in the example code, the variable was named "show_button_game_menu" because the buttons formed shortcuts to the game menu and the variable determines whether or not the menu should be shown. If you wanted a button that shows an inventory when clicked, a good variable name would be "show_inventory_button."

After you decide on the name, you have to decide whether or not you want the button to be shown at the beginning of the game, or if you want the button to be unlocked later. For the game menu example, it is a good idea to show the buttons starting from the beginning of the game. However, if you want to create a button that goes to a list of spells in the player's spellbook, and you don't let the player learn spells until halfway through the game, it would be a good idea to unlock the button later.

If you want to show the button from the beginning of the game, you would write "your_variable_name = True," and if you would write "your_variable_name = False"

init python:
    # Give us some space on the right side of the screen.
    style.window.right_padding = 100

    your_variable_name = True or False #depending on your decision

Next, you have to write the actual function itself.

init python:
    # Give us some space on the right side of the screen.
    style.window.right_padding = 100

    your_variable_name = True or False #depending on your decision

    def my_button_function():
        if your_variable_name:
            ui.vbox(xpos=0.99, ypos=0.98, xanchor='right', yanchor='bottom')
            ui.textbutton("Button Name", clicked=do_something, xminimum=80)
            ui.close()

And then finally, append the function to the config.overlay.

init python:
    # Give us some space on the right side of the screen.
    style.window.right_padding = 100

    your_variable_name = True or False #depending on your decision

    def my_button_function():
        if your_variable_name:
            ui.vbox(xpos=0.99, ypos=0.98, xanchor='right', yanchor='bottom')
            ui.textbutton("Button Name", clicked=do_something, xminimum=80)
            ui.close()

    config.overlay_functions.append(my_button_function)
Personal tools