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

Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

Last post 8/16/2009 7:56 AM by Manuel Moos. 6 replies.
  • 8/15/2009 4:53 AM

    Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    I get the following message when trying to use the intermediate serializer to serialize my tileset class.

    Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    I don't have any references to the GraphicsDevice inside my tileset class. Here is the code

    public sealed class Tileset  
        {  
            public string AssetName { getset; }  
     
            [ContentSerializer(SharedResource = true)]  
            public Texture2D Texture;  
     
            [ContentSerializer(SharedResource = true)]  
            public Rectangle[] Tile;  
     
            public Tileset(string assetName, Texture2D texture)  
            {  
                var width = 0;  
                var height = 0;  
                var i = 0;  
     
                AssetName = assetName;  
     
                Texture = texture;  
     
                width = Texture.Width / 32;  
                height = Texture.Height / 32;  
     
                Tile = new Rectangle[width * height];  
     
                for (var y = 0; y < height; y++)  
                {  
                    for (var x = 0; x < width; x++)  
                    {  
                        Tile[i] = new Rectangle(x * 32, y * 32, 32, 32);  
                        i += 1;  
                    }  
                }  
            }  
        } 

    Just for the fun of it, I use this class that I made to do the serialization:

    public static class XmlContentCreator  
        {  
            public static void Create<T>(string filename, T value)  
            {  
                XmlWriterSettings settings = new XmlWriterSettings();  
                settings.Indent = true;  
     
                using (XmlWriter writer = XmlWriter.Create(filename, settings))  
                {  
                    IntermediateSerializer.Serialize(writer, value, null);  
                }  
            }  
        } 

    I am not 100% sure what I am doing wrong here. I flagged what I thought to be the offending variables and still no dice. Thanks for any help in advance.

    "A good game is eventual, a bad game is bad forever..." -Shigeru Miyamoto
  • 8/15/2009 8:44 AM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    Answer
    Reply Quote
    Uninformed guess: It's the Texture2D. That one would certainly reference the graphics device it was created with. Instead of serializing that, just serialize the texture resource name; create a string typed property for that.
  • 8/15/2009 1:48 PM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    I might as well drop using the serializer for this object then. Adding a string property means that even after I call   myTileset = Content.Load<Tileset>("MyTileset"), I am still going to have to call an initialize function to load the content from that property which is double the work. I certainly appreciate your time and answers though, I had a feeling that was going to be it but I wanted a second opinion.
    "A good game is eventual, a bad game is bad forever..." -Shigeru Miyamoto
  • 8/15/2009 8:24 PM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    Well, nobody (apart from the .NET guidelines of not doing heavy work in property accessors) says you can't do the texture loading in the property set function directly.
  • 8/16/2009 12:44 AM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    Best reply evar! I have used properties for some many different things and I can't believe I didn't think of that. You totally rock!
    "A good game is eventual, a bad game is bad forever..." -Shigeru Miyamoto
  • 8/16/2009 2:21 AM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    Manuel Moos:
    Well, nobody (apart from the .NET guidelines of not doing heavy work in property accessors) says you can't do the texture loading in the property set function directly.
    That guideline exists for a very, very good reason. I strongly advise against loading resources in a property set accessor.

    If you're loading some data that requires associated content, give the loaded data a method named something like Initialize, that takes a reference to a ContentManager and uses it to load any dependencies. Much easier to debug and much more obvious than doing loading in a property setter - nobody ever expects 'obj.foo = bar' to cause a game to hang for half a second and then throw a content loading exception.
    Kevin Gadd, Squared Interactive
    Development Blog | Twitter
    Help playtest my game, Inferus!
  • 8/16/2009 7:56 AM In reply to

    Re: Cyclic reference found while serializing Microsoft.Xna.Framework.Graphics.GraphicsDevice. You may be missing a ContentSerializerAttribute.SharedResource flag.

    True. But it is also just a guideline for a reason. In this particular case, the context the set function would be called from already is content loading, so content loading is precisely what the user should expect. To avoid unaware calls from other contexts, use documentation and give the property a suggestive name.

    But sure, writing custom XML parsing code would be the cleaner overall solution.
Page 1 of 1 (7 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG