Setting _game_menu_screen dynamically

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
plaster
Regular
Posts: 89
Joined: Thu Jul 11, 2013 1:03 am
Tumblr: plasterbrain
Soundcloud: plasterbrain
Location: Chicago
Contact:

Setting _game_menu_screen dynamically

#1 Post by plaster » Fri May 26, 2017 3:45 am

My game menu has four screens, which we'll call foo, bar, and baz. By default, _game_menu_screen is set to "foo", so hitting the menu key, X, or right-clicking will open foo.

If a player goes to any one of the other screens and then closes the menu from there, I would like to be change _game_menu_screen to the last screen they accessed. For example, if someone goes to bar, and then closes out of the in-game menu, the next time they press X, I would like to open bar instead of foo.

Doing this:

Code: Select all

screen foo():
    on "show" action SetVariable("_game_menu_screen", "foo")
    text "[_game_menu_screen]"
shows "foo" on the screen. But if config._game_menu_screen was set to "bar," _invoke_game_menu will open bar. If I set _game_menu_screen to "baz" within the actual game, the game menu will become baz, but foo will still show "foo" !

The difficulty is that the in-game menu is a different context than the game itself, right? I don't really understand contexts, call stacks, or store variables. I am hack-and-slashing my way through OOP tbh. pls help tho I'm out of ideas :[

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Setting _game_menu_screen dynamically

#2 Post by PyTom » Mon May 29, 2017 11:41 pm

So, _game_menu_screen is, for a bunch of reasons, treated specially. Ren'Py makes it a dynamic variable when entering the the menus, and hence resets it to its own value when leaving.

To avoid it, avoid using _game_menu_screen entirely. Instead, intercept it at config.game_menu_action. My version is this, with a little bit of setup.

Code: Select all

default game_menu_screen = "save"

init python:

    class InvokeGameMenu(Action):

        def __call__(self):
            return renpy.run(ShowMenu(game_menu_screen))

    config.game_menu_action = InvokeGameMenu()
And then code like this in every screen.

Code: Select all

    on "show" action SetVariable("game_menu_screen", "save")
    on "replace" action SetVariable("game_menu_screen", "save")

You probably want to run on replace as well as show, so you get it triggered when changing screens. There's nothing special about the name game_menu_show, it just happens to be similar to _game_menu_show.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Bing [Bot]