.. _doc_c_sharp_differences: C# API differences to GDScript ============================== This is a (incomplete) list of API differences between C# and GDScript. General differences ------------------- As explained in the :ref:`doc_c_sharp`, C# generally uses ``PascalCase`` instead of the ``snake_case`` used in GDScript and C++. Global scope ------------ Global functions and some constants had to be moved to classes, since C# does not allow declaring them in namespaces. Most global constants were moved to their own enums. Constants ^^^^^^^^^ Global constants were moved to their own enums. For example, ``ERR_*`` constants were moved to the ``Error`` enum. Special cases: ======================= =========================================================== GDScript C# ======================= =========================================================== ``SPKEY`` ``GD.SpKey`` ``TYPE_*`` ``Variant.Type`` enum ``OP_*`` ``Variant.Operator`` enum ======================= =========================================================== Math functions ^^^^^^^^^^^^^^ Math global functions, like ``abs``, ``acos``, ``asin``, ``atan`` and ``atan2``, are located under ``Mathf`` as ``Abs``, ``Acos``, ``Asin``, ``Atan`` and ``Atan2``. The ``PI`` constant can be found as ``Mathf.Pi``. Random functions ^^^^^^^^^^^^^^^^ Random global functions, like ``rand_range`` and ``rand_seed``, are located under ``GD``. Example: ``GD.RandRange`` and ``GD.RandSeed``. Other functions ^^^^^^^^^^^^^^^ Many other global functions like ``print`` and ``var2str`` are located under ``GD``. Example: ``GD.Print`` and ``GD.Var2Str``. Exceptions: =========================== ======================================================= GDScript C# =========================== ======================================================= ``weakref(obj)`` ``Object.WeakRef(obj)`` ``is_instance_valid(obj)`` ``Object.IsInstanceValid(obj)`` =========================== ======================================================= Tips ^^^^ Sometimes it can be useful to use the ``using static`` directive. This directive allows to access the members and nested types of a class without specifying the class name. Example: .. code-block:: csharp using static Godot.GD; public class Test { static Test() { Print("Hello"); // Instead of GD.Print("Hello"); } } Export keyword -------------- Use the ``[Export]`` attribute instead of the GDScript ``export`` keyword. This attribute can also be provided with optional :ref:`PropertyHint` and ``hintString`` parameters. Default values can be set by assigning a value. Example: .. code-block:: csharp using Godot; public class MyNode : Node { [Export] private NodePath _nodePath; [Export] private string _name = "default"; [Export(PropertyHint.Range, "0,100000,1000,or_greater")] private int _income; [Export(PropertyHint.File, "*.png,*.jpg")] private string _icon; } Signal keyword -------------- Use the ``[Signal]`` attribute to declare a signal instead of the GDScript ``signal`` keyword. This attribute should be used on a `delegate`, whose name signature will be used to define the signal. .. code-block:: csharp [Signal] delegate void MySignal(string willSendsAString); See also: :ref:`doc_c_sharp_signals`. `onready` keyword ----------------- GDScript has the ability to defer the initialization of a member variable until the ready function is called with `onready` (cf. :ref:`doc_gdscript_onready_keyword`). For example: .. code-block:: gdscript onready var my_label = get_node("MyLabel") However C# does not have this ability. To achieve the same effect you need to do this. .. code-block:: csharp private Label _myLabel; public override void _Ready() { _myLabel = GetNode