Spritefont / Pipeline confusion!

Hi folks, been away for a while but trying to get back into the swing of things. Today I’ve been struggling with spritefonts, I’ve been searching for a web page that explains the process from start to finish, but I’ve drawn a blank, every tutorial I can find either seems to start half way through the process or isn’t clear at all. I’m sure it is to people who already know what they’re doing.

If I understand things correctly so far, a .spritefont file is a simple xml document that names the font and the size? That’s then imported into the pipeline tool which adds a sprinkling of pixie dust, performs some magic, and then you have a font you can use. You see, I haven’t got a clue!

Could someone please point me to a tutorial which covers the complete process, including how the font is finally .load(ed) into the project?


You do have a clue, because your understanding is spot on.

Say you have MyFont.spritefont in the content project. Once the font is processed, at runtime you use the ContentManager to load it.

  font = Content.Load<SpriteFont>("MyFont");

Then use SpriteBatch.DrawString to draw text.

Thanks for getting back to me Konaju, I’m glad I’m not driving down a dead end road with a deep lake at the end!

So as far as I can see everything is setup fine, no errors from Pipeline, but I can’t get anything to load. The Content.mgcb file is in the right pace and set to always copy, but I get the silly - Unable to load non content file message.

I’ve opened another topic for it with the example code I’m using.

So at least I’m making progress!

The xml is a little bit more than that. It has the selected font’s name in it, there are ranges for characters the font can display, there is a font size and lots of other properties.

The xnb file that is generated from the font - as far as I know - contains both this description and the font, that is why when you would like to use a brand new font, you need to only install it on the developer’s machine, create the xnb, and job’s done, you can then use the font wherever you want.

An important thing that usually everyone misses is, that when you load your font, you must not specify the extension in the code, so:
SpriteFont myFont = Content.Load(“NameOfYourFontWithoutExtension”); //e.g. “Arial”, if you have loaded the Arial.xnb into your project.

You also need to set Content property in the properties window, and the “Copy if newer” flag.

When you say loaded the xnb file into your project… When Pipeline has been run and the project is built, surely all the graphics and fonts are built into the mgcb, I don’t need to load extra files into the Content folder?

Even if I did that wouldn’t explain why I can’t get graphics to ‘Content.load’ using the mgcb.

Just a thought, the VB template I’m using for this first project was kind of cobbled together, it didn’t have a Content.mgcb file in place so I had to create it, then add it to the content folder. Is there any reason why this wouldn’t have worked properly?

Yeah, .mgcb files should not be loaded into your project and copied to output. They are the equivalent of the old Xna .contentproj files and Visual Studio does not understand them. Use pipeline.exe to open them and build them (or mgcb.exe if you use command line to build) and the Xnb files will be compiled for each input content file specified in your .mgcb file.

And you can set the output folder of the .mgcb to be your game bin Content folder meaning no manual copying if Xnb files. Good luck!

P.S. I used to use VB6 and VB.Net but once I learnt C# I didn’t look back. Not saying it’s bad, just the sheer volume of Xna / MonoGame samples in C# make a big difference.

Yeah, I hear a lot of people saying the same. I can transpose C# pretty easily so the examples are easy to work with, I just have awful difficulty debugging C# with all the brackets. I may get round to it one day.

Anyway, it then looks as if the Pipeline is doing exactly what it should, I have a Bin folder underneath the Content folder, and my Two folders under the Bin with my files in. But Content.Load still fails.

You need to make sure the output xnb files are making into your games output bin/x86/Debug/Content folder, or whatever it is depending on your target platform and Configuration.

Ah, now, I can see how that might make a small difference!

Well Blow My Socks! That works!

So, there are no settings anywhere to say copy these to the output directory?

The pipeline tool has settings for that… click on the root node of the tree view and peruse the property grid. I think you can use “{Configuration}” in the relative path so it substitutes “Debug” or “Release”. The output path is relative to the .mgcb file. It should also be possible to set up a post build step to run mgcb.exe on it, automating the content build from Visual Studio.

Thank you. I don’t know why I hadn’t seen that.

Tested and working fine.

It would be nice if we have the ability to rename files in the pipeline tool.

There is a PR for adding the Asset name property, same as in XNA. We’ll get this fixed up and merged after the 3.5 release.

1 Like