Home Technical Talk

python game level serialization/persistence doubt

polycounter lvl 17
Offline / Send Message
jogshy polycounter lvl 17
Hiya,

i'm finishing a game engine and I would like to hear from experienced game level designers about a serialization/persistence problem.

imagine I have a class called "Pet" in python:
    class Pet:
       def EmitSound(self):
          soundDrv.play("genericPetSound.wav")
then I inherit two classes... Dog and Rhino
    //Dog.py file
    class Dog(Pet):
       def EmitSound(self):
          soundDrv.play("woof.wav")

    //Rhino.py file
    class Rhino(Pet):
       def EmitSound(self):
          soundDrv.play("rhino.wav")

Then I add some Dog and Rhino instances to a scene:
    import Dog
    import Rhino

    class Scene:
       def __init__(self):
          //add a new instance of a Dog ( called dog01) and two Rhinos
          // ( called rhino01 and rhino02 ) to the animals scene's 
          //dictionary
          self.animals = { "dog01":Dog(), "rhino01":Rhino(), "rhino02":Rhino() }

          //add other properties like the sky's color
          self.skyColor = { "r":100, "g":100, "b":100 }

       def __getstate__(self):
          return {"animals":self.animals, "skyColor":self.skyColor }

       def __setstate__(self,state):
          if "animals" not in state:
             self.animals = {}
          else:
             self.animals = state["animals"];

          if "skyColor" not in state:
             self.skyColor = { "r":100, "g":100, "b":100 }
          else:
             self.skyColor = state["skyColor"]

then I pickle the scene:
    import pickle
    import Scene

    scn = Scene()
    pickle.dump(scn,"data.bin");

Then I decide that rhinos are no longer a good pet so I delete the rhino.py and the rhino class... then I unpickle the scene...
    scn = pickle.load("data.bin"); //baaang! Will fire an exception because the Rhino.py is no longer available!
so I bet an exception will occur because the rhino class is no longer available... how can I deal with this? I know I can use getstate/setstate to solve versioning problems... but in this case the complete class disapeared so the animals dictionary could not be deserialized well... Perhaps using shelves instead of a dictionary to store the animals? Basically I want the dictionary to be deserialized with the Dog instances and NULLs where rhino instances appear.

This is very important to solve some serialization versioning problems...
How UT3 and Crysis deal with this? What happens if you save a scene and then you delete a actor/entity script?

thanks.

Replies

  • Daaark
    Options
    Offline / Send Message
    Daaark polycounter lvl 17
    You are better off asking on GameDev.net as it's mostly artists here. With Python and Pet in the same sentence, they are probably on the wrong page.

    LuckyLarge.jpg

    Lots of Python guys over there.
  • jogshy
    Options
    Offline / Send Message
    jogshy polycounter lvl 17
    With Python and Pet in the same sentence
    Heheheheh :P
    Daaark wrote: »
    You are better off asking on GameDev.net as it's mostly artists here.
    I did already... and it seems nobody know how to deal with that :(
    I'm pretty sure here are many good level designers though. I really don't think to save a scene, then you remove an actor/entity and you try to load a scene is a very rare case... There must be a solution for this, i'm quite sure.
Sign In or Register to comment.