Jump to content
  • Advertisement
Sign in to follow this  
MartinMM

New to XNA, how does Content work?

This topic is 3307 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have been using SDL in my game but i have know decided that i want to try to convert it to using XNA instead. It seems that it was harder than i anticipated. Most of the trouble comes from the content system. When i were using SDL i had all my content in a self-made resourcefile. The resourcefile were modified using my winforms gameeditor. Everything was nice and easy, i just selected an image/wave file and gave it a resourcename and saved it to the resourcefile. I also had a scripteditor which added xml to the resourcefile without any need to save it to a separate file and then add it to the resourcefile. Now i would like it to work the same way but instead be using a content project. Both my game and editor is now an XNA-windows project and i have no trouble loading content and use it in both the editor and game. The problem is that i have to add content using visual studio. Is there some easy way of doing the resource loading in my editor as before? And also directly adding my xml to the content, and not have to save my xml to a file, open up visual studio, and add it to the content file? The XML is not any kind of serialized object, its processed in the game using System.Xml.Linq.XElement etc.. I have googled a lot and i think i have to use MSBuild somehow, but i'm not really sure how it works.

Share this post


Link to post
Share on other sites
Advertisement
For simple xml files like that where you already have your parsers together, I've found that adding it to the project, but not compiling it simply having it copy to the output works quite well.

Another option here is to add a single XML file to the content project and just modifying that. It's not terribly complicated to write a custom content type writer for different types of items and if your XML is just string data than your pretty much golden. Doing it this way you don't end up distributing raw xml.

As for loading content into the content project outside of VS, It's probably possible, but I'm not sure how, or if it's necessarily a good idea.

Share this post


Link to post
Share on other sites
Thanks for the response.

If i add an xml file without compiling and only copy to output. How do i access it from my game project? Using the filepath? Or can i still use the contentmanager somehow and get an XElement object?

For your second idea, can i modify that xml at runtime? How do one do that?

I think i somehow dont understand what content projects are used for :) I understand the part about precompiling textures and models to get them in a more suitable format. But other than that i dont really know.

Share this post


Link to post
Share on other sites
Basically the whole point of the Content Pipeline is to take a whole bunch of raw assets and do some processing on them to get them into a format suitable for runtime. You mentioned textures and models, those are the two most common cases. You might also do something like saving your map file as an XML file, and then write a processor that parses the map file and also loads in any textures and models required by that particular map. It's mainly useful on the Xbox 360, where you don't have library for things like loading a PNG file or processing a mesh (and it was also take too long to do it there at runtime). It's also the only way to use certain XNA classes like Model or SoundEffect.

If you already had your own content-packaging system, I'm sure it would be a pain in the ass to move things over to a Content project. You might want to just consider using your old system...on the PC you're not really limited in what you can do so you could just unpack the file and load up the textures and sounds. Actually the sounds will be a pain since you can't create a SoundEffect without the Content Pipeline...you'd have to create an XACT project and load that which isn't much fun. You'd probably want to use an alternative audio library like FMOD.

If you do want to use a Content project instead, you can manipulate it programatically at runtime using your map editor. To that you use the classes in the Microsoft.Build.BuildEngine namespace, mainly the Project class. The Content project will be located in the "Content" subfolder of your project, and will be called "Content.contentproj". You then add new files by adding BuildItem's, and you can also add BuildItemGroup's (folders). Then you set some metadata to specify things like the path to the file, and which importer and processor to use. I wrote about it here.

If you have a file that you don't want processed by the content pipeline and you just want to load it at runtime (like your XML files), then you'll want to set it to not be compiled by the Content project and just copied to the output folder. At build time that file will then be copied to the corresponding folder in the "Content" folder of your project output folder (bin\x86\Debug or bin\x86\Release). You don't want to use ContentManager to load it though...that class can only load compiled .XNB files created by the content pipeline.

Share this post


Link to post
Share on other sites
Thanks alot! Does any documentation exist on what parameters you can send into the buildinfo object? I managed to create my own custom XDocument importer and with the help from you guys i have now solved most of the problems with saving content in my editor. My next problem is related to loading content in the editor :), is there any way of get a list of available content of a specific type? My editor has functionality to select a sprite and tie to a gameobject and i would like to select that from a list of all sprites. The ContentManager object does not have any suitable methods, and the only thing i can think of now is parsing the content.contentproject but it feels like there would be an easier way?

Share this post


Link to post
Share on other sites
There's tons of documentation on the Content Pipeline on MSDN...what you're looking form may be documented there but honestly there's so much that I couldn't tell you for sure. Also be sure to check out (XNA developer) Shawn Hargreaves's blog, which has a lot of informative posts about the Content Pipeline (he has a blog index here). Also when you're dealing with Content projects, a good way to learn how they work is to simply add or change stuff to them in Visual Studio and then look at what changes to the actual XML in the .contentproj file.

For enumerating compiled content, you can simply look for all of the .xnb files in a particular directory and then try loading them as type "object". From there you can just call GetType to figure out what Type of content it is. From there it's pretty easy to make a Control or Form that displays it all for you...in fact I did exactly that and posted it on CodePlex if you're interested.

Also keep in mind that at runtime you can build content into a xnb file if you want to...this sample shows you how to. Just be aware that the content pipeline assemblies are only installed on a PC when you fully install XNA Game Studio (it's not included in the XNA Redist, since the MSBuild stuff only gets installed with Visual Studio), so if you build this functionality into an app then you can't have it run on an end-user's PC.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!