XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Using DefaultValueAttribute with XNA's Color / Vector2, etc.

Last post 4/25/2009 11:55 PM by bryanedds. 4 replies.
  • 4/25/2009 2:16 PM

    Using DefaultValueAttribute with XNA's Color / Vector2, etc.

    Hi all!

    I've been looking for a way to use the [DefaultValue] attribute on XNA's Color, Vector2, 3, etc. I tried pretty much everything with the attribute's overloads, but nothing seemed to work. It's really bad that like half of my .xml documents are a bunch of these -

          <Color>
            <R>255</R>
            <G>255</G>
            <B>255</B>
            <A>255</A>
            <PackedValue>4294967295</PackedValue>
          </Color>

    - over and over again.

    Any ideas?
    Please check out the Ox Game Engine!
  • 4/25/2009 5:04 PM In reply to

    Re: Using DefaultValueAttribute with XNA's Color / Vector2, etc.

    What serializer are you using to create this XML?

    From the way this is including both the R/G/B/A properties and the PackedValue, I would guess you're using XmlSerializer? That doesn't properly support the XNA Framework math types, so you will get this kind of duplication in the output XML.

    The Content Pipeline IntermediateSerializer class does know how to efficiently serialize all our math classes. Details here.
    XNA Framework Developer - blog - homepage
  • 4/25/2009 5:58 PM In reply to

    Re: Using DefaultValueAttribute with XNA's Color / Vector2, etc.

    Yes, I'm using the XmlSerializer and the duplicated values are really hurting my document load / save times.

    If there's no in-built way to fix this, can you think of a work-around I might try?

    Thanks Shawn!
    Please check out the Ox Game Engine!
  • 4/25/2009 11:47 PM In reply to

    Re: Using DefaultValueAttribute with XNA's Color / Vector2, etc.

    There is a built in way, it's called the IntermediateSerializer, it's linked in Shawn's post.
    Using that will allow you to define Vector3's as just <SomeVector>1 2 3</SomeVector>, as well as most other XNA types. This can then be compiled in to a binary format using a content writer, which means it will load a hell of a lot faster in your game.

    It will also allow you to set the [ContentSerializerIgnore] option on any properties which you do not wish to be contained in your .xml, and [ContentSerializer(Optional = true)] on any properties which you want to be serialized, but not necessarily required. This will allow you to then define default values in the constructor of your object.
  • 4/25/2009 11:55 PM In reply to

    Re: Using DefaultValueAttribute with XNA's Color / Vector2, etc.

    I think I'm going to try overriding some behavior of XmlReader / Writer for the Color items.
    Please check out the Ox Game Engine!
Page 1 of 1 (5 items) Previous Next