Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:51:48+00:00 2026-05-16T22:51:48+00:00

I am trying to work my way through the XNA MSDN documentation on saving

  • 0

I am trying to work my way through the XNA MSDN documentation on saving and reading game data, and I am not having much luck.

In essence I have a manager class which keeps track multiple instance of base classes.

I want to be able to save the state of the entire list of objects that the manager is keeping track of.
Then load them in the next time the game loads. Basically saving the state of the world.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:51:49+00:00Added an answer on May 16, 2026 at 10:51 pm

    If you use the XmlSerializer as shown in the XNA 4.0 help, base classes need to have the [XmlInclude(Type)] attribute specified for each concrete type they can be serialized into.

    Below is an example of how to save game data in XNA 4.0. Press F1 to save once the game is running. The data will be saved to a location similar to C:\Users\{username}\Documents\SavedGames\WindowsGame\Game1StorageContainer\Player1.

    Loading the data again is a very similar process.

    To get this working on XBox add references to Microsoft.Xna.Framework.GamerServices & System.Xml.Serialization.

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Xml.Serialization;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;
    using Microsoft.Xna.Framework.GamerServices;
    
    namespace WindowsGame
    {
        [XmlInclude(typeof(Soldier)), XmlInclude(typeof(Grenade))]
        public class BaseGameObject
        {
            public Vector3 Position { get; set; }
        }
    
        public class Soldier : BaseGameObject
        {
            public float Health { get; set; }
        }
    
        public class Grenade : BaseGameObject
        {
            public float TimeToDetonate { get; set; }
        }
    
        public struct SaveGameData
        {
            public string PlayerName;
            public Vector2 AvatarPosition;
            public int Level;
            public int Score;
            public List<BaseGameObject> GameObjects;
        }
    
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            enum SavingState
            {
                NotSaving,
                ReadyToSelectStorageDevice,
                SelectingStorageDevice,
    
                ReadyToOpenStorageContainer,    // once we have a storage device start here
                OpeningStorageContainer,
                ReadyToSave
            }
    
            GraphicsDeviceManager graphics;
            KeyboardState oldKeyboardState;
            KeyboardState currentKeyboardState;
            StorageDevice storageDevice;
            SavingState savingState = SavingState.NotSaving;
            IAsyncResult asyncResult;
            PlayerIndex playerIndex = PlayerIndex.One;
            StorageContainer storageContainer;
            string filename = "savegame.sav";
    
            SaveGameData saveGameData = new SaveGameData()
            {
                PlayerName = "Grunt",
                AvatarPosition = new Vector2(10, 15),
                Level = 3,
                Score = 99424,
                GameObjects = new List<BaseGameObject>() 
                { 
                    new Soldier { Health = 10.0f, Position = new Vector3(0.0f, 10.0f, 0.0f) },
                    new Grenade { TimeToDetonate = 3.0f, Position = new Vector3(4.0f, 3.0f, 0.0f) }
                }
            };
    
            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
    
    #if XBOX
                Components.Add(new GamerServicesComponent(this));
    #endif
    
                currentKeyboardState = Keyboard.GetState();
            }
    
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();
    
                oldKeyboardState = currentKeyboardState;
                currentKeyboardState = Keyboard.GetState();
    
                UpdateSaveKey(Keys.F1);
                UpdateSaving();
    
                base.Update(gameTime);
            }
    
            private void UpdateSaveKey(Keys saveKey)
            {
                if (!oldKeyboardState.IsKeyDown(saveKey) && currentKeyboardState.IsKeyDown(saveKey))
                {
                    if (savingState == SavingState.NotSaving)
                    {
                        savingState = SavingState.ReadyToOpenStorageContainer;
                    }
                }
            }
    
            private void UpdateSaving()
            {
                switch (savingState)
                {
                    case SavingState.ReadyToSelectStorageDevice:
    #if XBOX
                        if (!Guide.IsVisible)
    #endif
                        {
                            asyncResult = StorageDevice.BeginShowSelector(playerIndex, null, null);
                            savingState = SavingState.SelectingStorageDevice;
                        }
                        break;
    
                    case SavingState.SelectingStorageDevice:
                        if (asyncResult.IsCompleted)
                        {
                            storageDevice = StorageDevice.EndShowSelector(asyncResult);
                            savingState = SavingState.ReadyToOpenStorageContainer;
                        }
                        break;
    
                    case SavingState.ReadyToOpenStorageContainer:
                        if (storageDevice == null || !storageDevice.IsConnected)
                        {
                            savingState = SavingState.ReadyToSelectStorageDevice;
                        }
                        else
                        {
                            asyncResult = storageDevice.BeginOpenContainer("Game1StorageContainer", null, null);
                            savingState = SavingState.OpeningStorageContainer;
                        }
                        break;
    
                    case SavingState.OpeningStorageContainer:
                        if (asyncResult.IsCompleted)
                        {
                            storageContainer = storageDevice.EndOpenContainer(asyncResult);
                            savingState = SavingState.ReadyToSave;
                        }
                        break;
    
                    case SavingState.ReadyToSave:
                        if (storageContainer == null)
                        {
                            savingState = SavingState.ReadyToOpenStorageContainer;
                        }
                        else
                        {
                            try
                            {
                                DeleteExisting();
                                Save();
                            }
                            catch (IOException e)
                            {
                                // Replace with in game dialog notifying user of error
                                Debug.WriteLine(e.Message);
                            }
                            finally
                            {
                                storageContainer.Dispose();
                                storageContainer = null;
                                savingState = SavingState.NotSaving;
                            }
                        }
                        break;
                }
            }
    
            private void DeleteExisting()
            {
                if (storageContainer.FileExists(filename))
                {
                    storageContainer.DeleteFile(filename);
                }
            }
    
            private void Save()
            {
                using (Stream stream = storageContainer.CreateFile(filename))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
                    serializer.Serialize(stream, saveGameData);
                }
            }
    
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
    
                base.Draw(gameTime);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work my way through Ron Jeffries's Extreme Programming Adventures in C#.
I have been trying to work my way through Project Euler, and have noticed
I am trying to work my way through basic iPhone programming and I have
I am trying to work my way through the NerdDinner tutorial - and as
I'm trying to work my way through some frustrating encoding issues by going back
I'm trying to work my way through Compilers: Backend to Frontend (and Back to
I'm trying to work my way through an Objective-C tutorial. In the book there
I've been trying to work my way through Problem 27 of Project Euler, but
I'm trying to work my way through a Java Tutorial . The author wrote
I was trying to work my way through the nodepad tutorial on Dailyjs.com (found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.