Building from source

Hi, I’m trying to build MonoGame from source and use it in my project but I have run into some difficulties.

I cloned the latest version of the source from the github and built it using VS2019 which seems to have worked fine.
Now I want to add a reference to the .dlls to my project. The problem is that when I try to add them I get an error in Visual Studio that says “the reference is invalid or unsupported”.

I’m trying to add it by right clicking Dependencies and choosing “Add Project Reference” then browsing to the .dll. I’m a bit suspicious of that option because I remember previously this just being called “Add Reference” but there is no such option now.

Anyone have any idea how this is meant to work?

It’s probably better to add the MonoGame framework project to your solution, and then add a reference to that, rather than the dll. That way the MG source is available.

Could it be a mismatch of target frameworks. What’s the .NET target framework of your project, and which MG project are you trying to reference?

I think it’s “Add Reference” when you have a classic .NET Framework project, it changed with .NET Core.

It’s probably better to add the MonoGame framework project to your solution, and then add a reference to that, rather than the dll. That way the MG source is available.

I’d rather not because I don’t want everyone working on the project to have to download 1 gig worth of MonoGame source if they don’t need to.

Could it be a mismatch of target frameworks. What’s the .NET target framework of your project, and which MG project are you trying to reference?

My project is .NET Core 3.1 and trying to use the DesktopGL version of MonoGame.

Should I change my project to use .NET Framework? I’ve always been a little unsure of what the difference is between Framework and Core.

I’m assuming your project used the MonoGame NuGet package before and you removed that.

Once the NuGet reference is gone you should get an error message: “The MonoGamePlatform property was not defined in the project!”

I think this stops you from adding the MG dll reference. Try to add the MonoGamePlatform property to your csproj file and try again afterwards. The beginning of the csproj file should look something like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PublishReadyToRun>false</PublishReadyToRun>
    <TieredCompilation>false</TieredCompilation>
    <MonoGamePlatform>DesktopGL</MonoGamePlatform>
  </PropertyGroup>

No, unless you really need something that’s not part of .Net Core.

Also make sure you are referencing the .Net Standard dll for the MG framework, and not the .Net Framework one:
PathToMonoGame/Artifacts/MonoGame.Framework/DesktopGL/Release/netstandard2.0/MonoGame.Framework.dll

And of course the dependencies for the MG framework, that are also in this folder, need to be in your bin folder as well.

That seems to have fixed that problem, it now builds and runs.
But now I have a new issue: when it attempts to load any asset from the Content files it returns a null reference. This used to work fine when I was using the NuGet package but now that I’m loading from dlls it just cant find any of the content.
I noticed also that when I build it used to display messages in the output that it was building content files but now it doesn’t.

I’m guessing I’m missing some reference to the content system, any idea what that should be?

Yeah, I ran into this myself some time ago. When you remove the NuGet reference for MonoGame.Content.Builder.Task the link to the content builder gets lost. You should still be able to build content manually using the MGCB editor, but you can also fix this by adding some stuff to the csproj file.

I hope I’m getting this right. I just compared a csproj file, that I fixed up, to a newely created csproj file. It looks like I made two additions.

1.) Add a PropertyGroup for the MGCBPath. Change the path to wherever your mgcb.dll is. I used the debug version here, you can of course use release.

<PropertyGroup>
    <DotnetCommand Condition="'$(DotnetCommand)' == ''">dotnet</DotnetCommand>
    <EnableMGCBItems Condition="'$(EnableMGCBItems)' == ''">true</EnableMGCBItems>
    <MGCBPath Condition="'$(MGCBPath)' == ''">D:\MonoGame\Artifacts\MonoGame.Content.Builder\Debug\mgcb.dll</MGCBPath>
</PropertyGroup>

2.) Import the MonoGame.Content.Builder.Task.targets file, again adjust the path accordingly. This is also a child of the root Project tag.

<Import Project="D:\MonoGame\Tools\MonoGame.Content.Builder.Task\MonoGame.Content.Builder.Task.targets" />
1 Like

That fixed it, thanks :slight_smile: