Welcome to MSDN Blogs Sign in | Join | Help

Loading content from subfolders

A common pattern in XNA games is to nest a "Content" folder inside the main game project, to keep things organized. We do this in all the samples available on the creators.xna.com site, for instance.

When you come to load a piece of content, you have to write:

    Model ungulate = content.Load<Model>("Content/Dromedary");

or as some people prefer:

    Model ungulate = content.Load<Model>(@"Content\Dromedary");

Did you know there is an easier way? You can specify a root folder in the constructor where you initialize your ContentManager. Change this line from the standard game template:

    content = new ContentManager(Services);

to:

    content = new ContentManager(Services, "Content");

Now you can load content without bothering to specify the root folder:

    Model ungulate = content.Load<Model>("Dromedary");

So why don't we do this in our samples, or even build it in to the default game template?

Great question! I guess we just didn't think of it in time :-)

Published Saturday, June 02, 2007 11:12 AM by ShawnHargreaves

Comments

Saturday, June 02, 2007 6:56 PM by Mykres Space

# XNA Content Locations

Shawn has posted a nice quick one today on the location of Content in you XNA Games , and it is something

Sunday, June 03, 2007 2:11 AM by zygote

# re: Loading content from subfolders

it is something indeed! ;)

Tuesday, June 05, 2007 12:42 AM by Honmiz

# re: Loading content from subfolders

Thanks for your articles. I have learned a lot.

Thursday, July 05, 2007 1:04 AM by jumptack

# re: Loading content from subfolders

Is it possible to load a model outside of the content Manager?  Directly from the disk?  Thanks

Saturday, May 10, 2008 2:04 AM by Virtual Realm

# XNA Content Locations

Shawn has posted a nice quick one today on the location of Content in you XNA Games , and it is something that I all think we might be able to use. I do know that with the small project that I am working on, this is going to be another small piece of

Anonymous comments are disabled
 
Page view tracker