Author Topic: [Python] Saving lists of objects with lists of objects with...  (Read 7223 times)

Hundertzehn

  • Newcomer
  • Posts: 12
  • Karma: +0/-0
    • View Profile
[Python] Saving lists of objects with lists of objects with...
« 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.


koiwai

  • Rogueliker
  • ***
  • Posts: 99
  • Karma: +0/-0
    • View Profile
Re: [Python] Saving lists of objects with lists of objects with...
« Reply #1 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?

Hundertzehn

  • Newcomer
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: [Python] Saving lists of objects with lists of objects with...
« Reply #2 on: November 19, 2014, 12:49:59 PM »
As soon as you start doing it right, it works!  ;)

mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: [Python] Saving lists of objects with lists of objects with...
« Reply #3 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.