Building and loading content at runtime?

How can I build content files (xnb) and load them at runtime, without adding them to the content manager upfront (lets say I want to allow users to add their own files after game release and game to adapt and load them dynamically)?

All the docs / old posts I found on this are either very old or lack in documentation… must have missed something :slight_smile:
Thanks!

Hey @GeonBit

You are looking for the MonoGameContentBuilder (MGCB.exe).

It should be located here:
%ProgramFiles(x86)%\MSBuild\MonoGame\v3.0\Tools

Please take a look at this post where I describe how to use it:

Also, when working with the MGCB I recommend taking a look at this:
http://www.monogame.net/documentation/?page=MGCB

Cheers! :slight_smile:

Using mgcb.exe is one way.

On the other hand you can e.g. load textures from .png-files as well (which is slower though). So you don’t really need to build the content.
Let’s say you want your player to be able to set his custom profile-picture. You could have a folder where the user can put .png-files that will be loaded at startup.

1 Like

This is also possible, but then he needs to manually dispose those textures, because they are not loaded and managed by the ContentManager.

I think it depends on what he is trying to achieve. If he wants to have full modding support for his game or if he creates an editor kind of software, I would recommend using the MGCB.

If it’s just a single picture like for a profile, then just loading the png from stream and manually dispose it later is of course simpler.

@BlizzCrafter @Finnhax thank you for the replies :slight_smile: @BlizzCrafter is close to what I want, not exactly an editor but sometimes that let users add whatever they like, sounds and models included.

So I understand my best shot is to just invoke the exe from my code, knowing it will only work on windows / linux / Mac? Is there a more “programmable” way to do it via one of MonoGame’s APIs?

Thanks :slight_smile:

It’s at least the most easy way :wink:

And yes, you can also program / integrate your own content builder. Just take a look at the source of the MGCB and the PipelineManager.

Just experimenting with the pipeline manager should give you a clue what is needed to compile your content.

You would need a reference to the MonoGame.Framework.Content.Pipeline.dll, which is in the same directory as the MGCB.exe.

Edit: Either way I think you are bound to Desktop platforms.

2 Likes

I know I’m rather late, but here’s a hacked-together example I used to play around with real-time effect compilation.

2 Likes

@BlizzCrafter @Optmisitic_Peach thank you both for the help :slight_smile:

1 Like

I am also a little late, I was on holiday, well that’s my excuse. :grinning:

Anyway, I had the same problem. The way I solved it was to write a class that extends ContentManager.

From within this you can still use pre-built content, but if the content is not there you can fall back to manual loading.

At the moment I am handling my custom content types this way instead of using a new content importer, and I handle textures by loading them into memory and then creating a Texture2D from the MemoryStream.

The reason I do this is two fold.

  • I am streaming content on a background thread.

  • The game is designed to be modded after release.

It actual fact it is very easy to do and although slower than using pre-built assets works incredibly well.

If you have problems send me a line.