[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Pickling?



On Monday, Feb 24, 2003, at 05:18 America/New_York, Scott Russell wrote:

I thought there used to be a UserRect to make rects more flexible, but
it seems to be missing from the current pygame docs on pygame.org...

Did I imagine it, or was it removed?

On Sun, 2003-02-23 at 14:42, Gerrit Holl wrote:
Hi,

I am trying to save a game. Is pickling Sprites supposed to work?
When running my game with python -i and checking stuff afterwards,
I get:
What you need to do is either (a) create and user a Pickler that knows how to pickle Surface (and possibly Rect) objects and/or (b) create a pickler that knows how to pickle some subclass of Sprite where the Sprite keeps track of how the Surface was created (as in, from the image filename or some set of drawing instructions) so it can recreate the Surface without creating a gigantic pickle. If you combine the two, you've got a pretty generic solution, but you're probably going to want to use PIL to convert raw surface data to PNG format or something of the like and imgstring() it before putting a raw surface into a pickle that you don't have create instructions for.

Only basic python stuff can be pickled using a non-custom pickle (classes, dicts, strings, etc..). You can't pickle things like files or sockets unless you give the pickler or class that's being pickled instructions on how to recreate those objects from pickleable types (i.e. for a file you could give a path and a seek). Thats how pickling classes work, you don't pickle the methods, just string references to the fully qualified method names, and you hope that the code is still there and hasn't changed enough to break pickling (there's ways around this, but it requires you to add additional cruft to your classes.. a version number and methods to upgrade an instance of one version of your class to another. Twisted uses this method and it works rather well). Extension types (i.e. Surface, Rect, etc.) are generally not pickleable unless the author puts forth the extra effort and adds explicit pickling support to the extension module's code.

-bob

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org