Is there a way to make saving/loading work properly with class data attributes?

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
Ocelot
Lemma-Class Veteran
Posts: 2411
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Is there a way to make saving/loading work properly with class data attributes?

#1 Post by Ocelot »

When I tried to use class attributes, I noticedm that they are not saved, nor they are restored when we start a new game.
It does make sense, since technically we are only changing field and not top-level reference, which is documented as an action which does not cause RenPy to add object to list of saved/restored objects.
I tried to assign class reference around, which is enough to trigger saving for normal variables, but it doesn't seem to work with classes.

Here is an example. If you save at the line marked as save point, completely exit the game, launch it again and load the saved game, you will see added event type disappear: it wasn't saved. Additionally, if you play to the end and the start the game again without quitting, added event type will be there from the beginning.
Note: usage of class attribute instead of instance attributes is intentional.

Code: Select all

init python:
    class Event:
        types = ["repeating", "onetime"]

        @classmethod
        def new_event_type(cls, type):
            if type not in cls.types:
                cls.types.append(type)


label start:
    '[Event.types]'
    $ Event.new_event_type("timed")
    '....'
    'Save point: [Event.types]' # Save point
    '---[Event.types]---'
    return
Is there any way to make it work with RenPy save system, or should I move on to using globals insted?
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: barsunduk