Can I prevent renpy from trying to save certain 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
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Can I prevent renpy from trying to save certain objects?

#1 Post by zmook »

Okay, this one is starting to make me wonder if it's a bug. I have some python classes, Quest and Event, created to define the branching structure of my plot. These objects are intended to be *code,* not game state, and should *not* be saved in the game file. Nothing in them should change during play, and if I change their definitions (in the init python block that creates them), I want that to automatically take effect, regardless of what might be saved in some partial play through or whatever. (ie, if I add a new Event to a Quest, I want everyone who gets a new version to see the new Event. It would be stupid if their game save basically forced them to keep playing an old version of the game.)

Okay, so far so good. Now, the Event class has a property with a lambda function assigned to it to allow for custom per-event logic about when the Event can occur. This should be fine. Lambdas are not pickleable and can't be saved in a game save, but Events aren't supposed to be saved at all so this shouldn't matter.

However. Ren'py keeps trying to save one or more Events, and then bitching at me that they can't be saved because "PicklingError: Can't pickle <type 'function'>". The traceback doesn't seem to give me any information about *which* Event is trying to be saved, so I don't know if it's all of them or one in particular. I have read over my code, and I can't see anywhere where an Event instance is assigned to a variable that I think is supposed to be saved.

* Is there some way to read exactly what's in the save file, so I can maybe figure out what's in there that isn't supposed to be?
* Is there any way to declare that certain objects are *never* supposed to be saved?
* Is there documentation somewhere that lays out precisely what is and is not eligible for saving?

My understanding is that variables that are only assigned to in init blocks are *not* saved, and that variables that are local to python functions and methods are also *not* saved.

For clarity, this is some stripped down code to give an idea of what I'm doing:

Code: Select all

define all_quests=[]

init python:
    class Quest(object):
        def __init__(self):
            self.events = []

        def add_event(self, ev):
            ev.quest = self
            self.events.append(ev) 
    
    class Event(object):
        def __init__(self, condition=None):
            self.condition = condition if condition else (lambda x: True)
            
        def can_happen(self):
            return self.condition(clock)
                
                
    q = Quest()
    q.add_event(Event(condition = (lambda x: x>10)))
    q.add_event(Event(condition = (lambda x: x>20)))
    q.add_event(Event(condition = (lambda x: x==32)))
    
    all_quests.append(q)
I created a dummy project with just this code in it, and it works like I expect and saves and loads correctly. But my real game does not. There is *something* I'm apparently doing or assigning or creating that causes it to break, and I don't know what.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Can I prevent renpy from trying to save certain objects?

#2 Post by Kia »

as far as I know, variables that are defined using `default` are saved and those defined using `define` are not. I'm not sure what category the ones defined in python fall under but try `define q = Quest()` instead and see what happens.
I do use default for my quests and quest log and start the game with an empty log, the quests are appended to the log during the game and not at the start, this way I don't encounter any problem with saving.

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Can I prevent renpy from trying to save certain objects?

#3 Post by zmook »

Kia wrote: Fri Mar 12, 2021 1:04 pm as far as I know, variables that are defined using `default` are saved and those defined using `define` are not. I'm not sure what category the ones defined in python fall under but try `define q = Quest()` instead and see what happens.
That's what I understand, too, and the docs say

Code: Select all

define e = Character("Eileen")
is equivalent to:

Code: Select all

init python:
    e = Character("Eileen")
Doing it like I have it above, in `init python`, it *works* like I expect (it is not saved) when I make a new toy game with just that code in it. But in my actual game, once I start *using* the objects, somewhere somehow Renpy decides to add them to the *to be saved* list. I haven't been able to figure out where or why.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: Can I prevent renpy from trying to save certain objects?

#4 Post by zmook »

AHA. So it turns out that this:

Code: Select all

label check_scheduled_events:
    
    python:
        for quest in all_quests:
            for event in quest.events:
                 scene_queue.append(event.scene_label)
…counts as assignment to `quest` and `event` and results in renpy trying to save the last objects assigned. As it happens this would not actually be damaging to my game, except that my Events can't be pickled.

It looks like I can work around by adding

Code: Select all

 quest = event = None 
after the loops.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]