# XNA: Auto-populate content within the content project based on current folder/file structure and content management for large games

1) Is it possible to implement a system where I can simply drop a new image into my content project's folder and VS will automatically see that and bring it into the project for compiling?

2) Similarly, if I wanted a specific texture I could state something like var texture = Game.Assets.Image["backgrounds/sky_02"]; (where Game is the standard XNA Game class and Assets is some kind of content manager statically defined within Game). I know this is fairly simple to implement manually and have done such things in the past (static Dictionary defined within Game) except this only works for relatively small games where you can have all assets loaded at the start without much issue. How would you go about making this work for games where content is loaded and unloaded based on level / area?

I'm not asking for the solution, just how you would go about this and what things you would have to be aware of. Thanks.

• Why would you want to do this? The content manager adds some benefits, like compressing images into XNA formats (xnb) and stuff like that. – ashes999 Sep 4 '13 at 9:49

I had the same problem, I constantly have to add/remove items from the Content Project and it's a pain to keep it updated. It becomes especially difficult if you create the folder structure outside of Visual Studio.

The solution I went for was to create a tool that generates the Content project file for me upon request/build. This means that I can create assets in an application and place them in the Content folder hierarchy without having to manually add them to the Content Project.

What this tool does (heavily simplified):

1. Loads existing Content Project file as a XML document. Removes all nodes that reference files.
2. Scans Content directory for files and stores their paths in a collection.
3. ...
4. Adds all the files in the collection back into the XML document.
5. Saves the XML document back to disk and overwrites the old Content Project file.

Now, you could simply build and run the tool whenever you need it or you could add it as a pre-build event on your game project so that whenever you build your game, it will update the content project.

The advantage with this type of tool is that you no longer have to touch the Content Project anymore. Simply add/remove files/folders to/from the Content folder hierarchy directly.

UPDATE

Steps to use:

1. Place it where ever you like. You can include it in your project or use it as a standalone application.
2. Update the App.config file so it points to your content project file.
3. Run the project by either executing the executable or right-click on the project->Debug->Start new instance.

It is mandatory that the Content Project already exists, it will not create it for you.

• Are you able to share your tool for this and explain how to add this to a blank project? I have never done something like this before. – Joe Shanahan Oct 26 '13 at 20:55
• @Joe I've updated my answer for you – Dadgron Oct 27 '13 at 0:58
• Is there a reason the updater doesn't compile .mp3's or why it ignores .wav's completely? – Joe Shanahan Jan 29 '14 at 16:06
• @Joe I've set it to ignore .mp3 files by default since the compiling them to .xnb takes time and slows down the build considerably. To enable compilation, simply remove the line: tagName = "None" for .mp3 files in the UpdateFileSettings method. To include .wav files in the compilation, simply add another case statement: case ".wav" and set contentImporter = "WavImporter" and contentProcessor = "SoundEffectProcessor" – Dadgron Jan 30 '14 at 22:15
• Yeah I was able to fix them, to counter the compilation thing, I've edited the updater to only actually overwrite if there are any changes. As far as I know the content pipeline will only recompile changed content. Another issue that I can't see a work around to though, is that deleting content outside of VS will cause the code to be unbuildable (missing asset error), so it doesn't even get the chance to run any pre-build macros to fixup the content project. :( – Joe Shanahan Jan 31 '14 at 0:33

1) Should be possible, you need to compile your content to .xnb. You could etiher try to add a content pipeline extension which handles this for ou or take a look at the 'offline' content compiler project, which sure could be used to do this from code.

2) I have constructed a similar system some time ago, where I had content packages, which define the content needed at a certain point (level, game state, whatever you see fit). I would use an assertion like system to make sure the content is present when needed and at the same time backload the next packages. so while the player is playing level 1 the system will load level 2 as much as it could and when level 2 starts the game will show a loading screen until the package was fully loaded.

1) You can build assets at runtime if you have VS2010 installed.

There was a ContentManager project that scan a folder to find new files and builds them automatically at runtime, but I can't remember the blog where was the code.

But seems easy to code a standalone application to manage it, you need to use a FileSystemWatcher, built it again with a tool like the Xna 4.0 content compiler, or made by yourself (see Winform series )

And then if you have a Game running, you would comunicate both, the compiling tool and the game with an UDP socket to alert that assets have benn changed.

2) You can instance several content managers, so you can create a content manager per level, although it implies tht some assetes can be duplicated.

But there is a project named Content Tracker at codeplex that lets you know the references count of an asset and unload it when there is no references to it, so it's easy to implement a solution for what you want.

This is the link to the Xna 4.0 to the Content Tracker project