Nero-One
Nero-One

Reputation: 127

How to Serialize a list of Objects in XNA?

I have a list of blocks to be added into the game (kind of like minecraft) I have a working serialize function, but I'm confused on how to save Each of the blocks positions corresponding to the list.

for example,

    drive = StorageDevice.EndShowSelector(result);
        if (drive != null && drive.IsConnected)
        {

                SaveGame SaveData = new SaveGame()
                {
                    playerpos = player.playerPosition,
                    lvl = Level,
                    exp = Exp,
                    munny = Money,


                };
                IAsyncResult r = drive.BeginOpenContainer(containerName, null, null);
                //etc... etc..
                stream.Close();
                container.Dispose();
                result.AsyncWaitHandle.Close();

            }

this is how it saves, when I call a previous method called initiate saves it saves ints like the score and Vector2s like the players postition, however, refferring to the block position in anyway results in nothing happening upon load (except for the player position updating and the score updating).

If this were in an update loop I could simply count the objects like normal with:

      for (int b = 0; b < game.blocklist.Count; b++)
        {
            //etc..
         }

however, this is not in the update loop, and I'm confused as to whats happening.

EDIT: To make my problem more clear, I don't know exactly how to serialize the list in my storage class

For Example, this is how I normally set up a list in my Games Contsructor:

    public List<Builder> blocklist = new List<Builder>();

I can then add blocks to the blocklist using the parameters present in the Builder class like so,

    if (player.Builder == true && player.LMBpressed == true && blockspawnamount >= placeblock)
        {
            if (build.BlockID == 1)
            {

                position = new Vector2((int)(cursor.cursorPos.X / 58) * 58, (int)(cursor.cursorPos.Y / 58) * 58);

                blocktex1 = grass1;
                block = new Builder(this, blocktex1, new Vector2(position.X, position.Y));

                blocklist.Add(block);


                placeblock = 200.0f;
            }

        }

Then updating the blocklist accordingly. Now My issue is, I don't know how to save each of the blocklists items positions so that they can be loaded again upon command.

Upvotes: 0

Views: 504

Answers (1)

Codie Morgan
Codie Morgan

Reputation: 270

I'm amazed this was never answered, but is I understand correctly - you were asking how to serialize a list ( or array ) of your blocks ?

My answer assumes your entire class is serializable:

If you are storing each entity ( such are your blocks ) , in a list, you can use the XMLArray() attribute:

using System.Xml;
using System.Xml.Serialization;
using System.IO;

[Serializable()]
public class MyClass
{

    private list<blocks> m_Blocks = new List<block>();

    [XMLArray("Items")]
    public list<block> Blocks{ 
        get{ return m_blocks; }
        set{ m_Blocks = value; }
    };

    // ... other members
}

... which should work as long as all the fields in your object are serializeable objects or value-types.

After which, you can just serialize it the same way as any Non XNA application using System.XML.Serialization.XMLSerializer.

Upvotes: 1

Related Questions