2
\$\begingroup\$

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.

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

3 Answers 3

1
\$\begingroup\$

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

As requested, the project can be downloaded from here: ContentProjectUpdater

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.

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 26, 2013 at 20:55
  • 1
    \$\begingroup\$ @Joe I've updated my answer for you \$\endgroup\$
    – Dadgron
    Commented Oct 27, 2013 at 0:58
  • \$\begingroup\$ Is there a reason the updater doesn't compile .mp3's or why it ignores .wav's completely? \$\endgroup\$ Commented Jan 29, 2014 at 16:06
  • \$\begingroup\$ @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" \$\endgroup\$
    – Dadgron
    Commented Jan 30, 2014 at 22:15
  • \$\begingroup\$ 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. :( \$\endgroup\$ Commented Jan 31, 2014 at 0:33
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$
  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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .