Using C# in Godot with Visual studio code
Start Using C# in Godot Engine: A Quick Guide for Unity Users
If you're a Unity developer transitioning to Godot, you'll be happy to know you can put your C# skills to use. Here’s a step-by-step guide to get started, along with some important tips about C# support in the Godot engine.
1. Get the Right Godot Version
Go to the official Godot download page and ensure you get the
.NET version (formerly called Mono) of Godot. You’ll see ".NET" in the top right of the project manager if you have the correct version. If not, download and install the .NET version.2. Create a C# Script
In your Godot project, right-click in the FileSystem panel and choose to add a new script. You’ll see an option to select either GDScript or C#—choose C#. Give your script a name, or leave the default one, and click Create.
Note: The built-in Godot script editor doesn’t offer autocomplete or C# formatting. To work efficiently, you’ll want an external editor, like Visual Studio Code.
[IMAGE - "Person installing Visual Studio Code on a computer"]
3. Set Up Visual Studio Code
- Download VS Code from the official website.
- In Godot, go to
Editor→Editor Settings, scroll down and openDotnet→Editor. Select Visual Studio Code as your external editor. - Next, open any C# script. VS Code should launch.
- You may be prompted to "Trust the authors" of the project—click
Yes.
4. Enable C# Autocomplete in VS Code
- Go to the Extensions tab in VS Code and search for
C#(by Microsoft). Install this official extension. - After installation, restart VS Code by closing it and reopening your C# script in Godot. Autocomplete for classes such as
GDand signals should now be available.
5. Test Your Setup
In your script's
_Ready() method, try this:C#1GD.Print("Hello world!"); 2
Attach the script to a node, run your scene, and you should see "Hello World" in the output.
Preview
6. Example: Porting a Simple Script from GDScript to C#
Here’s a simple scenario: a button that increases a coin counter and updates a label.
-
Define an integer for coins:C#
1int coinsValue = 0; 2 -
Connect the button’s
pressedsignal to your script in theNodetab. Rename the handler (e.g.,OnGrantCoinsButtonPressed) for best C# practices.C#1public void OnGrantCoinsButtonPressed() 2{ 3 coinsValue += 10; 4 GD.Print(coinsValue); 5} 6 -
To update the label, enable
Unique Nameon your Label node. In your script:C#1Label coinsValueText; 2 3public override void _Ready() 4{ 5 coinsValueText = GetNode<Label>("CoinsValueText"); 6 coinsValueText.Text = coinsValue.ToString(); 7} 8And in your button handler, update the label:C#1coinsValueText.Text = coinsValue.ToString(); 2
Preview
7. Debugging C# in Godot
Visual Studio Code allows to atatch a debugger to running Godot game. This way you can place breakpoints in your code and resolve any problems much faster!
Here is a step by step guide:
In Visual Studio Code go to “Run and Debug” tab
Click “Create a launch.json file” and select any template (for example C#) That will create a new file .vscode/launch.json
Replace all contents of this file with this:
C#1{ 2 "version": "0.2.0", 3 "configurations": [ 4 { 5 "name": "Play", 6 "type": "coreclr", 7 "request": "launch", 8 "preLaunchTask": "build", 9 // Replace your "program" path with path to your Godot installation. 10 // Here you can find some examples (remember to replace [user]!): 11 // /Users/[user]/Downloads/Godot_mono.app/Contents/MacOS/Godot 12 // /Applications/Godot_mono.app/Contents/MacOS/Godot 13 // C:/Users/[user]/Downloads/Godot_v4.1-stable_mono_win64/Godot.exe 14 // C:/Program Files/Godot/Godot.exe 15 "program": "[ENTER PATH HERE]", 16 "args": [], 17 "cwd": "${workspaceFolder}", 18 "stopAtEntry": false, 19 } 20 ] 21}
Please enter your correct path to the Godot Engine in "Program" value. You can find example paths in the comments. Please remove all the comments after finished work.
Create a new file in .vscode folder called tasks.json
Paste this contents in .vscode/tasks.json:
C#1{ 2 "version": "2.0.0", 3 "tasks": [ 4 { 5 "label": "build", 6 "command": "dotnet", 7 "type": "process", 8 "args": [ 9 "build" 10 ], 11 "problemMatcher": "$msCompile", 12 "presentation": { 13 "echo": true, 14 "reveal": "silent", 15 "focus": false, 16 "panel": "shared", 17 "showReuseMessage": true, 18 "clear": false 19 } 20 } 21 ] 22 }
save both files and try out debugging! Open "Run and Debug" tab and press "Play". That should build and open your Godot game in Debug mode. In this mode all breakpoints will be triggered
Final Notes on C# Compatibility in Godot
While C# works great for most game logic, note that not all Godot extensions and plugins support C# yet (especially editor plugins). For most gameplay scripting, you’re good to go, but if you need advanced plugins, check for C# compatibility first.
