Saving Game

Apr 11, 2013 at 1:16 AM
I made over 100 blocks and want to just play but cant save the game! Im not the greatest at coding but i know bits of many types! I trying to put this in and keep having errors with game1.cs

http://robotfootgames.com/2011/05/savingloading-using-easystorage-with-xna-4-0/

any idea on a simple save and load code?
Apr 19, 2013 at 10:23 PM
Here is what I do to save a map. I created this class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TechCraftEngine.WorldEngine
{
    public class BlockSavable
    {
        public int BlockTypeSimple
        {
            get;
            set;
        }

        public Vector3 Position
        {
            get;
            set;
        }
    }
}
Then I added this method to the World class.
        public List<BlockSavable> getSavableBlocks()
        {
            List<BlockSavable> blocksSavable = new List<BlockSavable>();

            foreach (Region r in _regions)
            {
                for (int x = 0; x < r.Blocks.GetLength(0); x++) 
                {
                    for (int y = 0; y < r.Blocks.GetLength(1); y++) 
                    {
                        for (int z = 0; z < r.Blocks.GetLength(2); z++) 
                        {
                            if (r.Blocks[x, y, z] != BlockType.None)
                            {
                                BlockSavable blockSavable = new BlockSavable();
                                blockSavable.BlockTypeSimple = (int)r.Blocks[x, y, z];
                                blockSavable.Position = new Vector3(x, y, z);
                                blocksSavable.Add(blockSavable);
                            }
                        }
                    }
                }
            }
            return blocksSavable;
        }
Then to save and load, I use something like this..
        public static void SaveBlocks(List<BlockSavable> blocks)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Level));
            TextWriter textWriter = new StreamWriter(@"C:\tests\blocks.xml");
            serializer.Serialize(textWriter, blocks);
            textWriter.Close();
        }

        public static List<BlockSavable> LoadBlocks()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<BlockSavable>));
            TextReader textReader = new StreamReader(@"C:\tests\blocks.xml");
            List<BlockSavable> blocks;
            blocks = (List<BlockSavable>)deserializer.Deserialize(textReader);
            textReader.Close();

            return blocks;
        }