none
Is it possible to save and load my game? RRS feed

  • Question

  • Before I ask this question I'd like to say that I'm a beginner. So, I made a simple C# text adventure game in Visual Studio and was wondering if there was a way to save my game and then load the game exactly where I saved it. I want to retain the name, type and life I also want to load the game from where I left off. 


    Wednesday, February 27, 2019 7:51 PM

Answers

  • For a beginner, a simple text file might be a good starting point.

    // The name and fiull path where we store the state of the game. static string stateFile = "c:\\jobs\\stateFile.txt"; // Write the state of the game to file. static void WriteState(string name, int type, int livesRemaining) { FileStream stream = new FileStream(stateFile, FileMode.Create, FileAccess.Write, FileShare.None); using (StreamWriter sr = new StreamWriter(stream)) { sr.WriteLine(name); sr.WriteLine(type); sr.WriteLine(livesRemaining); sr.Close(); } } // Read the state of the game from the file. static void ReadState(ref string name, ref int type, ref int livesRemaining) { Stream stream = new FileStream(stateFile, FileMode.Open, FileAccess.Read, FileShare.Read); using (StreamReader sr = new StreamReader(stream)) { name = sr.ReadLine(); type = Convert.ToInt32(sr.ReadLine()); livesRemaining = Convert.ToInt32(sr.ReadLine());

    sr.Close(); } }


    Note that the above is a really simple example, to show the idea. In a more professional setting, the name, type, and lives would probably be contained in a class, TryParse would be more failsafe than Convert, and so on, and so on.

    Thursday, February 28, 2019 1:12 AM

All replies

  • In order to save some data, consider User Settings:

    I.e. add new definitions like myVariable in Settings area of Project Properties, then use code like ‘Properties.Settings.Default.myVariable = some_variable; Properties.Settings.Default.Save()’ to save the data, and ‘some_variable = Properties.Settings.Default.myVariable’ to read.

    Then decide how to use the loaded variables to restore the state of your game and continue from this state.


    • Edited by Viorel_MVP Wednesday, February 27, 2019 8:26 PM
    • Marked as answer by StudiousStudent Thursday, February 28, 2019 2:16 AM
    • Unmarked as answer by StudiousStudent Thursday, February 28, 2019 2:16 AM
    Wednesday, February 27, 2019 8:26 PM
  • For a beginner, a simple text file might be a good starting point.

    // The name and fiull path where we store the state of the game. static string stateFile = "c:\\jobs\\stateFile.txt"; // Write the state of the game to file. static void WriteState(string name, int type, int livesRemaining) { FileStream stream = new FileStream(stateFile, FileMode.Create, FileAccess.Write, FileShare.None); using (StreamWriter sr = new StreamWriter(stream)) { sr.WriteLine(name); sr.WriteLine(type); sr.WriteLine(livesRemaining); sr.Close(); } } // Read the state of the game from the file. static void ReadState(ref string name, ref int type, ref int livesRemaining) { Stream stream = new FileStream(stateFile, FileMode.Open, FileAccess.Read, FileShare.Read); using (StreamReader sr = new StreamReader(stream)) { name = sr.ReadLine(); type = Convert.ToInt32(sr.ReadLine()); livesRemaining = Convert.ToInt32(sr.ReadLine());

    sr.Close(); } }


    Note that the above is a really simple example, to show the idea. In a more professional setting, the name, type, and lives would probably be contained in a class, TryParse would be more failsafe than Convert, and so on, and so on.

    Thursday, February 28, 2019 1:12 AM
  • Thank you.
    Thursday, February 28, 2019 2:18 AM