Why does Ren'Py try to save screen variables?

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
martingerdes
Regular
Posts: 26
Joined: Sun Oct 07, 2018 5:14 am
Contact:

Why does Ren'Py try to save screen variables?

#1 Post by martingerdes »

I am using Ren'Py 7.3.5
Before I go and create a test game to make the bug(?) reproducible for others, I'll first just throw the general problem out here, and maybe somebody can tell me why it is the way it is, and maybe even offer a workaround?

Pseudocode:

Code: Select all

python init:
    class Obj(object):
    def __init__(self,name):
        self.name=name
        #probably needs more stuff here so it can't be pickled

    a_hash={"key":Obj("an example")}

screen testscreen():
    $ obj=a_hash["key"]
    vbox:
        text obj.name

label start:
    "before"
    call screen testscreen
    "after"
So assuming that instances of Obj, can't be pickled, what happens is the following:
So long as the game does not show testscreen, the game can be saved and loaded normally. That holds even after testscreen was shown (and left again).
However, if you try to save while testscreen is visible, you get PicklingError: Can't pickle ...: it's not the same object as ...
I haven't tested the exact pseudocode above, but if I comment out the lines assigning and then using the equivalent of <obj> in my game code, saving works.
-> a_hash does not get saved, because the variable gets set during initialization and does not change afterwards (even though the hash content might). Thus a_hash does not need to work with pickle.

So the general question here is, why does the pickling code even look at the screen variable <obj> in the first place? (And it must be a screen variable - if I was accidently writing to store, saving wouldn't work after leaving the screen.)

After all, when I load the game, it starts at the beginning of the current interaction, which is "call screen testscreen", so any screen variables will be calculated anew anyway!

Does anybody know why Ren'Py tries to save <obj>?
Or should the above code actually work and saving be possible while testscreen is shown? If so, I'll make a real testcase out of it.

My current plan is to change my code to do

Code: Select all

    $ key="key"
    vbox:
        text a_hash[key].name
Since key is a string, it can be pickled, and everything will work. However, there are about 20 places where I'd have to replace obj with a_hash[key], and it just doesn't seem right that the Ren'Py save system is keeping me from using screen variables in this way...
Last edited by martingerdes on Sun Jan 10, 2021 10:13 am, edited 1 time in total.

martingerdes
Regular
Posts: 26
Joined: Sun Oct 07, 2018 5:14 am
Contact:

Re: Why does Ren'Py try to save screen variables?

#2 Post by martingerdes »

I have now changed my code to hold the key in the variable and look up the dict value each time I need it.
As expected, saving and loading work that way.
I still feel like I have coded around a bug in Ren'Py here.

Post Reply

Who is online

Users browsing this forum: Google [Bot]