Skip to main content
Tweeted twitter.com/StackCodeReview/status/971422889063931905
added 96 characters in body
Source Link
Summer
  • 2.4k
  • 2
  • 16
  • 31

I almost forgot to add the data.

saveData.txt:

1 1 1 1 0 4 64 18 10 1 0 1 30 2 1 0 0

I would love any and all feedback. Thank you.

I would love any and all feedback. Thank you.

I almost forgot to add the data.

saveData.txt:

1 1 1 1 0 4 64 18 10 1 0 1 30 2 1 0 0

I would love any and all feedback. Thank you.

Source Link
Summer
  • 2.4k
  • 2
  • 16
  • 31

Read and write Game Save data

I have a class that reads and writes data to and from a .txt file using fstream.

As far as I can tell it does exactly what I want it to do:

  • if no file exists create one with default initial variables (those are the constants.)
  • if a file does exist read it and set the values of the data structure to those in the file.
  • overwrite the file with data when called.
  • I even left myself a little reset feature that I can use to allow the player to reset their data and start from scatch (although deleting the file would work too)

I don't check that the file hasn't been tampered with but I intend to have those values checked before passing them to their objects.

saveManager.h:

#pragma once
#ifndef SAVEMANAGER
#define SAVEMANAGER
#include <fstream>
#include "saveData.h"

class saveManager
{
public:
    saveManager();

    void read(saveData& data);

    void reset();

    void write(saveData& data);
private:
    // default magic numbers for initializing data

    const int m_major = 1;
    const int m_minor = 1;
    const int m_highWorld = 1;
    const int m_highSubWorld = 1;
    const int m_loot = 0;
    const int m_batSpeed = 4;
    const int m_batSize = 64;
    const int m_maxBallSpeed = 18;
    const int m_ballSize = 10;
    const int m_ballStrength = 1;
    const int m_homing = 0;
    const int m_value = 1;
    const int m_magnet = 30;
    const int m_maxLives = 2;
    const int m_bossDmg = 1;
    const int m_phantom = 0;
    const int m_TBD = 0;

    std::fstream saveFile;
};
#endif // !SAVEMANAGER

saveManager.cpp:

#include "saveManager.h"

saveManager::saveManager()
{
    // do nothing
}

void saveManager::read(saveData& data)
{
    int major;
    int minor;
    saveFile.open("saveData.txt");
    if (saveFile.is_open())
    {
        saveFile >> major >> minor;
        if (major == data.major && minor >= data.minor)
        {
            saveFile >> data.highWorld >> data.highSubWorld >> data.loot >> data.batSpeed >> data.batSize >> data.maxBallSpeed >> data.ballSize >> data.ballStrength >> data.homing >> data.value >> data.magnet >> data.maxLives >> data.bossDmg >> data.phantom >> data.TBD;
        }
        // handle older versions here as needed
    }
    else
    {
        saveFile.open("saveData.txt", std::fstream::in | std::fstream::out | std::fstream::trunc);
        reset();
    }
    saveFile.close();
}

void saveManager::reset()
{
    if (!saveFile.is_open())
    {
        saveFile.open("saveData.txt");
        saveFile << m_major << " " << m_minor << " " << m_highWorld << " " << m_highSubWorld << " " << m_loot << " " << m_batSpeed << " " << m_batSize << " " << m_maxBallSpeed << " " << m_ballSize << " " << m_ballStrength << " " << m_homing << " " << m_value << " " << m_magnet << " " << m_maxLives << " " << m_bossDmg << " " << m_phantom << " " << m_TBD;
        saveFile.close();
    }
    else
    {
        saveFile << m_major << " " << m_minor << " " << m_highWorld << " " << m_highSubWorld << " " << m_loot << " " << m_batSpeed << " " << m_batSize << " " << m_maxBallSpeed << " " << m_ballSize << " " << m_ballStrength << " " << m_homing << " " << m_value << " " << m_magnet << " " << m_maxLives << " " << m_bossDmg << " " << m_phantom << " " << m_TBD;
        saveFile.close();
    }
}

void saveManager::write(saveData& data)
{
    if (!saveFile.is_open())
    {
        saveFile.open("saveData.txt");
        saveFile << data.major << " " << data.minor << " " << data.highWorld << " " << data.highSubWorld << " " << data.loot << " " << data.batSpeed << " " << data.batSize << " " << data.maxBallSpeed << " " << data.ballSize << " " << data.ballStrength << " " << data.homing << " " << data.value << " " << data.magnet << " " << data.maxLives << " " << data.bossDmg << " " << data.phantom << " " << data.TBD;
        saveFile.close();
    }
    else
    {
        saveFile << data.major << " " << data.minor << " " << data.highWorld << " " << data.highSubWorld << " " << data.loot << " " << data.batSpeed << " " << data.batSize << " " << data.maxBallSpeed << " " << data.ballSize << " " << data.ballStrength << " " << data.homing << " " << data.value << " " << data.magnet << " " << data.maxLives << " " << data.bossDmg << " " << data.phantom << " " << data.TBD;
        saveFile.close();
    }
}

I would love any and all feedback. Thank you.