Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Augs on December 12, 2012, 12:52:57 pm

Title: C# saving
Post by: Augs on December 12, 2012, 12:52:57 pm
So I am making a game based on this story.

http://ourl.ca/17701/328477

And I need to know how to make a save file. I only have XNA lib installed do I need more? And what do I do with that to get a save file(a .txt would do)
Title: Re: C# saving
Post by: Juju on December 12, 2012, 01:11:09 pm
No, you would just do something like,

Code: [Select]
using System.IO;
and there you go you have file reading/writing methods. Check out stuff about streams.

Code: [Select]
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";
        if (!File.Exists(path))
        {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }
    }
}
This is an example from the MSDN (http://msdn.microsoft.com/en-us/library/system.io.file.aspx), this pretty much explains how to read/write files.
Title: Re: C# saving
Post by: stevon8ter on December 12, 2012, 01:14:19 pm
http://giyf.com

And

http://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
Title: Re: C# saving
Post by: harold on December 12, 2012, 01:32:39 pm
Text files are hard. Not because they're hard per se, but using a text file means you'll need a to round-trip your data through a stringyfier and a parser and still be the same.

Binary files are much easier, you can use a BinaryWriter to just Write every field in every object (you may need to or want to add IDs to some objects so you can save references), use a BinaryReader and ReadSomeType to load (in the same way and order as you did the writing, of course), and your data is guaranteed to be intact. There will be no annoying things such as "loading a saved double loses accuracy", you won't have to decide on a syntax, and you won't have to write a parser.
Using automatic binary serialization has some of the same pros, but it has big cons: terribly slow, won't work across multiple versions, generates huge files, doesn't work with some build-in types anyway.
Title: Re: C# saving
Post by: Augs on December 12, 2012, 01:50:20 pm
Thanks guys!

Edit: And sorry for asking stupid questions
Title: Re: C# saving
Post by: Juju on December 12, 2012, 02:00:50 pm
No problem. Actually, it depends if it's a configuration file (that you might want the user to edit) or not. For a save file, you might be better use a BinaryReader/Writer and Read/Write<insert type here>(). That or you could use a gzip'd XML file. Still, for a binary save file, you can always manage to be consistent across versions.
Title: Re: C# saving
Post by: stevon8ter on December 12, 2012, 03:35:25 pm
Or you can do the easy c# way... Just store the string inside the program, store aml strings/arrays/integers/bools/... In the program itself, it's like 2min work... But i forgot the name of it and can't tell you till tommorow... Sorry