Ren'Py Need help with a Ren'Py script to auto-save persistent data

ipaqi

Newbie
May 5, 2017
22
44
Hey, guys.

Over the last couple of weeks, I've had a couple of PC auto shutdowns and graphics driver updates that caused my games to crash, meaning that their persistent data never got saved.

I found that annoying (especially since gallery unlocks, read lines, etc, are almost always contained in that file), so I wanted to write a small script that people would be able to add to their games to auto-save.

Now, programmatically saving persistent data is extremely easy. Just simply need to call:

Code:
renpy.save_persistent()
My problem lies with being able to do the saving automatically, say, every 15 minutes.

I tried two methods (with a 60 second interval to simplify testing), but both failed in different ways with the games I tested.
The following are from the scenarios I tested on the game "Luke's Way", which is using Ren'Py 7.3.5.606 :

Method A (renpy.ui.timer):
Code:
ui.timer(60.0, renpy.save_persistent, True)
With this method I get a console output of
Code:
<Timer at {some memory address}>
But as time goes by, the save_persistent method never seems to get called

Method B:
Code:
timer 60.0 repeat True action renpy.save_persistent
With this method, the console output is simply:

Code:
File "game/zzz_persistentTimer.rpy", line 2: expected statement.
    timer 60.0 repeat True action renpy.save_persistent
            ^
Can anybody explain what I'm doing wrong or give me an alternative to running a timer / repeating timed event in Ren'Py? Preferably something that is supported in old versions as well?
 

ipaqi

Newbie
May 5, 2017
22
44
Alternatively, if you can help me find a way to call renpy.save_persistent whenever a regular save is triggered (maybe including auto saves, but not needed), that would be a good enough fix as well, I believe
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,263
15,073
Method A (renpy.ui.timer):
Code:
ui.timer(60.0, renpy.save_persistent, True)
With this method I get a console output of
Code:
<Timer at {some memory address}>
But as time goes by, the save_persistent method never seems to get called
Well, you create the object, but it's hosted by nothing, so it can't be triggered.


Method B:
Code:
timer 60.0 repeat True action renpy.save_persistent
With this method, the console output is simply:

Code:
File "game/zzz_persistentTimer.rpy", line 2: expected statement.
    timer 60.0 repeat True action renpy.save_persistent
            ^
And here I guess that you just tried to use a screen statement outside of a screen.


what you are looking for is something like this:
/!\ wrote on the fly, there's possibly typos /!\
Python:
init python:

    class AutoPersistent( renpy.python.RevertableObject ):
        def __init__( self, delay ):
            self.delay = delay
            self.tick = delay

        def __call__( self ):
            self.tick -= 1
            if self.tick > 0: return
            self.tick = self.delay
            renpy.save_persistent()

    # 15 minutes of 60 seconds that are divided in 20 units
    config.periodic_callbacks.append( AutoPersistent( 15 * 60 * 20 ) )
 

ipaqi

Newbie
May 5, 2017
22
44
Well, you create the object, but it's hosted by nothing, so it can't be triggered.




And here I guess that you just tried to use a screen statement outside of a screen.


what you are looking for is something like this:
/!\ wrote on the fly, there's possibly typos /!\
Python:
init python:

    class AutoPersistent( renpy.python.RevertableObject ):
        def __init__( self, delay ):
            self.delay = delay
            self.tick = delay

        def __call__( self ):
            self.tick -= 1
            if self.tick > 0: return
            self.tick = self.delay
            renpy.save_persistent()

    # 15 minutes of 60 seconds that are divided in 20 units
    config.periodic_callbacks.append( AutoPersistent( 15 * 60 * 20 ) )
Thank you so much!

I haven't tested it yet, but I'll try to do it tomorrow.