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

Forcing types of objects to not be cached

Last post 05-02-2008 12:11 PM by Shawn Hargreaves. 9 replies.
  • 05-02-2008 12:03 AM

    Forcing types of objects to not be cached

    So, the short question for those of you who aren't interested in the why - I've written the content pipeline for a particular type of object. When I load two of the same object, I'm given the cached in-memory instance of the first to use the 2nd time around.  Great for textures, bad for stuff that I modify in game.  Is there a way to write the pipeline such that it doesn't cache that particular type of object?

    More specifically, I'm making modifications to the object once I load it in my game; however, at a later time I want to load the original un-modified object, but due to caching I'm getting the modified object.  There are a few ways I can see to correct this:

    1. Have the code internally create a copy of the object the first time it's loaded, then return a clone of that object.  Then the clone can be modified, and when a new object is loaded, the original is cloned once again.  Would solve the problem, but also requires extra memory to hold unused copies of objects which can be expensive.
    2. Somehow force the content pipeline to load from .xnb every time.  Not sure how to do that.
    3. Load the content of this type in a special ContentManager which gets cleared out after every load.  Of course, since my object reference other things like textures, clearing this out would result in the textures and other assets that I want to keep around being nuked as well so that doesn't seem like a good option.
    4. Give up and become a mountain climber.
    Any thoughts on how to accomplish this?
    --Vic--
  • 05-02-2008 12:18 AM In reply to

    Re: Forcing types of objects to not be cached

    Answer

    Maybe if you told us what type of thing you were trying to do this with and why we could give some suggestions. When I first started with XNA, everytime I thought I needed to do this....I was wrong.

    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 05-02-2008 12:34 AM In reply to

    Re: Forcing types of objects to not be cached

    It's a custom type called a Scene.  It basically contains a list of different game elements like Sprites and Models.  So, let's say it has sprites for a stoplight, the post for the stoplight, and the road itself.  As soon as I load the object and move them, then I no longer have them in their old positions if I try to load that object.  In the case where I'm performing attachments and using their positions as relative positions, things can get pretty screwy.

    I'm interested in hearing your thoughts on the matter, but I'm really confident that this is the cleanest way to get around the problem - especially since the file type I'm using has been traditionally loaded from-file and NOT be cached in the engine for quite a number of years.  It's really small and it does use other cached data like textures. In other words, the usage pattern of not caching the Scene itself is pretty well established.
    --Vic--
  • 05-02-2008 12:40 AM In reply to

    Re: Forcing types of objects to not be cached

    I believe if you load in an object and then call Unload on the ContentManager used to load it, it will erase its cache, letting you load it from file again. So perhaps just make a second ContentManager that is only used for Scenes and then call Unload right before loading in a Scene object. I can't think of any reason why that would mess up your Scene. It would mess up textures and such because it will call Dispose on them (I think), but since you have a custom Scene object with no textures or anything, it might work just fine for your needs. Give it a try and see.

    Nick Gravelyn -- Microsoft XNA MVP
    Blog | XNA Wiki | FX-izer | EasyZip | Current Adventures | Next-Gen
  • 05-02-2008 12:46 AM In reply to

    Re: Forcing types of objects to not be cached

    Perhaps I'm misunderstanding, but the Scene class does contain objects which have things like ExternalReference<TextureContent> inside of them.  So, when I load the Scene class, I also load the Textures that objects in the Scene load, and I don't know how to have the Scene sit in one ContentManager while the Textures that it references throught the content pipeline sit on another.  Therfore, the unload doesn't seem like it'd work for me - again, unless I'm misunderstanding.
    --Vic--
  • 05-02-2008 12:54 AM In reply to

    Re: Forcing types of objects to not be cached

    public class Sprite
    {
    Texture2D Image;
    Vector2 Position;

    void Sprite()
    {

    }
    }

    List <Sprite> StopLights;

    void Initialize()
    {
    Texture2D StopLightImage = Content.Load<Texture2D>("stoplightimage.jpg");

    StopLights = new List<Sprite>;

    Sprite newLight = new Sprite();
    StopLights.Add(newLight);

    newLight = new Sprite();
    StopLights.Add(newLight);

    foreach ( Sprite thisLight in StopLights )
    {
    thisLight.Image = StopLightImage;
    }

    StopLights[0].Position = new Vector2(5, 10);
    StopLights[1].Position = new Vector2(30, 40);
    }
    Then you would just need to draw them. Oh, and I just threw this out from memory, so it may not work exactly like this, but hopefully you get the idea.

     

    XNA QuickStart Engine | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 05-02-2008 12:59 AM In reply to

    Re: Forcing types of objects to not be cached

    Answer
    Jusy Lover:
    Perhaps I'm misunderstanding, but the Scene class does contain objects which have things like ExternalReference<TextureContent> inside of them.  So, when I load the Scene class, I also load the Textures that objects in the Scene load, and I don't know how to have the Scene sit in one ContentManager while the Textures that it references throught the content pipeline sit on another.  Therfore, the unload doesn't seem like it'd work for me - again, unless I'm misunderstanding.


    In that case don't use Unload. :) I would then suggest making your Scene class have a Clone method and going that route. Load it in and clone it. Then use the clone. Just make sure to only clone things that need cloning (such as starting positions of object). Things like textures, walls, static light positions, etc don't need to be duplicated unless you plan on changing them as well. Takes a little extra memory, but shouldn't be too much. Then you'll be able to revert as needed.

    Nick Gravelyn -- Microsoft XNA MVP
    Blog | XNA Wiki | FX-izer | EasyZip | Current Adventures | Next-Gen
  • 05-02-2008 12:59 AM In reply to

    Re: Forcing types of objects to not be cached

    Thanks, Lord Icon, but the problem is not how to draw Sprites - I'm well beyond that :)  It's that the data I'm loading contains Sprite positions.  So if I change these positions and try to load the data again, I don't get the original values - I get copies. 

    To really emphasize the point, the object that I'm dealing with is an object that is WELL AGED in the engine - meaning that there are many systems built around this thing so changing the functionality is really not an option.  It is coming down to being able to load the object without having it cached.
    --Vic--
  • 05-02-2008 1:00 AM In reply to

    Re: Forcing types of objects to not be cached

    Thanks Nick.  Sounds like this may be the way to go.
    --Vic--
  • 05-02-2008 12:11 PM In reply to

    Re: Forcing types of objects to not be cached

    Two options:
    • Load the object once, then give it a Clone method if you need multiple copies
      • More code for you to write
      • Most efficient as the data will only be read from disk once
    • Subclass the ContentManager to customize how it handles this particular type
      • See here for details
      • Probably less code
      • More disk IO
    XNA Framework Developer - blog - homepage
Page 1 of 1 (10 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG