Calling a call from screen

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
User avatar
Posts: 402
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia

Calling a call from screen

#1 Post by Andredron » Sat May 18, 2019 10:42 pm

In order not to ruin all the addressing and other details unimportant to us, in Renpy it is impossible to make a call from the screen (switching to a label with a subsequent return). You can go to the label in a new context, but then Renpy will hide all screens. This is an example of how you can organize a call on a button and display hidden screens as if they were not hidden. But the button on which we jumped on the label, it is better to hide. Or replace with the back button

Code: Select all

# to call a location with a button on the screen screen,
# need to call it in a new context.
# i.e. another game opens up.
# and hide all previous screens.

# that's just when you save the game, only the place is remembered,
# from where the first call was called, as if all the return had already worked
# therefore, saving is better to disable
#Total: the method works only for all sorts of menus,
# but not for branching plot

init python:
    # stack for storing state of screens until call
    screens = []
    # action by ESC button
    gamemenu = config.game_menu_action
    # add a regular list of screens
    def s_push (item):
        global screens
        screens.append (item)
    # get the latest list of screens
    def s_pop ():
        global screens
        if len (screens)> 0:
            return screens.pop ()
        return []
    # starting amount of money for testing
    money = 10
    # flag calling location in new context
    is_call = False
    # from the screen can not perform the usual call label
    # create an analog
    class MyCall (Action):
        def __init __ (self, label, * args, ** kwargs):
            self.label = label
            self.args = args
            self.kwargs = kwargs
        def __call __ (self):
            global screens, is_call
            # disable ESC
            config.game_menu_action = NullAction ()
            # remember screens
            s_push (renpy.current_screen (). screen_name)
            # turn on the call flag of the new context (to hide the button)
            is_call = true
            # call location in new context
            renpy.call_in_new_context (self.label, * self.args, ** self.kwargs)
    # function to restore screens in new context
    def show_screens ():
        for i in screens [-1:]:
            renpy.show_screen (i)
    # function to return from location in new context
    def myreturn ():
        global is_call
        # hide screens
        for i in s_pop ():
            renpy.hide_screen (i)
        # uncheck the flag of the new location so that its call button appears again
        is_call = len (screens)> 0
        if not is_call:
            config.game_menu_action = gamemenu
        # save game data
        renpy.retain_after_load ()
        Return () ()
    # so that you can bind to digging, for example
    MyReturn = renpy.curry (myreturn)

# screen from which you can call
screen test:
    text _ (str (money) + "money") align (.05, .05)
    # button is shown only if the call is not executed by pressing it
    if not is_call:
        # button that performs analog call label
        textbutton _ ("Cheat") align (.95, .05) action MyCall ("menu1")
        # call a call from a label that is already called using call
        textbutton _ ("hint") align (.95, .05) action MyCall ("hnt")

label start:
    show expression "images / bg.jpg"
    show screen test
    "You have created a new Ren'Py game."
    "Add a plot, images and music and send it to the world!"

# another location from which you can return to the same place of the game
# if you wish, you can pass parameters when calling
# then you can call it like this: ... action MyCall ("menu1", plus = 100)
label menu1 (plus = 10):
    # display all screens hidden when calling a location in a new context
    $ show_screens ()
    # Actually some actions
    $ loop1 = true
    # we loop before pressing the 3rd button
    while loop1:
            "+ [plus]":
                $ money + = plus
            "- [plus]":
                $ money - = plus
                $ loop1 = False
    # go back to where you came from, restoring the old state of affairs
    # instead of return
    $ MyReturn () ()

label hnt:
    scene black
    centered "Hint: This is a nested call from another call."
    $ MyReturn () ()

Download the finished sample. ... n.html?m=1
I know, I'm writing terribly in English.

I'm writing a Renpy textbook (in Russian). Update 22.06.18

Honest Critique

Post Reply

Who is online

Users browsing this forum: No registered users