[Tutor] my text adventure, saving and restoring

John Fouhy john at fouhy.net
Mon Dec 5 02:41:52 CET 2005


On 05/12/05, david <din22 at earthlink.net> wrote:
> when i restore from the pickle i can see my exits and descriptions are still
> there.
>     def save(self):
>         f = open('savefile', 'w')
>         pickle.dump(world,f)
>         pickle.dump(rooms,f)
>         for i in rooms:
>             pickle.dump(i,f)
>         f.close()

Hi David,

You appear to be saving your information multiple times.

'world' is a dictionary with Room instances as its values.  Those same
Room instances occur in 'rooms'.  So, when you run your save method,
python does this:
 1. Write information to the pickle file telling python to construct a
dictionary.  This includes telling python how to construct the Room
instances.
 2. Write information to the pickle file telling python to construct a
list.  This includes telling python how to construct the Room
instances.
 3. For each Room instance, write information to the pickle file
telling python how to construct it.

Now, when you unpickle the information, python does this:
 1. Build a dictionary to be the world.  Build all the Rooms (because
the Rooms were the values of this dictionary).
 2. Build a list of Rooms.  Build all the Rooms (because the Rooms
were the contents of this list).
 3. Build all the Rooms again, and do nothing with them.

Initially, your Room isntances in world were the same as the Rooms in
rooms.  But after unpickling, python builds the Rooms separately for
each data structure, and so they end up different.

Let me try some ASCII art:
[view this in a monospaced font, eg, by cutting-and-pasting to Notepad]

Before:

world: (0,0) |-> Room0; (0,1) |-> Room1; (1,1) |-> Room2
                  /------/        /-------/      /--/
                 /               /              /
             /------\        /------\        /------\
	     | Room |	     | Room |	     | Room |
	     |object|	     |object|	     |object|
	     \------/	     \------/	     \------/
           /----/       /-------/        /------/
rooms: [ RoomA,       RoomB,           RoomC ]

After:

world: (0,0) |-> Room0; (0,1) |-> Room1; (1,1) |-> Room2
                  /------/        /-------/      /--/
                 /               /              /
             /------\        /------\        /------\
	     | Room |	     | Room |	     | Room |
	     |object|	     |object|	     |object|
	     \------/	     \------/	     \------/

rooms: [ RoomA,       RoomB,           RoomC ]
           \----\       \-------\        \------\
             /------\        /------\        /------\
	     | Room |	     | Room |	     | Room |
	     |object|	     |object|	     |object|
	     \------/	     \------/	     \------/

You could make your save() method a lot simpler; something like this:

    def save(self):
        f = open('savefile', 'w')
        pickle.dump(world,f)
        f.close()

This is all you need, because 'world' contains all the information
about your world.  Then, when you load the data back, you will need to
figure out some way of building your 'rooms' data structure from
'world'.

Does this help?

--
John.


More information about the Tutor mailing list