LoadGraphicsContent for Game and Component - What is the best practice...

Hi,

I've followed the spacewar example and put the call tonew ContentManager in theLoadGraphicsContentof the Game. Because I need to use the content manager in some of my components I have made it a static property on the top level game. I can call now call upon the content manager from theLoadGraphicsContentmethod within my components and load textures and stuff as required.

However, it seems that theLoadGraphicsContentof the components are being called before the one in the Game.

What is the best practice with regards to setting up the ContentManager, and using it within the Game and Component'sLoadGraphicsContent.

Any ideas?

Kind Regards,

James

[968 byte] By [jxl98c] at [2007-12-27]
# 1
Since the content manager is needed for initialization of the game (and it's components) I would put it in the game class's constructor, where it normally is if you use the Starter Kit. The LoadGraphicsContent method is meant to be used if your content needs reloading (changing game resolution for example which I believe resets the graphics device, which hoses your graphics content). You probably don't want to initialize the content manager there.
JimPerry at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 2

Hi Jim,

Are you looking at the same starter kit as me? As I said in my original post, and the main reason I asked was because it was not in the constructor in the Spacewar Starter Kit and it is in the LoadGraphicsContent. I do not mind creating it in the constructor if that is where it needs to be done but I'd like to know why.

Is this a mistake in the starter kit or not? If not, then how do I solve it for my problem.

As far as I can tell, Spacewar DOES create a new ContentManager for every device reset / create which is the opposite to what you have said.

Kind Regards,

James

jxl98c at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 3
In fact, it is absolutely necessary to (re)create a new ContentManager on device create/reset events since the content manager relies on a graphics device (which is lost in these events).

To answer your original question, it's absolutelt fine to create a local content manager in your game component. Since game components get passed a Game parameter in their constructor, you can hook your local content manager back into the games services...

MattDavey at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 4
Hmm, didn't think about that. The Windows Game Starter Kit should be changed then since it creates the content manager in the constructor. That's the Starter Kit I was talking about above.
JimPerry at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 5
I don't think its necessary to create new ContentManager instances when the GraphicsDevice is lost. The ContentManager accesses the GraphicsDevice through a game service, IGraphicsDeviceService, and the usual implementation of that, GraphicsDeviceManager, provides a current GraphicsDevice.

For much the same reasons the Game class doesn't have a direct reference to the GraphicsDevice but instead always goes through the GraphicsDeviceManager. When you access the GraphicsDevice in your code with something like 'this.graphics.GraphicsDevice', that extra level of indirection is protecting you from all sorts of GraphicsDevice management problems... and you thought they just liked making you type "graphics" lots :)

Cheers,
Leaf.

Leaf. at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 6
Well, changing the resolution in the middle of a game doesn't affect it when the content manager is created in the constructor so my original thought stands. I'm not sure how else I can test a lost GraphicsDevice.
JimPerry at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 7

Jim Perry wrote:
I'm not sure how else I can test a lost GraphicsDevice.

Change the bit depth or multi-sampling values.

ProfEclipse at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 8

Jim Perry wrote:
Hmm, didn't think about that. The Windows Game Starter Kit should be changed then since it creates the content manager in the constructor. That's the Starter Kit I was talking about above.

No it does not!!!!!

jxl98c at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 9
jxl98c wrote:

Jim Perry wrote:

Hmm, didn't think about that. The Windows Game Starter Kit should be changed then since it creates the content manager in the constructor. That's the Starter Kit I was talking about above.

No it does not!!!!!

Huh?

public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManger(Services);
}

That's the standard code created when you use the Windows Game Starter kit.

JimPerry at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 10
The documentation definately needs to clear this up.

Everybody is doing it differently.

vidalsasoon at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 11
Since SpaceWar was built before beta 1 was ready I'm not surprised it doesn't do things like the other Starter Kits. I guess as long as your code works, it's not a huge deal.
JimPerry at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 12

I see what you mean now. You were throwing me off by calling it a 'Starter Kit'...

The 'Windows Game' project does it as you say.

The 'Spacewar Windows Starter Kit' does not.

I'm doing it in the constructor and it work, but I'm not happy that I do not understand whether this is correct or not.

Cheers,

James

jxl98c at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 13

Jim Perry wrote:
Since SpaceWar was built before beta 1 was ready I'm not surprised it doesn't do things like the other Starter Kits. I guess as long as your code works, it's not a huge deal.

I did think of that but the ContentManager was not in beta1 so it has been added to Spacewar specifically for beta2.

jxl98c at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...
# 14
jxl98c wrote:

I'm doing it in the constructor and it work, but I'm not happy that I do not understand whether this is correct or not.

If the Windows Game project is doing it that way, then it is correct. The SpaceWar starter kit was written before the ContentManager was made available as Jim said. It probably wasn't clear at the time the starter kit was updated to Beta 2 which way was correct.

ProfEclipse at 2007-9-4 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,XNA Framework...