On Jun 1, 2:51 am, dodgyville <[EMAIL PROTECTED]> wrote:
> I'm interested in how people and saving their game states. Ideally I'd
> love to just pickle the entire "game" object. In pygame this doesn't
> work because of the image resources, but maybe it's different in
> pyglet?

The short answer: read up on the pickle protocol, including
__reduce__, __getstate__, and __setstate__ methods.

The long answer: I'm using ZODB to manage game object persistence for
an RPG.  This would be overkill for an arcade game like Asteroids,
which has few objects to keep track of at any one time, but with
potentially thousands of objects to keep track of, something like ZODB
can't be beat.  There's some significant learning curve to get past at
first, and a minimal number of hoops to jump through (significantly
fewer than you would have with some sort of ORM DB wrapper system).

The benefits: once you've loaded your database and prepared your game
object classes, all you have to do is instantiate game objects and
ensure that they're connected to the database root object. ZODB
automatically loads objects into memory as you access them, and
offloads them to disk if they haven't been used in some time.  Saving
state for all the objects in the db is a matter of a single call:
transaction.commit()

Most of the hoop-jumping involve making sure that ZODB is advised of
changes to object state, and it provides a Persistent superclass and a
number of solid data structures to ensure this.  All pickle-related
caveats also apply, as pickle lies deep at the heart of the
implementation.

As far as pickling image objects goes, I'd recommend customizing your
game objects to store the resource path (pyglet.resource is your
friend here), and keep the image itself in a volatile attribute that
can be reconstituted at unpickling time.  With ZODB, volatile
attributes can be prefixed with _v_, I then hide the volatile behind a
class property, using the getter method to restore the volatile if it
isn't there.  Without the ZODB, you'll have to roll your own
solution.  Read up on the pickle protocol and the usage of __reduce__,
__getstate__, and __setstate__.

Good luck!

--David Eyk
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to