Temple of The Roguelike Forums

Development => Programming => Topic started by: Hundertzehn on November 18, 2014, 03:55:33 PM

Title: [Python] Saving lists of objects with lists of objects with...
Post by: Hundertzehn on November 18, 2014, 03:55:33 PM
I am at the stage where I finally want to save the current game on disk.
I have an instance of a map object. In it, there is a list of instances of the tile object. The tile object has a list of instances of the thing object, one list entry for every thing on that tile. Things can have inventories, thus more lists with more thing instances.

A few loops and "pickle" could solve this problem, but I wonder if there is a more elegant solution.

Title: Re: [Python] Saving lists of objects with lists of objects with...
Post by: koiwai on November 18, 2014, 04:43:58 PM
https://docs.python.org/2/library/pickle.html#what-can-be-pickled-and-unpickled

The documentation says that you can pickle lists, tuples, etc as long as they contain only "picklable" objects. So, I guess, you should try to pickle the entire thing. Unless there are some bad objects, the pickle module should recursively serialize it all. Did you try to do it?
Title: Re: [Python] Saving lists of objects with lists of objects with...
Post by: Hundertzehn on November 19, 2014, 12:49:59 PM
As soon as you start doing it right, it works!  ;)
Title: Re: [Python] Saving lists of objects with lists of objects with...
Post by: mushroom patch on November 19, 2014, 03:32:00 PM
I always thought pickle couldn't handle recursive nested lists or other data structures with cycles of pointers, but it looks like it actually works. Huh.