IContenOwner question

Jul 2, 2010 at 6:42 PM
Edited Jul 2, 2010 at 6:49 PM

I have implemented the GameState-Management-Example from the Creators Club. But it has his own Input-Management and Content-Management. I will substitute this with the xen-parts now. But there is a problem. I don't understand exactly how IContentOwner works.

XNA Way: Every GameState hat his own ContentManager:

public override void LoadContent()
     content = new ContentManager(ScreenManager.Game.Services, "Content/Gameplay");
  gameFont = content.Load<SpriteFont>("gamefont");
... ((Game)ScreenManager.XenGame).ResetElapsedTime(); }

public override void UnloadContent()

XEN Way (???):
1) Implementing IContentOwner

IContentOwner.LoadContent(ContentRegister register, DrawState state, ContentManager manager)

     gameFont = manager.Load<SpriteFont>("gamefont");


AND ... how i dispose/ unload the Content?

IContentOwner.UnloadContent(ContentRegister register, DrawState state)


Jul 2, 2010 at 8:54 PM
Edited Jul 2, 2010 at 10:29 PM

You can still use the XNA ContentManager class just fine, in exactly that way.

IContentOwner classes go into a Xen.ContentRegister object (yeah, similar names :-) which is basically a higher level wrapper on XNA ContentManager. The reason it's a 'Register' and not a 'Manager' is that the ContentRegister doesn't own the instances it references - it stores weak references, so you don't have to explicitly remove items from the list and it won't keep objects alive.
ContentRegister has a Dispose method, which is equivalent to the Unload method in XNA.

IContentOwner exists purely to separate content ownership from existing hard coded classes such as XNA's GameComponent. You are not required to use it.

Hope that makes sense

Jul 3, 2010 at 10:57 AM

The first sentence is already sufficient, thank you. But still one last question: :-)

A weak-reference is a copy of a strong-reference and i can use this to restore a nulled strong-reference, when the waste collection was not faster. This means - i have no controll over the GC? Why then the UnloadContent-Function?



Jul 3, 2010 at 11:16 AM
Edited Jul 3, 2010 at 11:18 AM

There is a distinction between the content itself, and the objects referencing the content (content owners).
The weak reference is to the content owners, not the content itself. Due to how XNA is designed, you cannot safely dispose a single piece of content - such as a texture. You have to dispose/unload the entire ContentManager that loaded it.

This way, the ContentRegister will keep the content alive until it is disposed - but it will happily let the content owners get collected by the GC. In the case that the ContentRegister is disposed while some owners are still alive, then it calls Unload on the owners - signaling them that their references to textures, etc, will now be references to disposed objects. (However, I realise this isn't a likely situation, making the Unload method somewhat useless).

I'm half tempted to remove Unload from IContentOwner entirely.