|
@@ -1,49 +1,93 @@
|
|
|
.. _doc_c_sharp:
|
|
|
|
|
|
-C#
|
|
|
-===
|
|
|
-
|
|
|
Introduction
|
|
|
---------------
|
|
|
+============
|
|
|
+
|
|
|
+.. warning:: C# support is a new feature in Godot 3.0.
|
|
|
+ As such, you may still run into some issues or find spots where the documentation could be improved.
|
|
|
+ Please report those issues on `Github <https://github.com/godotengine/godot/issues>`_.
|
|
|
|
|
|
-C# is a high-level programming language developed by Microsoft. In Godot it is implemented with the Mono 5.2 .NET framework including full support for C# 7.0.
|
|
|
+This page provides a brief intro to C#, both what it is and how to use it in Godot.
|
|
|
+Afterwards, you may want to look at :ref:`how to use specific features <doc_c_sharp_features>`,
|
|
|
+read about the :ref:`differences between the C# and the GDScript API <doc_c_sharp_differences>`
|
|
|
+and (re)visit the :ref:`Scripting section <doc_scripting>` of the step-by-step tutorial.
|
|
|
+
|
|
|
+C# is a high-level programming language developed by Microsoft. In Godot it is implemented with the Mono 5.2 .NET framework including full support for C# 7.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 <http://www.mono-project.com/docs/about-mono/compatibility/>`_ page in the Mono documentation.
|
|
|
|
|
|
.. note:: This is **not** a full-scale tutorial on the C# language as a whole.
|
|
|
If you aren't already familiar with its syntax or features,
|
|
|
- see the `Microsoft C# guide <https://docs.microsoft.com/en-us/dotnet/csharp/index>`_.
|
|
|
+ see the `Microsoft C# guide <https://docs.microsoft.com/en-us/dotnet/csharp/index>`_ or look for a suitable introduction elsewhere.
|
|
|
+
|
|
|
+Setup C# for Godot
|
|
|
+------------------
|
|
|
|
|
|
-Necessary Downloads
|
|
|
--------------------------
|
|
|
+To use C# in Godot you must have `Mono <http://www.mono-project.com/download/>`_ installed (at least version 5.2).
|
|
|
|
|
|
-To use C# in Godot you must have `Mono <http://www.mono-project.com/download/>`_
|
|
|
-installed (at least version 5.2), and use a Godot version with Mono enabled, which adds C# support next to the existing options of GDScript, visual scripting and C++.
|
|
|
+Additionally, your Godot version must have Mono support enabled, so take care to download the **Mono version** of Godot.
|
|
|
+If you are building Godot from source, make sure to follow the steps to include Mono support in your build outlined on the :ref:`doc_compiling_with_mono` page.
|
|
|
|
|
|
Windows users also need MS Build 15.0, which comes bundled with Visual Studio 2017,
|
|
|
or can be downloaded separately with `build tools for Visual Studio 2017 <https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=15#>`_.
|
|
|
|
|
|
+Configuring an external editor
|
|
|
+------------------------------
|
|
|
+
|
|
|
+While Godot does have its own scripting editor, its support for C# is kept
|
|
|
+minimal, and it's recommended that you use an external IDE or editor, such as
|
|
|
+Microsoft Visual Studio Code, or MonoDevelop, which provide auto-completion,
|
|
|
+debugging and other features useful when working with C#.
|
|
|
+To set it up, in Godot click on ``Editor``, then ``Editor Settings``. Scroll
|
|
|
+down to the bottom, to the ``Mono`` settings. Under Mono click on ``Editor``,
|
|
|
+and on that page choose your external editor of choice.
|
|
|
+
|
|
|
+Creating a C# script
|
|
|
+--------------------
|
|
|
+
|
|
|
+After you sucessfully setup C# for Godot, you should see the following option when selecting ``Attach script`` in the context menu of a node in your scene:
|
|
|
+
|
|
|
+.. image:: img/attachcsharpscript.png
|
|
|
+
|
|
|
+Note that while some specifics change, most of the things work the same when using C# for scripting.
|
|
|
+If you're new to Godot, you may want to peruse the tutorials on :ref:`doc_scripting` at this point.
|
|
|
+While some places in the documentation still lack C# examples, most things can be transferred easily from GDScript.
|
|
|
+
|
|
|
+Project setup and workflow
|
|
|
+--------------------------
|
|
|
|
|
|
-History
|
|
|
---------
|
|
|
+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 project (``.csproj``) as well as some utility files and folders (``.mono``, sometimes ``Properties``).
|
|
|
+All of these but ``.mono`` are important and should be kept in your version control system. ``.mono`` can be safely added to the ignore list of your VCS.
|
|
|
+When troubleshooting, it sometimes can help to delete the ``.mono`` folder and let it regenerate.
|
|
|
|
|
|
-Back in 2016 the Godot team reached out to Microsoft, with Miguel de Icaza's
|
|
|
-support, to see if they would consider funding C# support being added to
|
|
|
-Godot. Microsoft agreed and gave the team a $24,000 donation to work on adding
|
|
|
-C# support. Thanks to that donation, Juan Lineietsky and Ignacio Roldán
|
|
|
-Etcheverry were able to work on bringing C# support to Godot using the Mono
|
|
|
-.NET framework. Support was added to Godot version 3.0 using mono 5.2, giving users
|
|
|
-the power of C# in their game making.
|
|
|
+Note that currently there are some issues where the Godot and the C# project don't stay in sync; if you delete, rename or move things like scripts or nodes, they may no longer match up.
|
|
|
+In this case, it can help to edit the solution files manually.
|
|
|
+
|
|
|
+Example: If you created a script (e.g. ``Test.cs``) and delete it in Godot, compilation will fail because the now missing file is still expected to be there by the CS project.
|
|
|
+You can for now simply open up the ``.csproj`` and look for the ``ItemGroup``, there should be a line included like the following:
|
|
|
+
|
|
|
+.. code-block:: xml
|
|
|
+ :emphasize-lines: 2
|
|
|
+
|
|
|
+ <ItemGroup>
|
|
|
+ <Compile Include="Test.cs" />``
|
|
|
+ <Compile Include="AnotherTest.cs" />``
|
|
|
+ </ItemGroup>
|
|
|
+
|
|
|
+Simply remove that line and your project should now again build fine. Same for renaming and moving things, simply rename and move them in the project file if needed.
|
|
|
|
|
|
Example
|
|
|
-------
|
|
|
|
|
|
-Here's a blank C# script with some comments to demonstate how it works.
|
|
|
+Here's a blank C# script with some comments to demonstrate how it works.
|
|
|
|
|
|
.. code-block:: csharp
|
|
|
|
|
|
using Godot;
|
|
|
using System;
|
|
|
|
|
|
- public class Path : Path
|
|
|
+ public class YourCustomClass : Node
|
|
|
{
|
|
|
// Member variables here, example:
|
|
|
private int a = 2;
|
|
@@ -53,6 +97,7 @@ Here's a blank C# script with some comments to demonstate how it works.
|
|
|
{
|
|
|
// 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)
|
|
@@ -62,14 +107,33 @@ Here's a blank C# script with some comments to demonstate how it works.
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+As you can see, the things normally in global scope in GDScript like Godot's ``print`` function are available in the ``GD`` namespace.
|
|
|
+For a list of those, see the class reference pages for :ref:`@GDScript <class_@gdscript>` and :ref:`@GlobalScope <class_@globalscope>`.
|
|
|
|
|
|
-Configuring an external editor
|
|
|
------------------------------------
|
|
|
+General differences between C# and GDScript
|
|
|
+-------------------------------------------
|
|
|
|
|
|
-While Godot does have its own scripting editor, its support for C# is kept
|
|
|
-minimal, and it's reccomended that you use an external IDE or editor, such as
|
|
|
-Microsoft Visual Studio Code, or MonoDevelop, which provide auto-completion,
|
|
|
-debugging and other features useful when working with C#.
|
|
|
-To set it up, in Godot click on ``Editor``, then ``Editor Settings``. Scroll
|
|
|
-down to the bottom, to the ``Mono`` settings. Under Mono click on ``Editor``,
|
|
|
-and on that page choose your external editor of choice.
|
|
|
+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, see the :ref:`doc_c_sharp_differences` page.
|
|
|
+
|
|
|
+Current gotchas and known issues
|
|
|
+--------------------------------
|
|
|
+
|
|
|
+As C# support is quite new to Godot, there are some growing pains and things that still 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 <https://github.com/godotengine/godot/labels/topic%3Amono>`_.
|
|
|
+
|
|
|
+- As explained above, the C# project isn't always kept in sync automatically when things are deleted, renamed or moved in Godot (`#12917 <https://github.com/godotengine/godot/issues/12917>`_)
|
|
|
+- Signals can only be added with ``add_user_signal()`` and don't show up in UI (`#11956 <https://github.com/godotengine/godot/issues/11956>`_)
|
|
|
+- Debug output like stack traces, file path and line numbers is missing on Windows (`#14589 <https://github.com/godotengine/godot/issues/14589>`_)
|
|
|
+- Writing editor plugins and tool scripts in C# is not yet supported
|
|
|
+- Exporting a project may not yet work
|
|
|
+
|
|
|
+Performance of C# in Godot
|
|
|
+--------------------------
|
|
|
+
|
|
|
+According to some preliminary `benchmarks <https://github.com/cart/godot3-bunnymark>`_, performance of C# in Godot - while generally in the same order of magnitude - is roughly **~4x** that of GDScript in some naive cases.
|
|
|
+For full performance, 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.
|