Text-based game, saving progress into some file, .sav? How to?

This is a discussion on Text-based game, saving progress into some file, .sav? How to? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone. I'm making another game. This time, a game, which is text-based. I would like to make players able ...

  1. #1
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59

    Text-based game, saving progress into some file, .sav? How to?

    Hi everyone. I'm making another game. This time, a game, which is text-based. I would like to make players able to save their progress. Like there's gonna be a few quests, items, etc. So, i would like make players able to save, what they did.

    Ho could i do that? Preferable a .sav file. Or something encoded and similar, so players wouldn't be able to edit their statistics, for reasons to-be-implemented.

    Thanks.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Hey. I've done similar work myself, actually just finished coding a load/save game function for a sudoku game I'm developing. I'd suggest looking into file streams, and how they work. I'm sure you are already using #include <iostream>. If you didn't already know thats your Input Output stream. If you do a google search on fstream you should find all the information you need to know about loading and saving .txt or .dat files. I personally have used txt files, and I haven't bothered to encrypt any of my save files, because honestly none of the games actually have a fan base yet lol. So if I give a game to a few friends and my cousin, well I'm not concerned if they decide to cheat. Furthermore I usually program a cheat code or two into each game and tell them about it when I give them the game lol.

    1. Learn about fstreams and do a test to see if you can save a file with just a few numbers and load it back into a program and cout those numbers.
    2. Once those are working look at your game and ask yourself "what is the fewest number of variables I can pass to a file and continue my game at any point"
    3. Impliment your save file function into the game and save all those variables into whatever format you want.
    4. Check your save file and make sure all the data is correctly saved. (open it in notepad if you save as .txt.. just don't type anything in)
    5. Impliment your load file function into the game and see if you can load the previous game.
    6. Once you get all that in working order then encrypting wouldn't be hard. The simplest form of encryption would be just like playing a cryptogram. Make your own "alphabet"/"number" key and use it to change the numbers before they are saved and convert back when loaded. (simple switch statement would do that)

    Hope this helps if you have any further questions on fstreams after you start reading up, try to code a function then post us the code and explain why you think its not working or what you've tried so far. Cheers!

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    This site itself has an introductory tutorial on file i/o available here.

    If that doesn't give you enough info, there is a quite thorough article available which, while it will be a much longer read, should clear up any persistent questions. Find it here.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    I suggest you get familiar with Boost.Serialization. You can find the tutorial here: Serialization
    This will cut the work down tremendously. Save to file. Read from file. Any type you want. It will even save files such as vectors and lists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Thanks for the answers.
    Elysia, thanks for that, but since this project is mainly for learning purposes, I'm going to do nothing with libraries. However, I'm planning on rewriting the game with the same story with libraries and graphics, after it's done.
    Anyway, I've tried to make it work, i almost did. But I'm stuck on the getline(); thing. I've read two tutorials about getline(). One of them, however, had two mistakes in the code, lol.
    Anyway, I cannot get it work, at least at the moment. Here's the code:
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int str;
      int file = 1;
      int file1 = 2;
      int file2 = 3;
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<file;
      a_file << file1;
      a_file << file2;
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file.getline(str, 1);
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    There's an error to the dot in here: b_file.getline(str, 1);. b_file. <--- this dot.

    What I'm trying to do, is appoint each line to a variable. The code, is going to be like this (as i understand it should be now):
    Code:
    int quest1completed = 1; // or true.
    int quest2completed = 0; // or false.
    /* the code to write it to two lines to the text file. */
    /* the code to read the file. */
    /* the code to make line one quest1completed's variable and second line - quest2completed's. */

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    I don't see what learning has to do with not using libraries. You are learning how to effectively use the tools at your disposal to accomplish your task.
    Also, I have no idea why you think it will output "this".
    What you do is store 1, 2 and 3 to the file. Then you try to read back a line using getline (which takes a char*!), and then read an int.
    The getline is wrong, wrong, wrong, wrong. First off, it takes a string, not an int. Then you should use std::getline instead of the member function getline. Finally, if you read back everything you wrote, you should get 1, 2 and 3.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Okay, I've tried to read about the Serialization thing from about 2PM, so it's been about half an hour. However, it seems, that it's too hard for me to understand it still. So again - there comes up the learning process, while writing the text-based RPG game.
    Hm, so you are saying, that i should use strings for saving and variables instead? So it would be something like this:
    Code:
    string q1c = "yes";
    string q2c = "yes";
    string q3c = "no";
    
    // Write the code.
    // Load the code.
    // Code loaded = q3c;
    That should be the principle of writing this save system?

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    No.

    He is not saying that.

    He is saying that output and input must have parity (not a parity bit). If you write integers, you need to process the input as integers to get back integers.

    Soma

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Also, I am saying that std::getline takes a string and istream.getline takes a char*, not an integer. If you want to use std::getline, you need to read into a string and not an integer.
    But again, this is rather pointless since you're writing integers. Like Soma says, the type you write is the type you shall read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Well, so it's either char variables, or string variables. Which means, that i couldn't use any integers or bools, which would be much more better for my game.

    By the way. I've used to play some games, some time ago, which have had options.ini files for example. Integers were like resolutionX = "640", resolutionY = "480", cheatmode = "on". Something like that. What was that? It seems like the integers were written right into the file, and read right from the file. Like inserting a line into the code.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Quote Originally Posted by newn View Post
    Well, so it's either char variables, or string variables. Which means, that i couldn't use any integers or bools, which would be much more better for my game.
    No, once again, you misunderstand. It doesn't matter what types you have. You can always read and write them.
    However, if you are going to use std::getline you must read into a string. You don't need to use std::getline, however. You can simply use the >> operator to read directly back into your ints.
    Basically reading is the same as writing, only reversed operator (<< vs >>). And it must be the same types!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Yea, i considered that, but the you cannot read in lines, it just reads the whole text, doesn't it? I've been studying 3 tutorials about that method. It didn't say anything in particular, however, the whole text was printed out.

    And sorry for misunderstandings, my main language isn't english.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    The stream operators are very nice. If you do 3 subsequent << to write, then you can always do 3 subsequent >> to read that data. But if and only if, you use the same types that you wrote.
    If you wish to read a whole line, then you should use std::getline. Then you would have to parse it and pick up the parts you want and convert those back to a specific type such as int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Novice programmer newn's Avatar
    Join Date
    Aug 2010
    Posts
    59
    Okay, i get output like that: 123123 (new line) many numbers (new line) many numbers.
    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int str;
      int str1;
      int str2;
      int file = 1;
      int file1 = 2;
      int file2 = 3;
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file << file << file1 << file2;
      a_file << file;
      a_file << file1;
      a_file << file2;
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str >> str1 >> str2;
      //Should output 'this'
      cout<< str <<"\n" << str1 << "\n" << str2 << "\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    I guess, that i didn't put the >> and << in the right place, or do i need to put some line breaks?

    Also, I've rewrote my own code, not copying from the example and adding a few things:

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int var1 = 1;
    	int var2 = 2;
    	int var3 = 3;
    
    	ofstream file;
    	file.open("file.txt");
    
    	file << var1;
    	file << var2;
    	file << var3;
    	file << var1 << var2 << var3;
    
    	file.close();
    
    	int var4;
    	int var5;
    	int var6;
    	int var7;
    
    	ifstream file1;
    	file1.open("file.txt");
    
    	file1 >> var4;
    	file1 >> var5;
    	file1 >> var6;
    	file1 >> var4 >> var5 >> var6 >> var7;
    
    	file1.close();
    
    	cout << var4 << "\n" << var5 << "\n" << var6 << "\n";
    	cin.get();
    }
    Same Stuff Happens...

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    What do you want to happen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21