0

Most (if not all) games will remember the things (money, village size, manager amount, etc.) that you have so that when you start the game again, you still have everything you had obtained when you last left off. I am programming a game that needs to remember values, and I declare them initially at the beginning of the program. But since money equals zero, every time the user comes back to play the game, their earnings will be reset.

I will ultimately need this for most of my variables but I am actively working on Tutorial();. I am going to need the tutorial to only run once so I need the program to check whether the user has completed the tutorial every time the program begins. I have tried setting up a boolean value called isTutorialCompleted and having it check that in main when I run my functions. Code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted = false;

void initApp () {
}

void Tutorial() {

    cout << "Please Enter your name" << "\n";

    cin >> createManager;

    createdManagers.push_back(createManager);

    cout << "\n" << "You have created Manager " << createManager;

}

int main() {

    initApp();

    if (isTutorialCompleted == false) {
        Tutorial();
    }



}

This did not work due to the fact that every time I restarted the program the boolean value would reset so I tried making the boolean value undefined initially and then having it change in Tutorial. Code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;

void initApp () {

}

void Tutorial() {

    isTutorialCompleted = false;

    cout << "Please Enter your name" << "\n";

    cin >> createManager;

    createdManagers.push_back(createManager);

    cout << "\n" << "You have created Manager " << createManager;

    isTutorialCompleted = true;

}

int main() {

    initApp();

    Tutorial();

}

The problem with this is that it would not keep the value. isTutorialCompleted would go back to undefined in the program and ultimately change to false when tutorial begins. Finally, I tried checking for the boolean value in initApp();. Code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

//Game Variables
int money = 0;
string existingManagers = { "Benny, Rachel, Barnes, Flora, Gregory" };
string createManager = "";
vector<string> createdManagers {""};
bool isTutorialCompleted;

void initApp () {

    //Check if tutorial is completed
    if (isTutorialCompleted = true) {
        Tutorial();
    }

}

void Tutorial() {

    cout << "Please Enter your name" << "\n";

    cin >> createManager;

    createdManagers.push_back(createManager);

    cout << "\n" << "You have created Manager " << createManager;

    isTutorialCompleted = true;

}

int main() {

    initApp();

}

So the ultimate question is: How do you permanently store a variable so that when you restart the program, the stored variable does not reset?

  • It's not going to just save from the code itself. You need to store it somewhere non-volatile. For most games that's a save file. You create a file and write those values to the file. Then when you reload the game you read from said file and if there are values in it use them. – Delta_G Sep 10 '20 at 01:31
  • 1
    Welcome to Stack Overflow. The answer is *"write a file",* which should be a chapter in the text you're using to learn C++. If not, just search for "C++ file I/O". More fundamentally, when you implement new functionality, do it *in isolation* as much as possible, Most of the code above has nothing to do with the problem, and does not belong here. – Beta Sep 10 '20 at 01:32
  • @Beta Thanks a lot! I should have known I was going to have to write to a file. I guess I was hoping I could get around that. Yes, I was debating whether to just keep the whole code in there or delete the parts that did not matter. I guess I should have deleted those parts. – HyperText Markup Man Sep 10 '20 at 01:37
  • @Delta_G Thanks too! – HyperText Markup Man Sep 10 '20 at 01:37
  • Google serialization. That should give you some ideas. I've used sqlite for this myself. – Michael Surette Sep 10 '20 at 01:49

1 Answers1

-2

Store and import from a config file whenever the game starts.

  • Hint, when someone essentially answers a question in a comment, its generally good community practice to let them answer the asked question... rather then trying to jump (as a point grab) instead. And for that new C++ person, “config file” what’s that look like? – zipzit Sep 10 '20 at 01:40