Author Topic: C# saving  (Read 3407 times)

0 Members and 1 Guest are viewing this topic.

Offline Augs

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 306
  • Rating: +30/-29
    • View Profile
C# saving
« 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)

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: C# saving
« Reply #1 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, this pretty much explains how to read/write files.
« Last Edit: December 12, 2012, 01:13:40 pm by Juju »

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: C# saving
« Reply #2 on: December 12, 2012, 01:14:19 pm »
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: C# saving
« Reply #3 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.
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

Offline Augs

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 306
  • Rating: +30/-29
    • View Profile
Re: C# saving
« Reply #4 on: December 12, 2012, 01:50:20 pm »
Thanks guys!

Edit: And sorry for asking stupid questions
« Last Edit: December 12, 2012, 01:52:58 pm by Augs »

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: C# saving
« Reply #5 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.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline stevon8ter

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 663
  • Rating: +10/-0
    • View Profile
Re: C# saving
« Reply #6 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
None of my posts are meant offending... I you feel hurt by one of my posts, tell me ... So i can appoligise me and/or explain why i posted it


Hi there, I'm the allmighty (read as: stupid, weak...) STEVON8TER