[SOLVED] How to save and load a list of objects?

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
Kinjo
Veteran
Posts: 218
Joined: Mon Sep 19, 2011 6:48 pm
Completed: When the Seacats Cry
Projects: Detective Butler
Organization: Goldbar Games
Tumblr: kinjo-goldbar
Deviantart: Kinjo-Goldbar
Github: GoldbarGames
Skype: Kinjo Goldbar
itch: goldbargames
Location: /seacats/
Contact:

[SOLVED] How to save and load a list of objects?

#1 Post by Kinjo » Sat Sep 01, 2018 4:00 am

I created a navigation system for a certain game using a custom Python class. The Navigation class is initialized at the beginning of the game by an "init python" block. It contains a member variable "previousRooms" which is a list of strings containing the names of the rooms the player has visited. This list naturally starts out empty, and with each visit to a room, a new string is appended.

During the navigation, the player can go back to the previous room by clicking a "back" button, and this works fine on its own. But when the player saves the game and tries to load, there are some big problems.

When the player saves and loads into a new file (without closing the game), this list carries over into the loaded save. So if my list is A, B, C, D and then I load a game that starts at F , instead of going back to E it goes back to D, C, B, and A in that order. So not only does it fail to load the previous room from the newly loaded file, but it retains the list of rooms from the old file. Loading into the new file after closing and re-opening the game simply causes the game to crash since the list has not been filled with any data.

EDIT: Solved it on my own. Apparently the navigation instance needs to be created after the init block. Feel free to close this.

User avatar
Remix
Eileen-Class Veteran
Posts: 1311
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: How to save and load a list of objects?

#2 Post by Remix » Sat Sep 01, 2018 5:57 am

My favourite approach when just one or two attributes of a class are wanting to be stored is to directly address the store within getattr and setattr...

Code: Select all

## Pseudo code

init python:

    import renpy.store as ren_store

    # if instantiating before 0, you'd need to alias the store
    # best to just do that anyway

    class MyClass(object):
        """ Handle stuff """

        def __init__(self, **kwargs):
            
            self.var = kwargs.get('var', 0)
        
        def __getattr__(self, name):
            
            if name == 'var':
                # Use store version only
                return ren_store.my_class_var or 0

            # we *should* use __getattr__ here, alter if you get any recursion
            # I found getattr messes with @property methods though
            return super(MyClass, self).__getattribute__( name ) 

        def __setattr__(self, name, value):

            if name == 'var':
                # set to the store
                ren_store.my_class_var = int( value )
     
            else:
                # Other values (not held in the store)
                super(MyClass, self).__setattr__( name, value )


    my_class = MyClass( var = 1 )
You could, alternatively, instantiate your class through using default var = MyClass( ... ) and it *should* save or even write your class as a direct extension of the renpy store object ... class MyClass( renpy.store ):
Just play a bit and see which works best for you.
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

Post Reply

Who is online

Users browsing this forum: Google [Bot], Hiddenwall