podstawowy C#¶
Wprowadzenie¶
Ostrzeżenie
Wsparcie dla C# zostało wprowadzone w Godot 3.0. W związku z tym nadal mogą wystąpić pewne problemy lub można znaleźć miejsca, w których można by było poprawić dokumentację. Prosimy o zgłaszanie problemów z C# w Godot na stronie Github Engine. I wszelkie problemy z dokumentacją na stronie Dokumentacja Godot Engine.
This page provides a brief introduction to C#, both what it is and how to use it in Godot. Afterwards, you may want to look at how to use specific features, read about the differences between the C# and the GDScript API and (re)visit the Scripting section of the step-by-step tutorial.
C# is a high-level programming language developed by Microsoft. In Godot, it is implemented with the Mono 6.x .NET framework, including full support for C# 8.0. Mono is an open source implementation of Microsoft's .NET Framework based on the ECMA standards for C# and the Common Language Runtime. A good starting point for checking its capabilities is the Compatibility page in the Mono documentation.
Informacja
To jest nie pełny poradnik na temat języka C#. Jeśli nie znasz jeszcze jego składni lub funkcji, zapoznaj się z przewodnikiem Microsoft C# <https://docs.microsoft.com/en-us/dotnet/csharp/index> lub poszukaj odpowiedniego wprowadzenia gdzieś indziej.
Setting up C# for Godot¶
Prerequisites¶
Install the latest stable version of the .NET SDK, previously known as the .NET Core SDK.
From Godot 3.2.3 onwards, installing Mono SDK is not a requirement anymore, except it is required if you are building the engine from source.
Godot bundles the parts of Mono needed to run already compiled games. However, Godot does not bundle the tools required to build and compile games, such as MSBuild and the C# compiler. These are included in the .NET SDK, which needs to be installed separately.
In summary, you must have installed the .NET SDK and the Mono-enabled version of Godot.
Dodatkowe informacje¶
Be sure to install the 64-bit version of the SDK(s) if you are using the 64-bit version of Godot.
If you are building Godot from source, install the latest stable version of Mono, and make sure to follow the steps to enable Mono support in your build as outlined in the Kompilowanie z Mono page.
Konfiguracja zewnętrznego edytora¶
Wsparcie C# we wbudowanym edytorze skryptów Godot'a jest minimalne. Rozważ używanie zewnętrznego IDE lub edytora, takiego jak Visual Studio Code lub MonoDevelop. Zapewniają one autouzupełnianie, debugowanie i inne użyteczne dla C# funkcje. Aby wybrać w Godot zewnętrzny edytor, kliknij Edytor → Ustawienia Edytora... i przewiń w dół do Mono. Pod Mono, kliknij na Edytor i wybierz zewnętrzny edytor wedle uznania. Godot wspiera obecnie następujące zewnętrzne edytory:
Visual Studio 2019
Visual Studio Code
MonoDevelop
Visual Studio for Mac
JetBrains Rider
See the following sections for how to configure an external editor:
JetBrains Rider¶
After reading the "Prerequisites" section, you can download and install JetBrains Rider.
Wtyczki edytora Editor → Editor Settings:
Set Mono -> Editor -> External Editor to JetBrains Rider.
Set Mono -> Builds -> Build Tool to dotnet CLI.
In Rider:
Set MSBuild version to .NET Core.
Install the Godot support plugin.
Visual Studio Code¶
After reading the "Prerequisites" section, you can download and install Visual Studio Code (aka VS Code).
Wtyczki edytora Editor → Editor Settings:
Set Mono -> Editor -> External Editor to Visual Studio Code.
Set Mono -> Builds -> Build Tool to dotnet CLI.
Visual Studio Code:
Install the C# extension.
Install the Mono Debug extension.
Install the C# Tools for Godot extension.
Informacja
If you are using Linux you need to install the Mono SDK for the C# tools plugin to work.
To configure a project for debugging open the Godot project folder in VS Code.
Go to the Run tab and click on Add Configuration.... Select C# Godot
from the dropdown menu. Open the tasks.json
and launch.json
files that
were created. Change the executable setting in launch.json
and command
settings in tasks.json
to your Godot executable path. Now, when you start
the debugger in VS Code, your Godot project will run.
Visual Studio (Windows only)¶
Download and install the latest version of Visual Studio. Visual Studio will include the required SDKs if you have the correct workloads selected, so you don't need to manually install the things listed in the "Prerequisites" section.
While installing Visual Studio, select these workloads:
Mobile development with .NET
.NET Core cross-platform development
Wtyczki edytora Editor → Editor Settings:
Set Mono -> Editor -> External Editor to Visual Studio.
Set Mono -> Builds -> Build Tool to dotnet CLI.
Next, you can download the Godot Visual Studio extension from github here. Double click on the downloaded file and follow the installation process.
Informacja
The option to debug your game in Visual Studio may not appear after installing the extension. To enable debugging, there is a workaround for Visual Studio 2019. There is a separate issue about this problem in Visual Studio 2022.
Informacja
If you see an error like "Unable to find package Godot.NET.Sdk", your NuGet configuration may be incorrect and need to be fixed.
A simple way to fix the NuGet configuration file is to regenerate it.
In a file explorer window, go to %AppData%\NuGet
. Rename or delete
the NuGet.Config
file. When you build your Godot project again,
the file will be automatically created with default values.
Tworzenie skryptu C#¶
After you successfully set up C# for Godot, you should see the following option when selecting Attach Script in the context menu of a node in your scene:

Note that while some specifics change, most concepts work the same when using C# for scripting. If you're new to Godot, you may want to follow the tutorials on Scripting languages at this point. While some places in the documentation still lack C# examples, most concepts can be transferred easily from GDScript.
Project setup and workflow¶
When you create the first C# script, Godot initializes the C# project files
for your Godot project. This includes generating a C# solution (.sln
)
and a project file (.csproj
), as well as some utility files and folders
(.mono
and Properties/AssemblyInfo.cs
).
All of these but .mono
are important and should be committed to your
version control system. .mono
can be safely added to the ignore list of your VCS.
When troubleshooting, it can sometimes help to delete the .mono
folder
and let it regenerate.
Przykład¶
Oto pusty skrypt C# z komentarzami, aby pokazać jak to działa.
using Godot;
using System;
public class YourCustomClass : Node
{
// Member variables here, example:
private int a = 2;
private string b = "textvar";
public override void _Ready()
{
// Called every time the node is added to the scene.
// Initialization here.
GD.Print("Hello from C# to Godot :)");
}
public override void _Process(float delta)
{
// Called every frame. Delta is time since the last frame.
// Update game logic here.
}
}
As you can see, functions normally in global scope in GDScript like Godot's
print
function are available in the GD
class which is part of
the Godot
namespace. For a list of methods in the GD
class, see the
class reference pages for
@GDScript and @GlobalScope.
Informacja
Pamiętaj, że klasa, którą chcesz dołączyć do węzła powinna być nazwana plikiem .cs
. Jeśli nie, pojawi się następujący błąd i nie będzie można uruchomić sceny: Nie można znaleźć klasy XXX dla skryptu res://XXX.cs`
Ogólne różnice między C# i GDScript¶
The C# API uses PascalCase
instead of snake_case
in GDScript/C++.
Where possible, fields and getters/setters have been converted to properties.
In general, the C# Godot API strives to be as idiomatic as is reasonably possible.
For more information, see the C# API differences to GDScript page.
Ostrzeżenie
You need to (re)build the project assemblies whenever you want to see new exported variables or signals in the editor. This build can be manually triggered by clicking the word Build in the top right corner of the editor. You can also click Mono at the bottom of the editor window to reveal the Mono panel, then click the Build Project button.
You will also need to rebuild the project assemblies to apply changes in "tool" scripts.
Current gotchas and known issues¶
As C# support is quite new in Godot, there are some growing pains and things that need to be ironed out. Below is a list of the most important issues you should be aware of when diving into C# in Godot, but if in doubt, also take a look over the official issue tracker for Mono issues.
Pisanie wtyczek i narzędzi w języku C# nie jest jeszcze obsługiwane.
State is currently not saved and restored when hot-reloading, with the exception of exported variables.
Attached C# scripts should refer to a class that has a class name that matches the file name.
There are some methods such as
Get()
/Set()
,Call()
/CallDeferred()
and signal connection methodConnect()
that rely on Godot'ssnake_case
API naming conventions. So when using e.g.CallDeferred("AddChild")
,AddChild
will not work because the API is expecting the originalsnake_case
versionadd_child
. However, you can use any custom properties or methods without this limitation.
Exporting Mono projects is supported for desktop platforms (Linux, Windows and macOS), Android, HTML5, and iOS. The only platform not supported yet is UWP.
Wydajność C# w Godot¶
According to some preliminary benchmarks, the performance of C# in Godot — while generally in the same order of magnitude — is roughly ~4× that of GDScript in some naive cases. C++ is still a little faster; the specifics are going to vary according to your use case. GDScript is likely fast enough for most general scripting workloads. C# is faster, but requires some expensive marshalling when talking to Godot.
Using NuGet packages in Godot¶
NuGet packages can be installed and used with Godot,
as with any C# project. Many IDEs are able to add packages directly.
They can also be added manually by adding the package reference in
the .csproj
file located in the project root:
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
...
</Project>
As of Godot 3.2.3, Godot automatically downloads and sets up newly added NuGet packages the next time it builds the project.
Profiling your C# code¶
Mono log profiler is available for Linux and macOS. Due to a Mono change, it does not work on Windows currently.
External Mono profiler like JetBrains dotTrace can be used as described here.