Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, September 7, 2016 4:34 PM
I just started a game for a school project in c# console application, is there any way to have a save/load data function?
I'm completely new to this.
All replies (5)
Wednesday, September 7, 2016 8:03 PM âś…Answered
Hi,
you could click on the links in my reply. StreamReader and StreamWriter are classes and the links will show you the documentation of the classes on MSDN.
Maybe the documentation at http://www.dotnetperls.com/streamwriter will help you to understand the streamwriter class.
There are also a lot of tutorials available. For example on youtube is a series "C# Beginners Tutorial". Part 39 / 40 are covering the StreamWriter: https://www.youtube.com/watch?v=ZhL0eECzcYM
https://www.youtube.com/results?search_query=c%23+tutorial+for+beginners+thenewboston+
With kind regards,
Konrad
Wednesday, September 7, 2016 5:13 PM
Hi,
there are a lot of ways how this could be done.
a) If you just want to save little data, then you might want to look deeper into config files. Visual Studio Projects have a build in support with a designer. Just open the project, go to te settings tab and add a config file.
- Make sure to have the default values coded in (Properties of the setting!)
- Choose user setting instead of application so that you can safe it.
Simply use the setting and after writing a new value: Safe the settings (Not possible with application settings! So take care to use the user settings!)
b) You could simply create the file yourself. You can write read / write logic on your own. (More work but you have full control. Could be good for a beginner to learn how to read / write files.)
c) You could simply create a class that holds the data and then use serialization to read / write an instance (e.g. binary serialization or XML Serialization.)
d) This is more or less b) - but instead of writing the files directly you keep that to special classes (e.g. XmlWriter / XmlReader or Linq to Xml) to write the data. That could be quite easy and straight forward, too. (But needs deeper knowledge and is not a good idea for a beginner in my eyes.)
So if you are a beginner, then I would recommend to look at a) and/or b.
With kind regards,
Konrad
Wednesday, September 7, 2016 6:44 PM
Hey,
Thanks a lot for your answer,
however I'm REALLY new and don't know anything about "read / write logic".
Could you perhaps use it in an example?
Excuse my ignorance :^)
Wednesday, September 7, 2016 7:31 PM
Hi,
that is no problem at all.
You could write a file line by line with the StreamWriter class. So you could write a text file with all values inside e.g. a key:value per line.
So the code could be something like this:
using (var sw = new StreamWriter(filename))
{
// use sr to write all values like this:
sr.WriteLine("{0}:{1}", "somekey", "somevalue");
}
When you read the file, you could use a StreamReader and then read the file line by line and then use String.Split to split the line at the ":".
And then you could check the key to store it in the correct variable.
So something like this:
using (var sr = new StreamReader(filename))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var columns = line.Split(new char[] {':'});
// columns[0] is the key
// columns[1] is the value
// So use these to set variables or so.
}
}
Be aware that the code was written in the editor here so there might by some typing errors. It is just meant to give you a first idea how code could like.
With kind regards,
Konrad
Wednesday, September 7, 2016 7:57 PM
Are the StreamReader & StreamWriter variables?
is the (filename) the name of my project?
I am completely lost... I am VERY new to this, I've been learning this for about three weeks.
Perhaps you could put it in correctly here?
Console.Clear();
System.Threading.Thread.Sleep(4000);
Console.WriteLine("Saving your game...");
System.Threading.Thread.Sleep(900);
Console.Clear();
Console.WriteLine("Saving your game..");
System.Threading.Thread.Sleep(900);
Console.Clear();
Console.WriteLine("Saving your game...");
System.Threading.Thread.Sleep(900);
Console.Clear();
Console.WriteLine("Saving your game..");
System.Threading.Thread.Sleep(900);
Console.Clear();
Console.WriteLine("Saving your game...");
System.Threading.Thread.Sleep(1300);
Console.Clear();
//I was thinking of having the codes here
Console.WriteLine("Your game has been saved!");
In case I still don't get it,
Do you perhaps know any good full tutorial videos/articles?
I have never even encountered what you are showing me...
I feel really stupid >.<