On Mon, 31 Jul 2006 14:35:39 -1000, Simon Forman <[EMAIL PROTECTED]> wrote:
> 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 > It's mostly a combination of things (I hope you can follow my logic). First, to use "good programming practice", I want to implement a try/except block for opening the pickle file. But I can't figure out if this block should be included outside the classes, included in just the base class, or if the base and subclasses need it; I'm leaning to putting outside the classes as a global method. When pickling a class instance, is the pickle statement placed within the class (say, at the end of the class) or is it where the instance is created, such as a test() method? Do I even need to bother with having the try/except block and pickle statements in the character generation module or should I have a separate module that does the file I/O, such that it opens the new file, calls the character module and makes a new instance, then pickles it? Plus, to modify data in a class, do I have to unpickle the whole thing first or is there a way to modify the data while it's pickled? Actually, I think I can answer that last question: a character instance, having been created, will stay resident in memory until the player quits, character dies, or otherwise is no longer needed. At that point the character instance should be pickled (if necessary) to disk; pickle shouldn't be used while data modification is required and the class instance is "active", correct? I also remember reading on a thread here that pickle sometimes has issues when used with classes, which makes me hesitant to pickle an entire class instance. That's why I thought XML may be safer/better. -- Python-based online RPG in development based on the Colonial Marines from "Aliens": http://cmrpg.sourceforge.net Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ -- http://mail.python.org/mailman/listinfo/python-list