Using C# and Visual Studio Code in Godot 4

Godot supports GDScript, C# and C++ as the scripting language. Godot also allows you to mix and match different languages in a single project. In this tutorial, we will see how to set up C# and VS code for Godot 4.

Word of Caution: Godot 4 does not support export to Android, IOS and Web platforms if the code is written in C#. This may change in future, so keep an eye out at the official docs. You can use Godot 3.x with C# if you want to export to the above platforms.

If you are switching from Unity to Godot, then check out our getting started with Godot for Unity users.

Download Godot Dot Net version

Godot game engine is available in two different versions. One is Godot with GDScript and the other one is Godot Dot Net version. If you have downloaded the regular Godot version, then go the download page and download Godot Dot Net version.

Don’t worry, any project that you have created with the regular version will work with the Dot Net version too.

Different Godot versions

Installing Dependencies

For Godot 4 to fully integrate with Visual Studio Code, you need the following packages

  1. Visual Studio Code
  2. Dot Net 7.x SDK(do not download Dot Net runtime)
VS code download page

Installation of both VS code and Dot Net SDK is very simple. Just follow the on screen instructions and it will get installed.

VS code extensions

Open VS code and install the C# extension from Microsoft, this will also install the “.NET Runtime Install Tool”.

C# extension from Microsoft

Microsoft recommends you to add on the C# Dev Kit extension. This will be required to debug the Godot project from VS code.

Close VS code and start Godot game engine.

Godot Settings for VS code

Inside Godot Editor, go to Editor>Editor Settings and find the Dot Net option on the left pane. Select the editor option on the left pane and set Visual Studio Code as your external editor.

Setting VS code as the external code editor for C#.

Now right click on any node and select “Attach Script”. Select C# as the language and click create. Double click on the C# script and it should now open in VS code.

You should now have IntelliSense enable by default. You can test it out by typing Input.Is inside the _Ready() function and you should see suggestions as shown below.4

IntelliSense in VS code for Godot 4

Setting Up ‘Run and Debug’ using launch.json and tasks.json

Select the Run and Debug icon on the left bar and click on “create a launch.json file”. Select .NET 5+ and .NET core from the debugger options.

Creating launch.json file

Inside the launch.json file, delete all the lines with warnings. Then assign your Godot exe file path to the program parameter. If you are on Windows then you need to change the “\” to “/” in the path.

Save the launch.json file. Here is how the final file should look like

"version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "C:/Users/rvinc/Downloads/Godot_v4.1.1-stable_mono_win64/Godot_v4.1.1-stable_mono_win64/Godot_v4.1.1-stable_mono_win64.exe",
            "args": [],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]

Under the Start debugging options select .NET Core Launch(console) and hit the green play button.

Start debugging options

You will see an error popup, select Configure Task.

Select “Create tasks.json file from template”

Select .NET Core as the template

Here is how the tasks.json file should look like

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "shell",
            "args": [
                "build",
                // Ask dotnet build to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                // Do not generate summary otherwise it leads to duplicate errors in Problems panel
                "/consoleloggerparameters:NoSummary"
            ],
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

Now if you hit the play button your Godot game should launch. You can place break points in your code to debug from VS code.

That’s it, now you have integrated VS code with Godot 4 for C#. If you are having any issues, feel free to comment below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading