0
\$\begingroup\$

In my C++ program I am trying to develop a simple saving and loading function for my game. I can easily get the save function on it, but the loading part of it really stumps me.

What I need is to load the numbers on the file (line by line) and insert them into different variables for my player. I'm just not able to figure that out. I've even looked all around the inter webs.

Here is the code for the player save and load functions:

void Player::Save()
{
    std::ofstream savePlayerFile;
    savePlayerFile.open("Save/playerSave.dat", std::ofstream::trunc);

    savePlayerFile << x << "\n" << y << "\n" << prevX << "\n" << prevY << "\n" << health << "\n" << 
    health  << "\n" <<  mana  << "\n" <<  gold  << "\n" <<  level  << "\n" <<  maxHealth << "\n" << 
    maxMana << "\n" << strength << "\n" << defense;

    savePlayerFile.close();
}

void Player::Load()
{
    std::ifstream loadPlayerFile;
    loadPlayerFile.open("Save/playerSave.dat", std::ofstream::in);

    /*std::string line;
    while(std::getline(loadPlayerFile, line))
    {
        std::istringstream iss(line);
        */
    loadPlayerFile >> ( x && "\n" && y && "\n" && prevX && "\n" && prevY && "\n" && health && "\n" && 
    health && "\n" && mana && "\n" && gold && "\n" && level && "\n" && maxHealth && "\n" && 
    maxMana && "\n" && strength && "\n" && defense);
}
\$\endgroup\$
  • \$\begingroup\$ Possible resource here and here. Basically, instead of \n for writing, use spaces (" "), and in reading, remove the (/), replace && by >>, and remove all "\n": loadPlayerFile >> x >> y >> prevX >> prevY >> [...]; \$\endgroup\$ – Alexandre Vaillancourt Jul 9 '15 at 23:47
  • 5
    \$\begingroup\$ You'll have an easier time writing a binary file! \$\endgroup\$ – Alan Wolfe Jul 10 '15 at 3:23
  • \$\begingroup\$ @AlexandreVaillancourt Thanks So Much! The way you told me to do it worked out great! \$\endgroup\$ – PlatyPi Jul 10 '15 at 5:43
  • \$\begingroup\$ @AlanWolfe I might try and take that on. It seems interesting. \$\endgroup\$ – PlatyPi Jul 10 '15 at 5:43
0
\$\begingroup\$

I highly recommend serialization as well, whether you use boost or your own magic.

But, if you want to keep it simple for maybe prototyping sake, you could use streams like you are. You could even make this really easy on yourself and do space delimited. Since you know the format your saving, you simply have to parse it back the same way into whatever vars you want.

For example:

save() {
    stream << health << mana;
}

load() {
    stream >> health >> mana;
}

Obviously lots of code left out, but you get the idea. You would need to know the types as well, casting or converting as needed. Here is a cpluplus reference to what I'm talking about. http://www.cplusplus.com/reference/sstream/istringstream/istringstream/

\$\endgroup\$
0
\$\begingroup\$

It may work to write it on your own like that but that isn't a very flexible approach. If you add variables to the save file, you have to update them twice. This isn't preferable. Also, if you want to change formats, you need to restructure the entire thing.

Personally, I would use the Boost.Serialization library.

It is a very flexible library with more features to boot. It easily lets you use binary formats and xml formats, along with a simple text format not u like the one you suggest.

You could also use it with other parts of your game, instead of writing your own save files.

\$\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.