Saving & And Loading Custom Defined classes

TearStar

Developer of Lesbian/Futa Games
Game Developer
Mar 12, 2018
511
1,065
Code:
class Quest(object):
        char = ""
        log = ""
        i = 0
        leng = 1
        state = ["", "", "", "" ,""]
        var1 = 0
        var2 = 0
        var3 = 0
        var1_name = ""
        var2_name = ""
        var3_name = ""
        lab_name = ""
        loc = ""
        day_time = ""
        completed = False
        def __init__(self, vname1, vname2,vname3, vval1,vval2,vval3,lname,char,log, loc, dait):
            self.var1_name = vname1
            self.var2_name = vname2
            self.var3_name = vname3
            self.var1 = vval1
            self.var2 = vval2
            self.var3 = vval3
            self.char = char
            self.log = log
            self.state = lname
            self.loc = loc
            self.day_time = dait
            object.__init__(self)
I try to store this classes with associated values to it.

I found some tips along the internet that told me but none of them explained to me clearly and I just ran a circle with problem two minutes ago. (inheriting object class was one of the tips)

Basically I have 8 variables that are used associated with a Quest (all quest are predefined instances, only assinged to one variable e.g. mainQuestOpen = quest_01 where quest_01 is predefined, mainQuestOpen is the one I use).

What did I try:
  • PicklingError: I set up
    define config.use_cpickle = False
    define config.save_dump = True
    These two.. That solved the problem for a while
  • Assingned values in start label
    Got me AttributeError: RevertableObject has no attribute 'char'
  • Inherited the object class
No, I don't want to use variables since you see quest class has serious ammount of variables, times the quest I have I would have 7 million variables...

Any other suggestions I'm open.
 

f95zoneuser463

Member
Game Developer
Aug 14, 2017
219
1,017
Not sure if I understand the problem correctly. So the problem is that this class isn't stored when saving a game?
Just a wild guess ... I'm really not good at scripting.
Code:
class Quest(renpy.store.object):
 
  • Like
Reactions: TearStar

TearStar

Developer of Lesbian/Futa Games
Game Developer
Mar 12, 2018
511
1,065
Okay, I'm gonna lose it xD

I saw this inheritence once: renpy.object.object was named... Didn't understand that.

Thank you, I might not kill myself now...
 

Rich

Old Fart
Modder
Respected User
Donor
Game Developer
Jun 25, 2017
2,469
6,936
You don't necessarily have to inherit from renpy.store.object - inheriting from just "object" works OK, but part of what's important is how you declare the instance of the variable. You've created a class named Quest, then you have to create an instance of it:

Code:
default myCoolQuest = Quest(list of variable values)
What this does is cause Renpy to create an instance of your class and assign it to the name "myCoolQuest". Because "myCoolQuest" is created with the "default" Renpy command, it will be included among the values that are stored when your game is pickled. (modifying the config parameters should not be necessary).

However, the assignments that you're making above the "def __init__" line are assignments to the class itself, not to an instance of the class - those values are not going to get pickled. Only the values that you're assigning to "self.something" are going to get pickled (i.e. saved and restored). If you intend those values to be unique for each instance, you should move those assignments inside the __init__ method (prefaced with "self.")

Finally, "normal" Python would have you call the base constructor BEFORE you do your own construction, so technically the "object.__init__(self)" should be the first line in the __init__ method. Given that you're deriving from "object" it really doesn't matter, but if you set up a class hierarchy, you usually want the base class initialized before you initialized the derived class.
 

TearStar

Developer of Lesbian/Futa Games
Game Developer
Mar 12, 2018
511
1,065
That was the problem, yes. But renpy.store.Object solved it and earlier I left the idea of predefined quests. Instead I moved to a JAVA type solution: Factory. I know have a questFactory so during the game I'll create the quest through factory and then save and load works because it's not using any instances.