How to show a screen from inside a custom displayable?

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
newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

How to show a screen from inside a custom displayable?

#1 Post by newbiemate »

How can I toggle a screen on/off (using show/hide) from inside a creator defined displayable[1]?

I tried using renpy.show_screen("test_screen"), but it doesn't show the screen.

To illustrate the problem, here is some sample code:

Code: Select all

init 1 python:
    class CustomDisplayable(renpy.Displayable):
        def __init__(self, **kwargs):
            super(CustomDisplayable, self).__init__(**kwargs)
    
        def render(self, width, height, st, at):
            render = renpy.Render(width, height)
            renpy.show_screen("test_screen")
            return render

            
screen test_screen:
    $ print "I am called"
    text "World" xpos 100 ypos 0
            
screen begin:
    add CustomDisplayable()
    
    text "Hello" xpos 0 ypos 0
            
label start:
    call screen begin()
    return
The text "I am called" never gets printed out.

[1] https://www.renpy.org/doc/html/udd.html

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: How to show a screen from inside a custom displayable?

#2 Post by trooper6 »

print is not a screen action.
Go with text.

Also...what are you ultimately trying to do? why do you want your image to regularly call a screen each second it is rendered? You are nesting so many screens in there? There has got to be an much easier way to do whatever it is you want to do. I'd say probably with just screen language.

So...what are you ultimately trying to do? Do you really need a CustomDisplayable at all?
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

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: How to show a screen from inside a custom displayable?

#3 Post by newbiemate »

I'm trying to code up a mini-game, and the user interaction is complex enough that I don't think using screen language will suffice (I've tried). I get the feeling that using custom displayables is discouraged, but it is very powerful and flexible depending on how you use it. And it aligns well for my use-case so far.

I have conditions that trigger the renpy.show_screen() in my code, so it won't render one after another constantly. But the point of the code sample above was to show that calling show_screen() is not working. As you said, it should be calling it over and over because it's in the render(), yet if you run the code it doesn't show up.

And you're right... "print" isn't a screen action, but I have it there so I can see it show up in the console when the screen is shown, which it never is and so that print statement never runs.

Is this expected behavior, that calling show_screen() will not show the screen? If not, then is this considered a bug, or am I missing something?

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: How to show a screen from inside a custom displayable?

#4 Post by trooper6 »

Displayables are quite powerful and very cool. I made an entire tutorial on them. But I have never seen an example of a displayable that included screen language. I don't know how wise that is. I see screens holding Displayables, but I don't see Displayables calling screens in their renders. Even the Pong mini game example in the tutorial doesn't involve that Displayable calling screens.

But if you are getting you game to work the way you want, keep going!
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
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: How to show a screen from inside a custom displayable?

#5 Post by Remix »

Theoretically it should work (even though I agree with trooper6 on it not being a wise approach) with a few areas that might be causing problems:

The displayable must be refreshing correctly, perhaps a print("render called") in the render method as a quick test to see if it runs lots of times per second.
Presuming the conditionals work, after the show_screen call it doesn't look like you are telling Ren'Py to redraw everything, perhaps a restart_interaction, full_redraw or redraw(get_screen('...'))
Simplest debug route is just scamper the render conditional logic and add "print('done NNN')" at various points... basically let the code say where it is rather than just waiting for a screen to show... If the console says the show_screen call was done and no screen showed, you know where the snaffu is

Personally, I'd put the logic in a screen, maybe add it to config.per_frame_screens if needed then have any interactable elements (displayables) just alter variable values to let the screen logic decide what to display (including on-off child screens)
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: How to show a screen from inside a custom displayable?

#6 Post by Imperf3kt »

Can you not just use a regular screen that contains a viewport? Just how complex is this minigame that you cannot use screen language?
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

newbiemate
Regular
Posts: 85
Joined: Tue Dec 19, 2017 1:36 pm
Contact:

Re: How to show a screen from inside a custom displayable?

#7 Post by newbiemate »

renpy.restart_interaction() worked... Although with everyone saying how weird it is to use show_screen() inside a CDD, I think I might be not understanding cdd correctly. Thanks guys! Might have to rewrite all my code...

When is it appropriate to use renpy.show_screen(), or any of the renpy.X functions? Because CDD is coded in python, and those functions are also in the section where python and renpy intersects.

Post Reply

Who is online

Users browsing this forum: Google [Bot]