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

Re: [pygame] Pickling?



Hello,

Thank you for your help. This morning, I have spent hours trying to
figure out how I could do a workaround, and I succeeded partially;
I am able to load/save my game now, but some information is lost (the
fall speed was implemented using an iterator).

Bob Ippolito schreef op maandag 24 februari om 14:52:35 +0000:
> 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.

For a Rect, this is actually so basic that I think the default Rect
should be able to do the task described here. A rect can be recreated
using a 4-integer-sequence, with the __getstate__ and __setstate__.
I have now implemented your description using a .setimage() function,
which each of my sprites has and which sets the .image attribute (although
setting a property was more difficult, it required a workaround with a
dict).

> 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.

I see. I also see that this question belonged on python-list rather than
on pygame-users :). Maybe a helper in itertools would be useful, with
the generator and an index number, although it wouldn't be reliable...

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org