c_sharp_basics.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .. _doc_c_sharp:
  2. Introduction
  3. ============
  4. .. warning:: C# support is a new feature in Godot 3.0.
  5. As such, you may still run into some issues, or find spots where the documentation could be improved.
  6. Please report issues with C# in Godot on the `engine Github page <https://github.com/godotengine/godot/issues>`_.
  7. And any documentation issues on the `documentation Github Page <https://github.com/godotengine/godot-docs/issues>`_.
  8. This page provides a brief intro to C#, both what it is and how to use it in Godot.
  9. Afterwards, you may want to look at :ref:`how to use specific features <doc_c_sharp_features>`,
  10. read about the :ref:`differences between the C# and the GDScript API <doc_c_sharp_differences>`
  11. and (re)visit the :ref:`Scripting section <doc_scripting>` of the step-by-step tutorial.
  12. C# is a high-level programming language developed by Microsoft. In Godot it is implemented with the Mono 5.x .NET framework including full support for C# 7.0.
  13. Mono is an open source implementation of Microsoft's .NET Framework based on the ECMA standards for C# and the Common Language Runtime.
  14. 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.
  15. .. note:: This is **not** a full-scale tutorial on the C# language as a whole.
  16. If you aren't already familiar with its syntax or features,
  17. see the `Microsoft C# guide <https://docs.microsoft.com/en-us/dotnet/csharp/index>`_ or look for a suitable introduction elsewhere.
  18. Setup C# for Godot
  19. ------------------
  20. Download and install the `Mono <http://www.mono-project.com/download/>`_ SDK.
  21. If you are using Godot 3.0.2, you must use Mono 5.4.
  22. Godot 3.0.3+ requires Mono 5.12 on all platforms.
  23. .. note:: To download Mono v5.12+ on a Mac, locate the "Stable Channel" link from the `Mono Downloads Page <http://www.mono-project.com/download/>`_. The Visual Studio channel is an earlier version of Mono and will not work with Godot 3.0.3+.
  24. You also need MSBuild (at least version 15.0) which should come with the Mono installation.
  25. .. note:: For instructions on installing older versions of Mono on Linux, see `this page <http://www.mono-project.com/docs/getting-started/install/linux/#accessing-older-releases>`_.
  26. Older versions of Mono for macOS and Windows can be found `here <https://download.mono-project.com/archive/>`_.
  27. Additionally, your Godot version must have Mono support enabled, so ensure you download the **Mono version** of Godot.
  28. 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.
  29. In summary, you must have installed 1) the correct Mono SDK version for your Godot version, and 2) The Mono version of Godot.
  30. Configuring an external editor
  31. ------------------------------
  32. While Godot does have its own scripting editor, its support for C# is kept
  33. minimal, and it's recommended that you use an external IDE or editor, such as
  34. Microsoft Visual Studio Code, or MonoDevelop, which provide auto-completion,
  35. debugging and other features useful when working with C#.
  36. To set it up, in Godot click on ``Editor``, then ``Editor Settings``. Scroll
  37. down to the bottom, to the ``Mono`` settings. Under Mono click on ``Editor``,
  38. and on that page choose your external editor of choice.
  39. .. note:: If you are using Visual Studio Code, ensure you download and install the .NET tools extension. Without this, Godot will crash when trying to create a new C# project or edit a C# script.
  40. Creating a C# script
  41. --------------------
  42. After you successfully setup C# for Godot, you should see the following option when selecting ``Attach script`` in the context menu of a node in your scene:
  43. .. image:: img/attachcsharpscript.png
  44. Note that while some specifics change, most of the things work the same when using C# for scripting.
  45. If you're new to Godot, you may want to peruse the tutorials on :ref:`doc_scripting` at this point.
  46. While some places in the documentation still lack C# examples, most things can be transferred easily from GDScript.
  47. Project setup and workflow
  48. --------------------------
  49. When you create the first C# script, Godot initializes the C# project files for your Godot project.
  50. This includes generating a C# solution (``.sln``) and project (``.csproj``) as well as some utility files and folders (``.mono``, sometimes ``Properties``).
  51. 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.
  52. When troubleshooting, it sometimes can help to delete the ``.mono`` folder and let it regenerate.
  53. 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.
  54. In this case, it can help to edit the solution files manually.
  55. 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.
  56. You can for now simply open up the ``.csproj`` and look for the ``ItemGroup``, there should be a line included like the following:
  57. .. code-block:: xml
  58. :emphasize-lines: 2
  59. <ItemGroup>
  60. <Compile Include="Test.cs" />``
  61. <Compile Include="AnotherTest.cs" />``
  62. </ItemGroup>
  63. 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.
  64. Example
  65. -------
  66. Here's a blank C# script with some comments to demonstrate how it works.
  67. .. code-block:: csharp
  68. using Godot;
  69. using System;
  70. public class YourCustomClass : Node
  71. {
  72. // Member variables here, example:
  73. private int a = 2;
  74. private string b = "textvar";
  75. public override void _Ready()
  76. {
  77. // Called every time the node is added to the scene.
  78. // Initialization here.
  79. GD.Print("Hello from C# to Godot :)");
  80. }
  81. public override void _Process(float delta)
  82. {
  83. // Called every frame. Delta is time since last frame.
  84. // Update game logic here.
  85. }
  86. }
  87. As you can see, the things normally in global scope in GDScript like Godot's ``print`` function are available in the ``GD`` namespace.
  88. For a list of those, see the class reference pages for :ref:`@GDScript <class_@gdscript>` and :ref:`@GlobalScope <class_@globalscope>`.
  89. .. note::
  90. Keep in mind that the class you wish to attach to your node should be named as the ``.cs`` file.
  91. If not, you will get the following error and won't be able to run the scene: ``Cannot find class XXX for script res://XXX.cs``.
  92. General differences between C# and GDScript
  93. -------------------------------------------
  94. The C# API uses ``PascalCase`` instead of ``snake_case`` in GDScript/C++.
  95. Where possible, fields and getters/setters have been converted to properties.
  96. In general, the C# Godot API strives to be as idiomatic as is reasonably possible.
  97. For more, see the :ref:`doc_c_sharp_differences` page.
  98. Current gotchas and known issues
  99. --------------------------------
  100. As C# support is quite new to Godot, there are some growing pains and things that still need to be ironed out.
  101. 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>`_.
  102. - 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>`_)
  103. - Writing editor plugins and tool scripts in C# is not yet supported
  104. - Exporting Mono projects is only supported for desktop platforms (Linux, Windows and macOS). HTML5, Android, iOS and UWP are not currently supported (`#18364 comment <https://github.com/godotengine/godot/issues/18364#issuecomment-406222102>`_)
  105. - Attached C# scripts should refer to a class that has a class name that matches the file name and is unique within the project assembly (`#7402 comment <https://github.com/godotengine/godot/issues/7402#issuecomment-269910926>`_)
  106. Performance of C# in Godot
  107. --------------------------
  108. 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.
  109. 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.
  110. C# is faster, but requires some expensive marshalling when talking to Godot.
  111. Using Nuget Packages in Godot
  112. -----------------------------
  113. `Nuget <https://www.nuget.org/>`_ Packages can be installed and used with Godot,
  114. as with any project. Many IDEs (such as vs code) can add packages directly. They
  115. can also be added manually by adding the package reference in the .csproj file
  116. located in the project root:
  117. .. code-block:: xml
  118. :emphasize-lines: 2
  119. <ItemGroup>
  120. <PackageReference Include="Newtonsoft.Json" Version="11.0.2"/>
  121. </ItemGroup>
  122. ...
  123. </Project>
  124. Whenever packages are added or modified, run nuget restore in the root of the
  125. project directory, to ensure that the nuget packages will be available for
  126. msbuild to use, run::
  127. $ msbuild /t:restore