Usage of invoke_in_new_context function

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
kallistThe
Newbie
Posts: 5
Joined: Fri Feb 02, 2024 10:54 am
Contact:

Usage of invoke_in_new_context function

#1 Post by kallistThe »

I develope a kind of text quest using Ren'Py engine.

There are a lot of mini games. One of them offers player to react properly to some events.

Image

There is a timer that periodically triggers some event. The player sees event text in Ren'Py basic spaech placement and needs to click one of theree imgbuttons.
Timer triggers a fucntion with the code:

Code: Select all

def operate_cave_action(action):
        ......
        renpy.hide_screen("ch6_countdown")
        renpy.invoke_in_new_context(renpy.say, "", message)
        renpy.call("ch6_idle")
        return
Label ch6_idle has the following code:

Code: Select all

label ch6_idle:
		........
                $ ch6_timer_range = max(ch6_timer_range - ch6_timer_reduce, ch6_timer_min)
                $ ch6_time = ch6_timer_range
                $ ch6_event = roll_cave_enent()
                show screen ch6_countdown()

                $ ch6_event_text = ch6_cave_events[ch6_event]

                label ch6_action_blocker:
                    "[ch6_event_text]"
So all that works properly... but I have a little problem :D
After finishing that mini-game the basic Ren'Py field that displays text continue to display event text when all other renpy.say functions are fulfiled.
That make me think that I don't finish some context or smth like that.

Is that OK or I need to change something in my code?
Thank you!

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Usage of invoke_in_new_context function

#2 Post by m_from_space »

kallistThe wrote: Mon Feb 05, 2024 6:03 am

Code: Select all

def operate_cave_action(action):
        ......
        renpy.hide_screen("ch6_countdown")
        renpy.invoke_in_new_context(renpy.say, "", message)
        renpy.call("ch6_idle")
        return
Is that OK or I need to change something in my code?
Thank you!
If you call something, you have to return from it. Are you returning from ch6_idle?

User avatar
kallistThe
Newbie
Posts: 5
Joined: Fri Feb 02, 2024 10:54 am
Contact:

Re: Usage of invoke_in_new_context function

#3 Post by kallistThe »

m_from_space wrote: Mon Feb 05, 2024 6:42 am If you call something, you have to return from it. Are you returning from ch6_idle?
No... Where should I use return? Inside renpy code after label ch6_idle?

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Usage of invoke_in_new_context function

#4 Post by m_from_space »

kallistThe wrote: Mon Feb 05, 2024 7:25 am
m_from_space wrote: Mon Feb 05, 2024 6:42 am If you call something, you have to return from it. Are you returning from ch6_idle?
No... Where should I use return? Inside renpy code after label ch6_idle?
Yes, every label should end with either a return statement or a jump. Otherwise the Renpy game pointer will advance after that label (probably into another label or if you're lucky to the end of the file and therefore return automatically). Labels are are not closed automatically by the code's format like a Python function.

By the way, in your code you have "ch6_action_blocker" indented into the other label. But only sublabels (names starting with a dot) are considered part of the outer label.

Code: Select all

label mylabel:
    ...
    label .sublabel:
        ...
    return

label mylabel2:
    ...
    return

User avatar
kallistThe
Newbie
Posts: 5
Joined: Fri Feb 02, 2024 10:54 am
Contact:

Re: Usage of invoke_in_new_context function

#5 Post by kallistThe »

Oh, I see, thank you! Now try to understand where I should paste return statement.

By the way, after I use invoke_in_new_context function function, Ren'Py hides all screens. How can I show them in that new context?
Image

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Usage of invoke_in_new_context function

#6 Post by m_from_space »

kallistThe wrote: Mon Feb 05, 2024 5:53 pm Oh, I see, thank you! Now try to understand where I should paste return statement.

By the way, after I use invoke_in_new_context function function, Ren'Py hides all screens. How can I show them in that new context?
Image
I wouldn't use any new context at all, maybe just return to the current statement after the call. Try if it makes sense in your project. So basically, you call a label, that shows the message, that then calls your idle label, after both returning, the function will go back to where it started, showing the last message again.

Code: Select all

default message = ""

init python:
    def operate_cave_action(action):
        global message
        ......
        renpy.hide_screen("ch6_countdown")
        # nope ... renpy.invoke_in_new_context(renpy.say, "", message)
        # nope ... renpy.call("ch6_idle")
        renpy.call("ch6_interrupt", from_current=True)
        return
        
label ch6_interrupt:
    # assuming "message" is a global variable
    "[message]"
    call ch6_idle
    return
    
label ch6_idle:
    ...
    return

Post Reply

Who is online

Users browsing this forum: No registered users