Save files

How do you create a save file for a game, that is not a separate file? Specifically I am using code::blocks and sfml 2.1 to make a game and it saves to a text file at the moment. My problem is that it is very easy to modify the text file, and it is annoying to have to copy and paste several files if you want to use a copy of the game. I have a feeling that it may be to do with resource files, but I'm not exactly sure how to get these to work or whether you can modify them dynamically. Any help would be greatly appreciated.
I'm not directly answering your question here... so my apologies. I'm just throwing in my 2 cents:

How do you create a save file for a game, that is not a separate file?


Are you talking about a save file? Or a data file?

IE... a user's saved game vs. something like level data.

If you mean the former, you can't really. Your options are an external file, modifying the executable file, or doing something "hidden" like making registry entries. (Note: 2 of those 3 approaches are absolutely horrible and you should not do them -- just use a file)

If you mean the latter (a data file) then resources are an option... but a bad one. External files allow for more flexibility and do not require you to recompile the program whenever you change them. Plus, the entire exe is loaded into memory before it's run and if you have a ton of resources that don't all need to be loaded immediately, it's kind of wasteful.

As for how to actually make resources with C::B... I couldn't tell you. Sorry. =(

Also note that the interface for accessing resources is typically not portable. So if you try to make an .rc file and access the resources with WinAPI function... then your program becomes Windows-only.

My problem is that it is very easy to modify the text file,


That's a good thing. It's kind of the whole point of using text files. They're human readable so you can go in and modify them without having to mess with code.

If you are worried about people cheating by going in and changing the level data or whatever... you can throw in some basic encryption. But who cares if people want to cheat? Let them.

If that makes it more fun for them, that's good, right? (Unless it's like some online thing where it screws other people... but I'm assuming that's not the case here)

and it is annoying to have to copy and paste several files if you want to use a copy of the game.


Put it all in a .zip or something. You should probably be doing that anyway even if it's just the exe. zips are a fraction of the size so they're more reasonable to distribute.

Anyone will know what to do with a zipped game if you give it to them: unzip it and run the exe file. Doesn't get much simpler.

I have a feeling that it may be to do with resource files, but I'm not exactly sure [..] whether you can modify them dynamically.


You can't. Resources get compiled into the executable binary (ie: the exe file). Modifying them would result in modifying the exe itself, which is generally a very bad idea.
Okay thanks, you've pretty much answered everything I could possibly have been asking there. I waas thinking of both save and data files. I was basically trying to make my game more portable by making it a standalone exe. But I guess I should just stick with a sprites folder and a .txt save file. I've actaully just done a lot of work on encryption for something completely different so I could probably apply that.
But I guess I should just stick with a sprites folder and a .txt save file. I've actaully just done a lot of work on encryption for something completely different so I could probably apply that.

That you store the save file as text doesn't mean the file name has to end with .txt. You could use whatever file extension you want. Using .txt makes it obvious that it's a text file that can be read and edited in a text editor. If you use something like .save or .savegame it becomes clear the file is a save file but without giving any clue about the format.
That's a really good idea. I had forgotten that changing the extension of something like that doesn't actually change the data. Thanks.
Why not just use binary files? Your entire save could be a single struct that you can put/get in a single operation.
Why not just use binary files? Your entire save could be a single struct that you can put/get in a single operation.


I don't recommend writing full structs to a binary file. I personally don't even like to write anything more than 1 byte at a time.
Topic archived. No new replies allowed.