Professional Documents
Culture Documents
com/godot-csharp-setup
GIULIANOB
There are a few different guides out there for setting up Godot but I feel like
man don’t give ou all of the nitt gritt details needed to get a great
workflow. This guide is a bit long but it will set ou up with the following:
I have tested this setup on Windows and Linux. If ou have an issues on
other platforms message me on discord @giulianob#9552. If ou’re looking
for just a template, head on over to the sample Github repo.
Download the latest version of VCode from the official website and
complete the installation.
148
KUDOS
Windows users will also need to download the NuGet command line utilit.
ave the nuget.exe to a director ou will remember for example c:\bin
\nuget.exe .
Download the latest version of Godot with Mono and extract it to a director
of our choice.
If ou’re on Windows then install the Visual tudio Build Tools. For other
platforms, install the Mono DK.
tart up Godot and create a new project or open our existing project.
There are a few settings we should change to work with VCode and
improve our workflow.
errors that crash our game unexpectedl. Feel free to lower the “Max
Log Files” setting.
Port: 23685
This is an addon that gives us a shortcut to the “Mono > Debugger Agent >
Wait For Debugger” setting. We’ll need to toggle this setting whenever we
would like to perform a debugging session.
2. Extract the zip to the addons folder of our project. For example if our
project is located at c:\myproject then ou should have a c:\myproject
\addons\MonoDebuggerQM-1.0.1\ director after extracting the zip.
3. Go to Project menu > Project ettings > Plugins and change the Status of the
MonoDebugger plugin to Active . If ou don’t see the plugin make sure ou
extracted it to the right location.
Let’s create our initial scene. Just create a scene with a simple Node2D or
anthing ou like. Click Play or press F5 and make sure the game runs.
VCode should have opened when we created our script, if it didn’t, just
double click on our script in the FileSystem view or click on the script
icon on our root node.
C# (b Microsoft)
Now let’s configure our csproj file to include some extra goodies.
1. Open our .csproj file. If our project is named MGodotGame then the file
will be called MyGodotGame.csproj .
to
This is technicall not required but if ou ever build our own addons ou
will not receive Intellisense unless ou change the project default to Tools
148
KUDOS
1. Add the following lines after the <Configuration .. line we just edited
above:
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<LangVersion>7.3</LangVersion>
This will enable some newer C# features that are not available in the default
settings of the csproj file.
1. Find the last <ItemGroup> which should contain the .cs file ou created.
For example mine is:
<ItemGroup>
<Compile Include="Default.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
change it to
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
This option configures our project to build ever .cs file it finds and gets
around that unreliable setting we disabled earlier to auto update the project
file.
witch back to Godot and make sure ou can still run our game. If ou get
an Godot editor crashes now or in the future tr deleting our .mono
director.
Let’s configure the abilit to run our game right from VCode.
148
KUDOS
2. If ou don’t et have a launch.json it will ask ou to choose a template.
Choose anthing, we’ll be replacing the contents.
3. Replace the contents of the launch.json file with this one from Github. I’ll
explain this file in more detail later.
Windows Users: Replace the path to the Godot .exe to wherever ou
have Godot installed.
Linux Users: Replace the path to the Godot .exe with godot-mono or
wherever ou have Godot installed.
5. Once again if ou don’t have a tasks.json et defined then choose to create
a new one.
6. Replace the contents of the tasks.json file with this one from Github.
Linux Users: Just simpl use msbuild and nuget for the command.
The should be installed in our /usr/local/bin director.
{
"mono-debug.exceptionOptions": {
"System.ArithmeticException": "always",
"System.ArrayTypeMismatchException": "always",
"System.DivideByZeroException": "always",
"System.Exception": "always",
"System.IndexOutOfRangeException": "always",
148
"System.InvalidCastException": "always", KUDOS
"System.NullReferenceException": "always",
"System.OutOfMemoryException": "always",
"System.OverflowException": "always",
"System.StackOverflowException": "always",
"System.SystemException": "always",
"System.TypeInitializationException": "always"
}
}
These settings will make sure that the debugger pauses if ou receive an
exceptions in our game. Tpicall the game will just crash and ou ma not
even get a stacktrace which makes it extremel difficult to debug.
1. Click on the Debug icon on the left hand menu bar and change the Debug
configuration to Run and Attach
2. witch back to Godot for just a second and check the “Wait for Debugger”
option on the top-right Debugger menu that was added b the 148
KUDOS
MonoDebugger addon.
We’re all set up and we should be able to run our project right from
VCode!
You can see on the left hand side all of our local variables. You can add
other variables to watch in the Watch window or use the Debug Console.
To see the output of our code, ou will need to switch the Debug Console
to the Run configuration.
148
KUDOS
2. Attach : tarts the Mono debugger in VCode. The debugger listens for
connections that our game establishes when ou have the Wait For
Debugger option enabled in Godot.
3. Run and Attach : This will run the two configurations above to both start the
debugger and our game.
Now to stop debugging ou have to either click the top button twice or
press Shift+F5 twice. If ou just tr to close the game ou will notice that it
just freezes.
Antime ou switch back to Godot and wish to run the project from Godot
itself then ou have the following options.
Option 1) Disable the “Wait for Debugger” option otherwise our game will run
148
for a few seconds then quit because it cannot connect to the debugger. You
KUDOS
Option 2) Change VCode to use the “Attach” task, hit F5 , then run Godot so
it can attach to the debugger with the “Wait for Debugger” option enabled. If
ou do this, ou will need to start the VCode debugger ever time ou restart
our game in Godot. I tpicall just disable the “Wait for Debugger” option if I’m
working within Godot and enable when in VCode.
Roslnator is an open source static analsis tool that can also perform some
automatic code fixes. If ou have experience with Resharper it’s an open
source alternative that has much better performance.
To install Roslnator:
1. Open our csproj file and add the following: In the first <PropertyGroup>
add
<CodeAnalysisRuleSet>roslynator.ruleset</CodeAnalysisRuleSet>
<ItemGroup>
<PackageReference Include="Roslynator.Analyzers">
<Version>2.2.0</Version>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;
buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
At the time of this post Roslnator is at version 2.2.0 but feel free to install a
newer version if available.
This file defines some overrides to the default rules. For example, ou ma
not like some of the warnings Roslnator gives ou. The file above are some
rules that I personall disable or lower the severit to Info.
You can start with a blank ruleset (just make sure ou have the root
RuleSet element) and add exceptions as needed. You can view the full set
of analzers on the Roslnator project.
3. Reload VCode.
148
KUDOS
If we now tpe some code that fails one of the static analsis rules we will
receive a warning. For example
We can access the quick-fix menu b putting the cursor over one of the
issues and either clicking the lightbulb icon or pressing Ctrl+.
I’ve gone ahead and applied all of the automatic code fixes
148
KUDOS
You can also access quite a few automated refactorings b pressing the
same shortcut even when there isn’t a warning. For example given the
following code
148
KUDOS
You can actuall keep going and it will offer to inline the offset .
There are times where our game can crash during start up, we enabled
pausing on exceptions in an earlier step which means the debugger will
stop whenever there’s an exception. However, it’s possible that once ou
export a release version of our project ou have some exceptions that ou
need to debug. That’s wh we enabled the file logging option in Godot in
the beginning of this tutorial. File logging will log all of our game’s
GD.Print statements in a file located at %APPDATA\Roaming\Godot
\app_userdata\YourProjectName . However, Godot b default will not log
exceptions thrown from our application so we have to do this ourselves b
adding the following code at our main scene cs file.
using System;
using Godot;
namespace SampleProject
{
public class Node : Node2D
{
public override void _Ready()
{
AppDomain.CurrentDomain.UnhandledException +=
UnhandledExceptionHandler;
}
148
KUDOS
That’s it!
I hope everthing here made sense. If ou need an help join the official
Godot #csharp channel on Discord and feel free to message me on Discord
at giulianob#9552.
You can find a sample project over at Github with all of the configurations
complete.
148 KUDOS
GIULIANOB @giuliano_b
SVBTLE
148
KUDOS