Closed Thread
Results 1 to 5 of 5

Thread: How do i Save/Load my own file

  1. #1
    kavery is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    currently base at Bolton
    Posts
    40
    Blog Entries
    3
    Rep Power
    0

    Question How do i Save/Load my own file

    hi

    i making a game which i want to be able to save out the important detail and be able to read it again at a later date (like .SAV files for games). but i don't know where to start.

    I'm using XnA 3 with VisualStudio 2008. this is being designed for pc and xbox360 so i need to know how both will work.

    any toutarials ya know of would be great cause i can't find any (not good with internet) i dont know much on saving and loading so i'm learning this bi from scrath

    thanks for any help

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    QuackWare is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    95
    Rep Power
    8

    Re: How do i Save/Load my own file

    Since I am not entirely sure what you want to save and what format you want to use I suggest you look through these C# File Stream Samples. There's examples for text files, binary files, and a bunch of other save methods.

  4. #3
    kavery is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    currently base at Bolton
    Posts
    40
    Blog Entries
    3
    Rep Power
    0

    Re: How do i Save/Load my own file

    The thing is i don't know anything about it so i don't know what i want. this is why i need help to learn it and then use it.
    I need to find a way or a few of saving out all the data i require I.E , will include, player: level, items, weapons, powers etc,. world changes, list<T> of differening lengh and any so on. hope that makes it easy.

    i'll check out the site and see what i can find thanks

  5. #4
    kavery is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    currently base at Bolton
    Posts
    40
    Blog Entries
    3
    Rep Power
    0

    Re: How do i Save/Load my own file

    okay i've looked at a bit and found that i think the text saving is to constricve.
    but i think i can do want i want using xml.

    looking at XNA Essentials as an example could work in my idea.

    I just got a few questions that i want to ask,
    1) xml looks like you can load and save a serizable strut, would it be possible to make a seizale class
    2) it dosen't mention any limits so can you store higher acrhy of lists i.e. list of players party, with list of items on each, so on ans so forth. (save an entire class)
    3) and i think it unlikely but could ya save in sections in the xml, or combine xml files into another type so ya could do something a section at change so player destory city, can ya just save that part or do you have to save entire game.

  6. #5
    kavery is offline Learning Programmer
    Join Date
    Nov 2009
    Location
    currently base at Bolton
    Posts
    40
    Blog Entries
    3
    Rep Power
    0

    Lightbulb Re: How do i Save/Load my own file

    i've one some reseach and i've come up with this

    Code:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework.Storage;
    
    using System.Text;
    
    using System.Xml.Serialization;
    
    //Kavery
    //K_SaveManager   V1.0
    //28-01-2010
    
    
    namespace KaveryFramework2D
    {
        /// <summary>
        /// a Class that can save any class data Class
        /// will have an auto save. function
        /// may have large file size but this was all i could understand
        /// </summary>
        /// <typeparam name="ClassToSave"></typeparam>
        [Serializable]
        class K_SaveManager<ClassToSave> //where ClassToSave : new()
        {
            //require for serialization
            public K_SaveManager()
            {
            }
    
            
            public void Save(ClassToSave data, string fileName)
            {
                // Get the path of the save game
                string fullpath = Path.Combine(StorageContainer.TitleLocation, fileName);
    
                // Open the file, creating it if necessary
                FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
                try
                {
                    // Convert the object to XML data and put it in the stream
                    XmlSerializer serializer = new XmlSerializer(typeof(ClassToSave));
                    serializer.Serialize(stream, data);
                }
                finally
                {
                    // Close the file
                    stream.Close();
                }
            }
    
            public void AutoSave(ClassToSave data)
            {
                Save(data, "AutoSave");
            }
    
            public ClassToSave AutoLoad()
            {
                return Load("AutoSave");
            }
    
            /// <summary>
            /// will load default obhect if there n load
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public ClassToSave  Load(string fileName)
            {
                ClassToSave data;
    
    
                // Get the path of the save game
                string fullpath = Path.Combine(StorageContainer.TitleLocation, fileName);
    
                FileStream stream;
                // Open the file se to null if no there
                try
                {
                    stream = File.Open(fullpath, FileMode.Open, FileAccess.Read);
                }
                catch 
                {
                    //stream = File.Open(fullpath, FileMode.OpenOrCreate, FileAccess.Read);
                    data = default(ClassToSave);
                    return data;
                }
    
                try
                {
    
                    // Read the data from the file
                    XmlSerializer serializer = new XmlSerializer(typeof(ClassToSave));
                    data = (ClassToSave)serializer.Deserialize(stream);
                }
                
    
                finally
                {
                    // Close the file
                    stream.Close();
                }
    
                return (data);
            }
    
    
        }
    }
    I've tested it a Vector2, and it seems to work
    if anyone can notice a problem or a knows better way i'm happy to hear ya ideas.

    oh yeah and thankz for the link it got me startedand give me a chance to improve my personal framework.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. load file address??
    By PutchPT in forum C# Programming
    Replies: 4
    Last Post: 06-01-2011, 03:10 PM
  2. Load / save data from text file
    By wazofski in forum Visual Basic Programming
    Replies: 20
    Last Post: 11-14-2010, 10:10 AM
  3. load a file on an RTB
    By ravmonster in forum Visual Basic Programming
    Replies: 7
    Last Post: 08-02-2009, 05:38 PM
  4. Replies: 1
    Last Post: 01-09-2009, 05:51 AM
  5. Save/Load Data?
    By slovig in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-04-2008, 03:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts