Is it possible to use monogame in VS code?
Asked Answered
C

4

10

I am using an iball notebook and I don't have enough memory to install Visual Studio. I am having no problems using VS code and i am able to make and create executables of my console applications. I need to learn game development but everyone keeps saying that i need to install visual studio for it

However i did find a fourm on monogame on the topic and found that it is possible(At least on Linux) to use Monogame in VS code.

http://community.monogame.net/t/visual-studio-code-and-monogame/2371

Please Help me out.I want to know if it's really possible to compile and run a monogame app in windows.

Ceratoid answered 17/12, 2016 at 11:58 Comment(1)
I'm confident this is possible. I'll give it a try when I get a moment and report back (unless someone else answers sooner). You just need to get the proper nuget packages restored and then hookup the MGCP tool.Dreyfus
C
12

Answer edited as Monogame released official dotnet project templates

I finally got it working.

I realized that all I needed was to create a monogame project (*.csproj) and Compile/Build it without Visual Studio. VS Code is just a feature-rich text editor and I would need other toolset for it.

MSBuild tool is used to Compile/Build monogame project and is available as a CLI. It is available without installing Visual Studio. For C# project building, dotnet core is required. executing the script

dotnet new [Template]

creates a new Project. We need to add a template for monogame here.

As per the latest update by the Monogame Team, you can install the templates by executing

dotnet new --install "MonoGame.Templates.CSharp"

Use the script

dotnet new -h

to check out all the templates available.

Now, to generate the project, use the following

dotnet new mgwindows

On successful execution, this will generate [FolderName].csproj, Game1.cs, Program.cs and other files/folders of monogame project. Please not that this csproj is on .NET Framework (version 4.5 if I'm not wrong....) and therefore it might not work with dotnet run command. (If you're a bit stubborn, you might need to copy the Monogame installed folder(which contains, among many other files, Monogame.target file) in your dotnet installed folder.)

In other words, use msbuild to build and run the project

msbuild

If the program does not contain any compile time errors, the .exe file will be built successfully and you will get to see the the Output file path which you get to execute.

If you're working on Linux or have some other reason not to use MSBuild, you should not generate a mgwindows project. You can rather chose

dotnet new desktopgl

which works on dotnet core (i.e you can use dotnet run command to execute it).

Ceratoid answered 31/8, 2018 at 3:13 Comment(3)
I get this error C:\Program Files\dotnet\sdk\3.0.100-preview-009812\MonoGame\v3.0\MonoGame.Content.Builder.targets(40,5): error MSB4062: The "MonoGame.Build.Tasks.CollectContentReferences" task could not be loaded from the assembly C:\Program Files\dotnet\sdk\3.0.100-preview-009812\MonoGame\v3.0\MonoGame.Build.Tasks.dll. Could not load file or assembly 'Microsoft.Build.Utilities.v4.0, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specifiedTonry
@KALEIDAWAVE Hi. I think either you are using dotnet core for a project that is supposed to build to .NET Framework (for which you should use MSBuild) or have some errors on installing Monogame. Anyways, I have updated the answer which I should have done long ago given the Monogame Updates, so please try that.Ceratoid
Yes can confirm "desktopgl" works with dotnet and the dotnet run command. Before you run make sure you change the namespace of Program.cs to match the one of Game1.cs as there is a mismatch bug.Tonry
H
7

I wrote this (Windows-only) solution in medium. It's a step-by-step of how to install and run dotnet with MonoGame in the terminal of VSCode.

You need to install:

  • .NET SDK 5.0
  • .NET Core SDK 3.1
  • .NET Runtime 5.0

You can run dotnet in your terminal and see if it's working.

  1. Install MonoGame editor:

    dotnet tool install --global dotnet-mgcb-editor
    

    and

    mgcb-editor --register
    
  2. Install MonoGame Templates:

    dotnet new --install MonoGame.Templates.CSharp
    
  3. Create a new project in the chosen template:

    dotnet new mgdesktopgl -o ProjectName
    
  4. Enter in your project with cd ProjectName and add the MonoGame package to it:

    dotnet add package MonoGame.Framework.DesktopGL --version 3.8.0.1641
    
  5. And finally:

    dotnet run Program.cs
    
Hunks answered 8/4, 2021 at 10:18 Comment(3)
The medium article works fine for linux tooDunghill
Fixed my problem under Ubuntu 18.04Tomtit
Needed to install .NET Runtime 3.1 alsoTomtit
D
0

There is absolutely no reason you cannot work with MonoGame from Visual Studio Code. It will not be an optimal setup since you'll lack debugging, and the setup will be difficult, but if you're okay with that then continue on.

You've already noted that you have no issues creating executable console applications. This is all you really need to be able to do. The key here is that you must build targeting .NET4+ or Mono. If you've followed tutorials that lead you to building .NET Core applications they will not work with MonoGame (at this time). If you are building .NET Core, spend some time looking into how to build Desktop CLR applications using MSBuild or Mono. If you need more information I can expand upon this. You'll also need to be sure you know how to reference other .NET assemblies from your console applications. Please do some research on how to do this before moving on.

For Windows you have the option of targeting DesktopGL (OpenGL) or WindowsDX (DirectX) versions of MonoGame. I'm partial to the DirectX versions myself. You'll need 2 things to get up and running: 1. the MonoGame assemblies, and 2. the MonoGame Pipeline Tool (this is used to compile your content into .XNB files so they may be imported into your game).

To get at MonoGame's assemblies and tools the easiest way I can think of is to install Visual Studio Community Edition and then download and install MonoGame for Visual Studio. This will bring all the tools to you. You'd then need to look at "C:\Program Files (x86)\MonoGame\v3.0\Assemblies" for the appropriate assemblies and "C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools" for the MonoGame Pipeline Tool.

If Visual Studio will not let you install because your machines doesn't meet the requirements then you are not out of luck. The assemblies can be pulled in via nuget. Download the latest nuget.exe here: https://dist.nuget.org/index.html and then run: nuget.exe install MonoGame.Framework.WindowsDX or nuget.exe install MonoGame.Framework.DesktopGL. This will create a directory containing a lib folder that contains a net40 folder which contains the .DLL files you need. For WindowsDX I think you'll also need the DirectX runtime https://www.microsoft.com/en-us/download/details.aspx?id=34429. For OpenGL I think you'll need OpenAL (for audio) https://www.openal.org/downloads/.

Once you have the assemblies you'll need to reference them when you build your code. As you've said you're already familiar with creating and running console applications I'll assume you know how to do this. Just reference every managed .DLL you downloaded with Nuget or pulled from the Assemblies folder from the install.

To test things out, drop this into a .cs file, reference the MonoGame assemblies in your build, build it as you would a console application, and execute:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;

public class Game1 : Game
{
    GraphicsDeviceManager graphics;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
    }

    [STAThread]
    static void Main()
    {
        using (var game = new Game1())
        game.Run();
    }
}

You should get a window with a cornflower blue background. If you don't, then you're not building or referencing things right, or you're missing a dependency.

The trick now is getting your hands on the Pipeline Tool, and MGCB.exe. If you were able to install MonoGame for Visual Studio, great!, these files are in the folder I references above. If not, for whatever reason the MonoGame project doesn't distribute stand alone versions of these, only with the installer. I've taken the contents of what you need and plopped it into a dummy release on GitHub here: https://github.com/srakowski/derp/releases/tag/MG. Download the Pipeline.zip file, extract it, and you should have what you need.

Create an empty Content.mgcb file and open it with Pipeline.exe. You should be able to add and build content files. You'll need to copy these files into the same directory where your .exe lives. Commonly, these are put into a Content folder, and Content.RootDirectory = "Content"; is added to the Game's constructor.

Once you get all this working you should be free and clear to create games as your heart desires. Please let me know if you have troubles and we'll work things out.

Dreyfus answered 21/12, 2016 at 2:29 Comment(5)
I get the feeling this will work. There's a small problem now. I've almost added the assemblies of monogame but when I try to restore them I get an errorCeratoid
Package MonoGame.Framework.WindowsDX 3.5.1.1679 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package MonoGame. Framework.WindowsDX 3.5.1.1679 supports: net40 (.NETFramework,Version=v4.0) One or more packages are incompatible with .NETCoreApp,Version=v1.0.Ceratoid
What to do now?Ceratoid
As noted you are using a .NET Core application, which will not work with MonoGame. You'll need to study up on using normal Desktop CLR .NET.Dreyfus
I did a lot of workaround and suddenly i realized of a horrible fact... I'm using a 32-bit System. So i dug around and almost made a Monogame exe but at the end it showed me 64 bit compatiblity issue.Ceratoid
G
-1

I've tried Monogame on Visual Studio and own a Windows PC. So I can safely confirm that Monogame does work on Windows. To answer your question.

However, if you want to try it without Visual Studio, then I don't think you can really get far. as there are several build in tools needed to make a decent one. And you cannot debug it either. You're working really limited if you cannot use Visual Studio.

Try to clean up your PC to make some free space. Visual Studio would really be a better choice to work with.

Graviton answered 19/12, 2016 at 10:49 Comment(2)
Correct me if I'm wrong, but I believe that I need two things to obtain a running executable of a monogame app via VS code : The monogame library and the compiler for (at least) DirectX. Which of these things is not possible in vs code?? BTW I'm fine with the fact that I can't debug in VS code (for now...).Ceratoid
I'm sorry, but I can't really answer that as I've not worked outside of Visual Studio.Graviton

© 2022 - 2024 — McMap. All rights reserved.