Persistent Data - Ren'Py

documentation index ◦ reference manual ◦ function index

# Persistent Data

Ren'Py also supports persistent data, which is saved data that is not associated with a particular point in a game. Persistent data is data that is accessed through the fields of the persistent object, which is bound to the variable persistent.

Variable: | persistent | = ...

The persistent variable is bound to the special persistent object. All data reachable through fields on persistent is saved whenver Ren'Py terminates, and loaded when Ren'Py resumes. The persistent object is special in that an access to an undefined field on it will have a None value, rather than causing an exception.

An example use of persistent is the creation of an unlockable image gallery. This is done by storing a flag in persistent that determines if the gallery has been unlocked, as in:

    label gallery:

        if not persistent.gallery_unlocked:
            show background
            centered "You haven't unlocked this gallery yet."
            $ renpy.full_restart()

        # Actually show the gallery here.

When the user gets an ending that causes the gallery to be unlocked, the flag must be set to True:

    $ persistent.gallery_unlocked = True

## Multi-Game Persistence

Multi-Game persistence is a feature that lets you share information between Ren'Py games. This may be useful if you plan to make a series of games, and want to have them share information.

To use multipersistent data, a MultiPersistent object must be created inside an init block. The user can then update this object, and save it to disk by calling its save method. Undefined fields default to None. To ensure the object can be loaded again, we suggest not assigning the object instances of user-defined types.

Function: | MultiPersistent | (key):

Creates a new MultiPersistent object. This should only be called inside an init block, and it returns a new MultiPersistent object corresponding to key.

key a key that is used to access the multipersistent data. To prevent conflicts, we suggest that the key be suffixed by a domain you own... so if you owned renpy.org, you could use the key "demo.renpy.org".

Method: | MultiPersistent.save | ():

Saves a MultiPersistent object to disk. Changes are not persisted until this method is called.

Example. Part 1 of a multi-part game:

    init:
        $ mp = MultiPersistent("demo.renpy.org")

    label start:

        # ...

        # Record the fact that the user beat part 1.

        $ mp.beat_part_1 = True
        $ mp.save()

        e "You beat part 1. See you in part 2!"

And the code for part 2:

    init:
        $ mp = MultiPersistent("demo.renpy.org")

    label start:

        if mp.beat_part_1:
             e "I see you've beaten part 1, so welcome back!"
        else:
             e "Hmm, you haven't played part 1, why not try it first?"

Data Location. The place where the data is stored varies based on the operating system being used:

- Windows: %APPDATA%/RenPy/persistent
- Mac OS X: ~/Library/RenPy/persistent
- Linux/Other: ~/.renpy/persistent

documentation index ◦ reference manual ◦ function index