Skip to content

Instantly share code, notes, and snippets.

@31
Last active November 10, 2023 23:15
Show Gist options
  • Save 31/302bab2630ddf8d90ab4aec0d358b538 to your computer and use it in GitHub Desktop.
Save 31/302bab2630ddf8d90ab4aec0d358b538 to your computer and use it in GitHub Desktop.

Using Visual Studio to debug C#/.NET code in Godot

I highly recommend reading the official page about using Visual Studio with Godot first. It may be more up to date than this doc, and has been past review.

If you have any problems or questions, come visit the C# channel in the Godot Discord to chat.

To debug C#/.NET code in Godot:

Godot 3.x (3.2.3 or later)

Visual Studio 2019: Click here to expand instructions for a workaround to use the VS extension with Godot 3.x. These steps don't work with Godot 4.x, and they don't reliably work with VS 2022.

Godot 3.x in Visual Studio 2019

This is a step by step guide to using this workaround for Godot 3.2.3+ support in Visual Studio: godotengine/godot-csharp-visualstudio#10 (comment)

  1. Install the latest 1.x version of the Godot VS extension from https://github.com/godotengine/godot-csharp-visualstudio/releases.

  2. In the solution explorer, add a new project to your solution.

    image

  3. Search for library .net framework to find the "old-style" project style. Select it and press Next.

    image

  4. Set up the project.

    • Name: StubProj
    • Location: make sure this is your project directory.
    • Framework: the default is fine, we're going to replace this later anyway.

    image

  5. Open the StubProj directory. You can use Open Folder in File Explorer to do this easily.

    image

  6. Close Visual Studio. Press Yes if prompted to save files.

  7. In the StubFolder directory, delete all files and directories except StubProj.csproj. (Delete bin, obj, Properties, Class1.cs.)

  8. Open StubProj.csproj in a text editor. (Not Visual Studio.) Delete the content, then paste this text (a slightly modified version of the original workaround)

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{B83A5A30-7D9A-4BCC-BB56-2974A2AAE767}</ProjectGuid>
        <OutputType>Library</OutputType>
        <RootNamespace>StubProj</RootNamespace>
        <AssemblyName>StubProj</AssemblyName>
        <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <Deterministic>true</Deterministic>
        <ProjectTypeGuids>{8F3E2DF0-C35C-4265-82FC-BEA011F4A7ED};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <ProjectReference Include="..\*.csproj">
          <Private>False</Private>
        </ProjectReference>
      </ItemGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    </Project>
  9. Start Visual Studio. Open your Godot project back up.

  10. Set StubProj as your Startup Project.

    image

  11. See the extension's debugging options are there!

    image

Visual Studio 2022: There is no widely known way to make VS 2022 work with the extension that allows C# debugging in Godot 3.x. See godot-csharp-visualstudio#28

If you install Visual Studio 2022 and Visual Studio 2019 on the same machine, the Godot "External Editor" option will pick 2022. To make Godot open 2019 for you, you can replace the GodotSharp\Tools\GodotTools.OpenVisualStudio.exe file in your Godot installation with a new program that prefers 2019 rather than 2022. To do this, modify and recompile GodotTools.OpenVisualStudio. A user on Discord has done this successfully and made their copy available as an attachment. (As always, be careful with programs created by others online!)

Godot 4.x (4.0 beta or later)

In 4.x, Visual Studio 2019/2022 now works without any extension!

Godot 4.x changes to use .NET [Core] Runtime rather than Mono, so you can attach to a running Godot game with the VS debugger, or set up a launch profile with Godot.exe to start the game and start debugging when you run your project in VS. Until this is documented, for now, here's an example of how to set that up: https://www.reddit.com/r/GodotCSharp/comments/xgpqfh/oc_rundebug_godot4_c_projects_from_visual_studio/.

@fhgaha
Copy link

fhgaha commented Mar 11, 2023

This is helpful

@andreisbarnea
Copy link

Workaround mentioned for VS2022 works but with one additional note: Remove existing VS extension if it was previously installed.

@chaojian-zhang
Copy link

Workaround mentioned for VS2022 works but with one additional note: Remove existing VS extension if it was previously installed.

Tried this and still cannot set breakpoints. Looks like there is the error "No CLR has been loaded" - any ideas?

@littleclamwyl
Copy link

littleclamwyl commented Sep 11, 2023

[Just For Tips:] if you use VS2019, follow the blogger 31's guide and run debug mode, and run into error like "Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)", you need to: right click StubProj --> Add references --> Project to choose your godot project. And then, the break points work.

If you use VS2022, just do as the VS2019 guide do, I come into an error " method not found:" void mono.debugging.visualstudio.startinfo..ctor and ......", unfortunately, I fail to fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment