0

First I would like to state that this is not a Content.RootDirectory = "Content"; problem. I've tried this with and without that line of code. I have a class called FileFactory that has three dictionaries. One for textures, one for sounds, and one for sprite fonts. I also have other factories that load in XML files for other types of objects and those work perfectly. Here's the code that loads the fonts:

        public static void LoadFonts(ContentManager content)
    {
        string[] filePaths = Directory.GetFiles(@"Content\fonts\");
        string key;

        foreach (string s in filePaths)
        {
            key = Path.GetFileNameWithoutExtension(s);
            Fonts.Add(key, content.Load<SpriteFont>(s));
        }
    }

The error it gives is: "Error loading "Content\fonts\ammoFont.xnb". File not found." even though the file is CLEARLY there considering it would have to be in the directory to be added to the filePaths array as a string. I've even checked the folder and it is there. Here is the full error:

Microsoft.Xna.Framework.Content.ContentLoadException was unhandled
Message=Error loading "Content\fonts\ammoFont.xnb". File not found.
Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject) at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName) at OfUnknownOrigin.FileFactory.LoadFonts(ContentManager content) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\FileFactory.cs:line 38 at WindowsGame1.UnknownOrigin.LoadContent() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 110 at Microsoft.Xna.Framework.Game.Initialize() at WindowsGame1.UnknownOrigin.Initialize() in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Game.cs:line 102 at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun) at Microsoft.Xna.Framework.Game.Run() at WindowsGame1.Program.Main(String[] args) in C:\Users\Chris\Documents\Visual Studio 2010\Projects\WindowsGame1\WindowsGame1\WindowsGame1\Program.cs:line 15 InnerException: System.IO.FileNotFoundException Message=Error loading "Content\Content\fonts\ammoFont.xnb.xnb". File not found. Source=Microsoft.Xna.Framework StackTrace: at Microsoft.Xna.Framework.TitleContainer.OpenStream(String name) at Microsoft.Xna.Framework.Content.ContentManager.OpenStream(String assetName) InnerException:

Answer: Change this code:

Fonts.Add(key, content.Load<SpriteFont>(s));

to this:

Fonts.Add(key, content.Load<SpriteFont>("fonts\\" + key));
Bagofsheep
  • 159
  • 1
  • 1
  • 8

2 Answers2

0

Game content paths must be relative to your Content.RootFolder ("Content\" by default) and must not contain file extension (so avoid duplicate file names, even if the extensions are different).

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • The paths are relative to that. I've tried this with and without setting a Content.RootFolder and the same thing happens. The path for Directory.GetFiles is not relative to the Content.RootFolder though. I've fiddled with the code and string layout to no avail. – Bagofsheep Feb 09 '13 at 02:00
  • You include file extensions in your paths, you must remove those extensions. – user1306322 Feb 09 '13 at 03:21
  • Look at the code above. I haven't added any file extensions into the paths. – Bagofsheep Feb 09 '13 at 19:14
  • @Bagofsheep I quote your question: `The error it gives is: "Error loading "Content\fonts\ammoFont.xnb". File not found."` – user1306322 Feb 10 '13 at 01:28
0

From the exception message, it looks like the string you are calling it with is wrong. Content is in there twice, and the extension is there twice:

Error loading "Content\Content\fonts\ammoFont.xnb.xnb"

You didnt post the code where you initialize the array, but if you have them in the definition, remove them.

Unicorno Marley
  • 1,744
  • 1
  • 14
  • 17
  • Here is the Dictionary for the fonts being initialized: `public static Dictionary Fonts = new Dictionary();` I can't figure out why Content and the extension are there twice. – Bagofsheep Feb 09 '13 at 05:33
  • Try `Fonts.Add(key, content.Load("fonts\\" + key));` – Unicorno Marley Feb 09 '13 at 07:13
  • Ok, that seems to have worked. I'm not exactly sure why though. Thank you! – Bagofsheep Feb 09 '13 at 19:17
  • Its because `Directory.GetFiles(@"Content\fonts\");` gives you back the _full_ file path including the extension, so in this case `Content\fonts\ammoFont.xnb`. Load is expecting just the path from root and the file name, and preppends the root directory (`Content\`) and appends the expected extension (`.xnb`). You already got just the file name it wants in your `key` definition, so you can just feed Load the directory name (`fonts\`) and the file name (`key`) and it does the rest for you. – Unicorno Marley Feb 09 '13 at 20:34
  • Yeah, after examining the code more I realized that. Thank you for the explanation! – Bagofsheep Feb 10 '13 at 06:09