• To improve security, we will soon start forcing password resets for any account that uses a weak password on the next login. If you have a weak password or a defunct email, please update it now to prevent future disruption.

Ren'Py Having trouble running a function in the background using Python

Dogrin

New Member
Aug 19, 2023
2
0
Hello!
I'm facing an issue with running a function in the background. Basically, I want a Python idle function to perform calculations and update a progress bar in the background.
I utilized the "renpy.restart_interaction()" command to update the progress bar. Without it, I wouldn't get a visual update for the progress bar. Everything seems to be going well, but after some time, I encounter this error:


Code:
While running game code:
  File "game/scripts/test.rpy", line 24, in script
    call screen progress_bar
  File "renpy/common/000statements.rpy", line 670, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
Exception: renpy.restart_interaction() was called 100 times without processing any input.

Here is a code which I use:

Code:
init python:
    from time import time
    class IdleRun:
        def __init__(self):
            self.start = time()
            self.progress_bar_value = 0

        def update_progress_bar(self):
            if time() - self.start >= 0.1:
                self.progress_bar_value += 1
                renpy.restart_interaction()
                # self.start = time()

    
    # a method to run some calculations in the background
    def background_update(t, st, at):
        idlerun.update_progress_bar()
        return 0

label progress_bar_test:
    $ idlerun = IdleRun()
    $ update_placeholder = Transform(function = background_update)

    call screen progress_bar
    return

screen progress_bar:
    add update_placeholder
    bar:
        value idlerun.progress_bar_value
        range 100
        xysize(200,25)
        xalign 1.0
        yalign 0.5
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,219
14,973
I'm facing an issue with running a function in the background.
Then this how to is made for you, more precisely its part regarding the timer screen statement.
Alternatively it would also works with a , but there's no real need to go this far.
 

Dogrin

New Member
Aug 19, 2023
2
0
Thank you very much!
"config.periodic_callbacks" is definitely what I need.
Now I know how to make it the most efficient way, thanks to you!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,219
14,973
Thank you very much!
"config.periodic_callbacks" is definitely what I need.
Now I know how to make it the most efficient way, thanks to you!
While, as I said, the timer screen statement is how you should do it, since it would take care of the screen update for you...
 
  • Like
Reactions: Dogrin