Hardycool200
Hardycool200

Reputation: 21

Monogame: Could not load asset as a non-content file

I'm just getting started with MonoGame, and whenever I run my program I get a Could not load asset as a non-content file error. I've been searching the web and haven't found anything on my problem. please help!

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MoveDemo
{

public class MainGame : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D mainmenu;

    public MainGame()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        mainmenu = Content.Load<Texture2D>("MainMenu"); 
    }

    protected override void UnloadContent()
    {
    }


    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();


        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        spriteBatch.Begin();
        spriteBatch.Draw(mainmenu, new Rectangle(0, 0, 840, 480), Color.White );
        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

Upvotes: 2

Views: 1520

Answers (2)

PowerUser
PowerUser

Reputation: 812

You certainly can do it like @SBurris says, but I will suggest a simpler and overall better solution to the problem:

The cause:

So when you try to debug your project in Visual Studio, you get a ContentLoadException which tells you that an asset can not be loaded as a non-content file. What is a content file, you ask? A content file is a binary file containing the compiled asset and has an .xnb extension. When specifying the path to your asset (e.g. a texture) like this:

protected override void LoadContent()
{
    this.Content.Load<Texture2D>("path/to/myTexture");
}

You aren't actually linking to the .png file of your texture, rather you link to the .xnb content file which holds the data you require. The exception simply tells you that no content file (.xnb) was found in the specified directory.

The solution:

So to fix this, simply open the MonoGame Content Builder/Pipeline tool, which resides in a folder called Content in your project's directory. The pipeline tool itself is also called Content (or Content.mgcb) and has MonoGame's orange logo as it's icon.

Once you've done that, add the asset you need to your Content Project (by default called Content) by right clicking on it -> Add -> Existing Item.

And then you save and done! Wrong. In order to have the content file compiled and ready for use, you need to create it first. That's done by clicking on the Build menu and selecting Build or alternatively, press F6 on your keyboard.

This will compile the asset file into a content file for you and your game will be ready to read it.

XNA:

When I first started with MonoGame, I had experience with Microsoft's XNA and when I found out that MonoGame utilizes a different approach to adding assets to a project, I had the same problem like you. That is because I was used to the XNA way which allowed me to simply Add -> Existing item and forget about it -- Visual Studio would compile the asset files automatically for me. Well, that is no longer the case with MonoGame so remember to build your Content Project from the Content Pipeline tool!

Cheers!

Upvotes: 2

SBurris
SBurris

Reputation: 7448

Did you tell Visual Studio the MainMenu was content and to copy it?

enter image description here

In my case, since the CarBlue is in a Graphics folder under content I load it with this line:

blueCarSprite = Content.Load<Texture2D>(@"Graphics\CarBlue");

Upvotes: 1

Related Questions