XNA Creators Club Online
Page 1 of 1 (7 items)
並べ替え : 前へ 次へ

Using Content Pipeline for 'Complex' Classes

Last post 2009/07/21 15:51 by Shawn Hargreaves. 6 replies.
  • 2008/05/10 13:23

    Using Content Pipeline for 'Complex' Classes

    Thanks to Nick's excellent article I can use a custom content reader/writer to load class types at run-time successfully.

    However, where I am now getting stuck is where the class being imported has other user-defined classes within it, or indeed Lists of user-defined classes.

    E.g.
    Public ClassA
    {
    public int Foo;
    public List<ClassB> Bar;
    }


    Public ClassB
    {
    public int Foo;
    }


    How would you amend the Content Reader/Writer and the XML file to handle this situation ?  Any help would be greatly appreciated.
  • Re: Using Content Pipeline for 'Complex' Classes

    Answer
    返信 引用
    The Content Pipeline has a built in read/writer pair for List<T>, so as long as you provide a reader/writer for both your ClassA and your ClassB, in the ClassA reader you can then call writer.WriteObject(Bar), which will automatically call into our built in List<T> writer. That writer will loop over all the elements in the list, and call into your writer for ClassB to write out each of the individual values inside the list.
    XNA Framework Developer - blog - homepage
  • Re: Using Content Pipeline for 'Complex' Classes

    Thanks for the quick reply Shawn, I'll give that a go.


  • Re: Using Content Pipeline for 'Complex' Classes

    That works great, thanks again.


    Additional question, is the Queue collection type also supported, or would I have to enqueue all the items from a List<> ?
  • Re: Using Content Pipeline for 'Complex' Classes

    Queue is not supported by default, so you'd have to write custom code for loading and saving those (not terribly hard: you'd basically just write out the number of elements in the collection, then WriteObject each element in turn, but not so easy as if the type has a built in writer).

    The built in collection types we support, if I remember right, are Arrray, List<T>, and Dictionary<K,T>.
    XNA Framework Developer - blog - homepage
  • Re: Using Content Pipeline for 'Complex' Classes

    Related to this, I want to serialize/save as XML a part of my level, which is a levelRow.

    now my levelrow is a list of a type, where the type is an enumaration BlockType.

    I don;t seem to be able to write a contentwriter for BlockType, as it;s an enum, and not a class?

    public

     

    class LevelRow : List<BlockType>

     

    {

    #region

     

    Fields

     

     

    //List<BlockType> blocks;

     

     

    public int Width { get; set; }

     

     

     

     

  • Re: Using Content Pipeline for 'Complex' Classes

    The pipeline has built in support for both List<T> and enums, so you don't need to provide your own ContentTypeWriter for this.
    XNA Framework Developer - blog - homepage
Page 1 of 1 (7 items) 前へ 次へ