crystalattice wrote: > I'm creating an RPG for experience and practice. I've finished a > character creation module and I'm trying to figure out how to get the > file I/O to work. > > I've read through the python newsgroup and it appears that shelve > probably isn't the best option for various reasons. This lead me to > try messing w/ pickle, but I can't figure out how to use it with > classes. I've found many examples of using pickle w/ non-OOP code but > nothing that shows how to use it w/ classes, subclasses, etc. I've > read the documentation but it doesn't explain it well enough for me. > > Then I started thinking perhaps I'm going about it wrong. My current > thought is to save an instance of each character so all the info (name, > skills, hit points, etc.) is stored in one place. But looking at > OpenRPG made me think that perhaps using XML to store the information > would be better. Each character could have a separate XML file, though > I don't know how it would work if many NPC's are required. > > I guess my question is, what are the benifits of getting pickle to work > w/ my classes vs. converting all the character data into XML and just > writing that to a file? Since most of the data would need to be > modified often, e.g. hit points, is one storage format better than the > other? Can you even modify data if it's been pickled w/o having to > unpickle it, change the data, then repickle it?
Um, there's nothing tricky to using pickle with classes: |>> import pickle |>> class foo: pass |>> f = foo() |>> pstr = pickle.dumps(f) |>> pstr '(i__main__\nfoo\np0\n(dp1\nb.' |>> newf = pickle.loads(pstr) |>> newf <__main__.foo instance at 0xb664690c> Pickle is simple and should work "out-of-the-box". I wouldn't mess with XML until I was sure I needed it for something. What kind of trouble were you having with pickle? Peace, ~Simon -- http://mail.python.org/mailman/listinfo/python-list