简体   繁体   English

如何在XNA中序列化对象列表?

[英]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. 我有一个要添加到游戏中的方块列表(有点像minecraft),我有一个有效的序列化功能,但是我对如何保存与该列表相对应的每个方块位置感到困惑。

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). 这就是保存的方式,当我调用先前的方法称为initialize时,它会保存分数(例如分数)和Vector2s(例如玩家位置),但是无论如何,返回到块位置都不会导致加载时发生任何事情(玩家位置更新除外)并更新分数)。

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, 然后,我可以使用Builder类中存在的参数添加到阻止列表中,如下所示:

    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. 现在我的问题是,我不知道如何保存每个阻止列表项目的位置,以便可以在命令后再次加载它们。

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: 如果将每个实体(例如您的blocks)存储在列表中,则可以使用XMLArray()属性:

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 . 之后,您可以使用System.XML.Serialization.XMLSerializer以与任何非XNA应用程序相同的方式对其进行序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM