Antoine Dahan
Antoine Dahan

Reputation: 713

XNA 4.0 ContentManager Issue

What exactly does this do?

Content.RootDirectory = "Content";

And how does it relate to:

player.Initialize(Content.Load<Texture2D>("player"), playerPosition);

Thanks.

Upvotes: 0

Views: 804

Answers (3)

Darren Reid
Darren Reid

Reputation: 2322

I'm going to assume that your Content object is actually a ContentManager that is used to load assets.

The first line sets the default directory from where your assets in your Content Project are being loaded.

Content.RootDirectory = "Content";  

This can avoid always having to specify the full path including folders if your content is else where in your projects structure.

A good place to start to understand how to use these might be reading some documentation. The ContentManager making Loading Content quite simple. A good idea to make it easier to read your code is to specify the type of the content you are loading as well eg,

player.Initialize(Content.Load<Texture2D>("player"), playerPosition);

This makes it easier to understand what is actually being loaded as file extensions are ommited when loading assets.

Hope that helps.

Upvotes: 2

Lukasz Madon
Lukasz Madon

Reputation: 15004

Content.RootDirectory = "Content"; 

It sets the root directory for ContentMaganer. In your XNA solution you usually have 2 projects. One is the project where all your logic is and second is the content project where you put all textures, fonts, sounds etc. If you set that Content manager will look there to load the assets.

player.Initialize(Content.Load("player"), playerPosition);

Here ContentManager of the game is loading player for player initialization. If you set the RootDirectory then ContentManager will load Content.Load("Content/player")

Upvotes: 1

Inisheer
Inisheer

Reputation: 20794

Content.RootDirectory = "Content";

"Content" is the folder/directory where image and model assets are physically stored. There should be a directory in your solution automatically created with a new XNA solution.

player.Initialize(Content.Load("player"), playerPosition); 

Here, a player (custom class I assume) is being initialized with an asset name ("player") to display, and it's position in 2D/3D space.

In essence, the first code is telling the compiler where all the content lives, whereas the second piece of code is loading the asset for the player class.

Upvotes: 1

Related Questions