Given a user file–custom key-bindings, player profiles, or save-games, for example–where should I be saving them, and how?
First, should I be saving such files into Multifiles, or just as ordinary text/binary files? (At the least, I imagine that Multifiles offer a little bit of extra security via not being obviously text files.)
Second, where should I be putting them? I’ve discovered that under Windows 7, files seem by default to be saved into “user/AppData/Local/Panda3D/start/”; I haven’t gotten around to checking where they end up in Windows XP or Ubuntu. Should I be saving such files to this folder (presumably in a sub-folder that I create for each game), or should I be changing this somehow?
As far as windows -In the olden days it was expected that if a game is installed into C:\games\game_title\ then all the files of that game (be it saves, mads or screenshots) where saved there and only there. Lately it became popular to save files in …???\My Documents\My Games\game_title\ - I, for one, don’t like that - but I can understand why devs may choose to do so - to give each system user a new set of saves, mods, keybinds etc.
Getting the path to “My Documents\My Games” ain’t all that simple. There’s os.path.expanduser(’~’), but who says that my “my documents” folder is actually called “My Documents”? E.g. on my system it’s “Moje dokumenty” on someone elses it could be “Meine Dokumente” or “Mis Documentos”… To be honest I no not how to do it without win32com or ctypes (but don’t ask me how to do it with win32com or ctypes - I only know/think it can be done )
The Panda3D start folder is in such a obscure and hidden place that I wouldn’t save any files there at all (C:\Documents and Settings<your name>\Local Settings\Application Data\Panda3D\start\ on English winXP, in my case it’s ‘C:\Documents and Settings<user name>\Ustawienia lokalne\Dane aplikacji\Panda3D\start’).
For these reasons I suggest to write all files where the game was installed (where the exe/p3d file is).
from direct.showbase.AppRunnerGlobal import appRunner
I can’t say where Linux and Mac people prefer to keep their files.
I wouldn’t encode save files, if players want to cheat - let them, but if you want to make it a bit harder for them - take a look at base64 or rot13… or You could also make something really silly - for my game I made a random(-ish) string generator and the actual data saved into a save file was the number of lines in the file, anything else was just random noise
I would go for the %APPDATA%, which you can get with
If you want to prevent cheating I would use binary representations (struct.pack) with some random stuff, like stored_health = health * health * 123 + 456 and additionally save a hash of the file anywhere else. But I guess for a simple game that’s not necessary.
Regarding saving location, I performed a few quick tests on my side (using Ubuntu and Windows XP; for some reason Python doesn’t seem to be installed on the Windows 7 machine that I have access to):
It seems that the “APPDATA” variable isn’t ubiquitous; in particular, “getenv(‘APPDATA’)” seems to return “None” under Ubuntu. It does produce a useful directory under XP, however.[/]
Trying “path.expanduser(’~’)” (using tilde rather than dash, as originally suggested), I get the user’s home directory under Ubuntu (“home//”), and “C:\Documents and Settings<username>” under XP.[/]
But does it have to be called that? Surely, as long as I get a consistent result from “expanduser”, that should suffice: even if the result varies between machines, is it not enough that it’s consistent within a given machine? Or does “expanduser” only give an English result? (The computers that I have access to here use English.)
Hmm… I hesitate there because I seem to vaguely recall that newer versions of Windows restrict rights to writing to such locations–am I remembering correctly? I do believe that Ubuntu, at least, restricts writing outside of the user’s home directory. (These things can be done by gaining the appropriate access, but I’m not sure that it’s a good idea to circumvent such security measures; even outside of security issues, users might expect to find files in unrestricted places, for one.)
That does look useful, thank you.
As to security, I’m not really thinking of going anywhere near as far as your suggestions! ^^; I was really just thinking of not making the files obviously editable: the difference, by analogy, between leaving one’s door open and closing it behind you: even if the door’s unlocked, it’s at least not invitingly open. However, Wezu, I feel that you make a good point about letting people cheat if they want to.
expanduser gives a valid path on non-english windows, it’s just not complete (I would expect that ‘my documents’ would be a ‘home’ dir, not its parent dir). I’m just saying that if you append path.expanduser(’~’) with ‘my documents’ then that path may not be the directory the user uses as ‘my documents’… but if it works on other os you want to target, and what you say about write restrictions is true (and I think it is), then it’s not a bad place to write saves to.
Ah, right–I managed to miss the lack of “My Documents” at the end of the “home directory”! I blame lack of sleep! ^^;
However, I disagree with the idea that “My Documents” would be the “home” directory: are “My Pictures” and “My Music”–which should be within the same directory as “My Documents”–not part of the user’s “home space”, so to speak?
Additionally, I don’t know how others use it, but I tend to use “My Documents” specifically for storage of… well… documents: text-based documents (text files, pdfs, LibreOffice files, etc.). I don’t think that I’d expect to find game files in “My Documents”.
To my mind the parent directory of “My Documents” would indeed be the “home” directory, not “My Documents” itself.