Between the manual and the reference, I haven’t been able to find much information on performing file I/O in the python side of Panda3D. Does anyone know a good place to look up information on that?
Thats what the task chains can do. Basicly you give it another thread to load things off side of the main thread giving you both IO input and output. This does have limitions tho. Aka, you can’t have two threads writing to the same file at the same time… One of them will have to wait.
If you just mean python, import threading is what you want to look at then maybe.
As you read, it’s better to just use the task chains than to use the other setup.
Okay, I’ve made a note of using task chains to do the I/O, thanks for that.
My question was a little more basic though. I’m wondering about the commands to load a file and parse it, and to save a file as well. Some kind of encoding or encryption or something so that the file is at least difficult to tamper with would be nice as well. Is this something built into Python rather than Panda, and I should be looking for it through Python docs?
Oh, I miss understood then sorry:) For panda you could use: panda3d.org/manual/index.php/C … Multifiles
For python you could use:
import bz2 f=open("test.txt", "w") f.write(bz2.compress("mypassword")) f.close() w = open("test.txt", "r") password = bz2.decompress(w.read()) w.close() print password
I like the hash idea someone said… this way passworda or usernamea are never saved in this way.
for file-i/o for non-panda-files you should have a look at the python docs http://docs.python.org/library/stdtypes.html#file-objects but other parts of the standard library could also be interesting… (like chapter 13, 14, 20 in the docs)
btw: just using bz2 for hiding passwords is not very secure…
I don’t think the hash idea would work for my purpose, my goal is to create a save file for the game that would retain information on, say, what level the player was on, story decisions they had made up to that point, etc. The reason I didn’t want it to human readable/editable is because I didn’t want people to be able to alter their saves to give themselves an advantage or something.
I’ll look through all the links people have posted to see if there is something that will work for my purpose. I’m sure it’s out there.
Hm, I don’t think I’m understanding what you mean. Do you just want to load or write a file, in a thread-safe way and possibly from the VFS? Then you can just use the file/open methods from direct.stdpy, as described here:
Or do you want to write Panda3D objects to disk? You can do so using the pickle/cPickle module (Requires Panda3D 1.7.0 or later).
From the posts above, I don’t think the OP was asking about threading at all, and in fact I wouldn’t recommend using threading or task chains unless there is a real reason to do so.
If the original question was simply about reading and writing files, Python provides a fairly complete interface for this, using the open() function and file objects, all of which is documented pretty well on python.org. It’s then completely up to you to decide how to structure the data that you read and write, but if you’re just saving a handful of values, it’s not difficult.
The next level of sophistication is to use the pickle/cPickle module; you can construct a dictionary with all of the game data you want to store, and use pickle to write it to a file. Then you can read it again later.
It seems like my question was confusing, so I’ll restate it in a more specific way.
I wish to have a “Save Game” option in my game. The save game option will write out a file to the disk that contains the following information, which could be entirely done in strings, as python objects, or whatever. The format is not that important to me so long as it can be re-read into the program when a “Load Game” option is selected, and that it isn’t easy to edit outside of the game.
The information is:
- The current level the player has reached
- Choices made at decision points, such as “Invited Guy to Pary” or “Did not go to football game” or even as simple as 1, 2, or 3.
- The pass/fail record for previous levels.
And other things of that nature.
When the “Load Game” option is selected, the player is returned to the start of the level during which they had saved, with their game history intact. If the state of the current level is not saved, and progress into that level is lost, that’s fine. The levels are only intended to be about 15 minutes long anyway, so the loss would not be grievous.
easiest (i think) would be to pickle/unpickle (in binary mode) the object(s) containing the status of the game
I think this has been stated but just take your variables and write them to a file.
just google “file I/O in python” and pick a tutorial
then when the user loads the game have it read the data out of the file. For encrypting the file so that users cant edit it just make an encrypt and decrypt function, or maybe see if there is one online or in python that can be used
If you want something more human readable may I suggest pyyaml? It also writes out python classes.
If you are looking for forward/backward compatibility there is google’s protocol buffers as well.
Thanks for all the solutions everyone, I’ll start going through them and select one that works for me. I appreciate all the help on this one.