0
\$\begingroup\$

So, I am looking into game development, and I just learned how save files work and how to create and implement them. Here is the code that I was using on Dev C++:

// basic file operations
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main () {
    string line;
    ifstream saveFiles ("save.txt");
    if (saveFiles.is_open()) {
        getline (saveFiles, line);
        string arr[2];
        int i = 0;
        stringstream ssin(line);
        while (i < 2){
            ssin >> arr[i];
            ++i;
        }
        saveFiles.close();
        cout << "You have " << arr[0] << " hearts" << endl;
        cout << "You have " << arr[1] << " food";
    }
    else {
        cout << "Hearts > ";
        int hearts;
        cin >> hearts;
        cout << "Food > ";
        int food;
        cin >> food;
        ofstream saveFile;
        saveFile.open("save.txt");
        saveFile << hearts << " " << food;
        saveFile.close();
    }
    return 0;
}

For me, this checks if there is a save file, if there isn't, asks for input and then saves it. Then, when they come back, it checks again and it finds the file and gives you the data. This seems like it'd be how you make a save file (instead you likely wouldn't directly enter information like in my example).

As you can see, I create a save.txt file and it works fine. However, for games that I've run (like Minecraft), it doesn't use a .txt file. What difference does it make and what would work best for me (in any example)?

\$\endgroup\$
  • \$\begingroup\$ One problem with your file format you should be aware of is that it isn't forward-compatible. Adding a new feature which changes what data you need to save might break older savegames. But unless you intend to update your game a lot after release, this won't be a problem. Just something to keep in mind for the future. \$\endgroup\$ – Philipp Feb 8 '18 at 19:06
8
\$\begingroup\$

A file extension is not a file type.

A file extension is used (as a convention) by some OS's to assume the file type. But it doesn't really determine the file type. Ultimately the only thing that determines the file type is the content of the file.

So you can write a bunch of plain-old-ASCII to a file and call it savegame.bob if you like. It might deter casual introspection of the file, but it's still fundamentally a text file. And, for that matter, a perfectly fine way to save your games.

Eventually you may want to save your games by writing bytes that are not text, but rather are the numbers representing your game state directly. The advantage to such files (which are commonly called "binary files," even though that's a misnomer) is that they're usually easier and/or faster to load (since you don't have to parse the text. They're also slightly more difficult to casually inspect and hack (but not much so). The disadvantage is that they can be a bit more complicated.

But ultimately, no, it doesn't matter how you format your save file as long as it works for your game.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.