Ren'Py Resetting an object instance after loading a saved game.

Alfius

Engaged Member
Modder
Sep 30, 2017
2,223
4,606
Hi, Still complete noob in Renpy/Python trying to help out someone:

So I have an Object
Code:
    class Person:
        def __init__ (self, fname, lname, age):
            self.fname = fname                      # First Name for Characters
            self.lname = lname                       # Last Name for Characters
            self.age = age
I instantiated an instance
Code:
            default  TClaire = Person("Claire","White,27)
So everything works perfectly

Then I decided to add a portrait to the object.

Code:
    class Person:
        def __init__ (self, fname, lname, age,img):
             self.fname = fname                      # First Name for Characters
             self.lname = lname                       # Last Name for Characters
              self.age = age                                   # age of character
            self.img = img                                   #Portrait of character
I updated the code where I instantiated an instance
Code:
            default  TClaire = Person("Claire","White,27,"Clare01.png")
So if I start a new game everything works fine.
But if I load a saved game. The TClaire object does not have a Portrait property

Is there a way after a load to force the object to be recreated?

(I don't really care about the saved values. The object consist of a mix of static values that will never change and other values that are updated ingame from other existing variables. I actually do not even want to save the object instances, but Renpy seems to do it automatically.)

Any hep will be appreciated.
 
Last edited:

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,574
2,202
You might want to swap:

default TClaire = Person("Claire","White,27,"Clare01.png")
for
define TClaire = Person("Claire","White,27,"Clare01.png")

Though that largely depends on whether you are going to change any of the properties as the game goes on.

Basically define create variables that aren't saved. The end result is they are recreated each time the game runs. As your example stands right now, it looks like define would work for you, as you say the values are static.

Alternatively, if you haven't released the game in any way... you could just delete your existing saved games and start again using your new definitions. If you swap to using define you would have to do this anyway, since the values you set up using default are going to override anything as soon as you load a save file.

Finally, there is , a label called immediately after a saved game is loaded. This one is more tricky because it it is called EVERY time a saved game is loaded... so whatever you do there mustn't break anything for "normal" save files. There is a way (I can't recall right now) to check if a variable exists... it could be that you could check to see if the portrait attribute exists and set it if it's missing. I'm thinking a check for if {attr} == None: probably will generate an error, but it might be worth a try. More likely though, you're going to need to do an hasattr check (which is the solution I can't recall).

You could always go the "sledge hammer to crack a nut" route and set the value every time a saved game is loaded, whether it exists already or not. But that is kind of inelegant.
 

Alfius

Engaged Member
Modder
Sep 30, 2017
2,223
4,606
You might want to swap:

default TClaire = Person("Claire","White,27,"Clare01.png")
for
define TClaire = Person("Claire","White,27,"Clare01.png")

Though that largely depends on whether you are going to change any of the properties as the game goes on.

Basically define create variables that aren't saved. The end result is they are recreated each time the game runs. As your example stands right now, it looks like define would work for you, as you say the values are static.

Alternatively, if you haven't released the game in any way... you could just delete your existing saved games and start again using your new definitions. If you swap to using define you would have to do this anyway, since the values you set up using default are going to override anything as soon as you load a save file.

Finally, there is , a label called immediately after a saved game is loaded. This one is more tricky because it it is called EVERY time a saved game is loaded... so whatever you do there mustn't break anything for "normal" save files. There is a way (I can't recall right now) to check if a variable exists... it could be that you could check to see if the portrait attribute exists and set it if it's missing. I'm thinking a check for if {attr} == None: probably will generate an error, but it might be worth a try. More likely though, you're going to need to do an hasattr check (which is the solution I can't recall).

You could always go the "sledge hammer to crack a nut" route and set the value every time a saved game is loaded, whether it exists already or not. But that is kind of inelegant.
Thanks for the help...

I tried using define instead of default, but since the original object variable TClaire already exists.
It seems to still load the old object from the save, instead of just creating a new one.
(It's an existing game already out there, so the saves already exists)

In terms of
it could be that you could check to see if the portrait attribute exists and set it if it's missing
This would be my preferred method: How to I add an attribute to a object after it's already created?
If I can do this, that would solve my problem.

Will look at the "label after_load" option.
 
Last edited:

Alfius

Engaged Member
Modder
Sep 30, 2017
2,223
4,606
Thanks for the help...

I tried using define instead of default, but since the orgina object variable TClaire already exists.
It seems to still load the old object from the save, instead of just creating a new one.

In terms of
This would be my preferred method: How to I add an attribute to a object after it's already created?
If I can do this, that would solve my problem.

Will look at the "label after_load" option.
I ultimately created a whole new set of objects with different names using define...So far so good.

Thanks for the help!
 
  • Like
Reactions: 79flavors

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,299
15,166
But if I load a saved game. The TClaire object does not have a Portrait property
Because the property wasn't in the saved object. And like a load restore an object like it was when the save happened...


Is there a way after a load to force the object to be recreated?
You don't need it to be recreated, just to be updated :
Code:
label after_load:
    if hasattr( TClaire, "img" ) is False:
       TClaire.img = "Clare01.png"