C# API Unterschiede zu GDScript

Dies ist eine (unvollständige) Liste von API Unterschieden zwischen C# und GDScript.

Allgemeine Unterschiede

As explained in the C# Grundlagen, C# generally uses PascalCase instead of the snake_case used in GDScript and C++.

Globaler Bereich

Globale Funktionen und einige Konstanten mussten in Klassen verschoben werden, da C# es nicht erlaubt sie in Namespaces zu deklarieren. Die meisten globalen Konstanten wurden in ihre eigenen Aufzählungen verschoben.

Konstanten

Globale Konstanten wurden in ihre eigenen Aufzählungen verschoben. Zum Beispiel wurden ERR_* `` Konstanten in die ``Error Aufzählung verschoben.

Sonderfälle:

GDScript C#
SPKEY GD.SpKey
TYPE_* Variant.Type Aufzählung
OP_* Variant.Operator Aufzählung

Mathematische Funktionen

Globale Mathefunktionen wie abs, acos, asin, atan und atan2 liegen unter Mathf als Abs, Acos, Asin, Atan und Atan2. Die PI Konstante findet man unter Mathf.Pi.

Zufallsfunktionen

Globale Zufallsfunktionen wie rand_range und rand_seed liegen unter GD. Beispiel: GD.RandRange und GD.RandSeed.

Weitere Funktionen

Viele andere globale Funktionen wie print und var2str liegen unter GD. Beispiel: GD.Print und GD.Var2Str.

Ausnahmen:

GDScript C#
weakref(obj) Object.WeakRef(obj)
is_instance_valid(obj) Object.IsInstanceValid(obj)

Tipps

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.

Beispiel:

using static Godot.GD;

public class Test
{
    static Test()
    {
        Print("Hello"); // Instead of GD.Print("Hello");
    }
}

Exportiere Schlüsselwort

Use the [Export] attribute instead of the GDScript export keyword. This attribute can also be provided with optional PropertyHint and hintString parameters. Default values can be set by assigning a value.

Beispiel:

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 Schlüsselwörter

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.

[Signal]
delegate void MySignal(string willSendsAString);

Siehe auch: C# Signale.

Singletons (Entwurfsmuster)

Singletons sind als statische Klassen verfügbar, anstatt das Singleton-Muster zu verwenden. Dies dient dazu den Code weniger ausführlich zu machen als bei einer Instance-Eigenschaft.

Beispiel:

Input.IsActionPressed("ui_down")

However, in some very rare cases this is not enough. For example, you may want to access a member from the base class Godot.Object, like Connect. For such use cases we provide a static property named Singleton that returns the singleton instance. The type of this instance is Godot.Object.

Beispiel:

Input.Singleton.Connect("joy_connection_changed", this, nameof(Input_JoyConnectionChanged));

Zeichenkette

Use System.String (string). Most of Godot's String methods are provided by the StringExtensions class as extension methods.

Beispiel:

string upper = "I LIKE SALAD FORKS";
string lower = upper.ToLower();

Es gibt allerdings ein paar Unterschiede:

  • erase: Strings are immutable in C#, so we cannot modify the string passed to the extension method. For this reason, Erase was added as an extension method of StringBuilder instead of string. Alternatively, you can use string.Remove.
  • IsSubsequenceOf/IsSubsequenceOfi: An additional method is provided, which is an overload of IsSubsequenceOf, allowing you to explicitly specify case sensitivity:
str.IsSubsequenceOf("ok"); // Case sensitive
str.IsSubsequenceOf("ok", true); // Case sensitive
str.IsSubsequenceOfi("ok"); // Case insensitive
str.IsSubsequenceOf("ok", false); // Case insensitive
  • Match/Matchn/ExprMatch: An additional method is provided besides Match and Matchn, which allows you to explicitly specify case sensitivity:
str.Match("*.txt"); // Case sensitive
str.ExprMatch("*.txt", true); // Case sensitive
str.Matchn("*.txt"); // Case insensitive
str.ExprMatch("*.txt", false); // Case insensitive

Basis

Structs cannot have parameterless constructors in C#. Therefore, new Basis() initializes all primitive members to their default value. Use Basis.Identity for the equivalent of Basis() in GDScript and C++.

Die folgende Methode wurde in eine Eigenschaft mit einem neuen Namen konvertiert:

GDScript C#
get_scale() Scale

Transform2D

Structs cannot have parameterless constructors in C#. Therefore, new Transform2D() initializes all primitive members to their default value. Please use Transform2D.Identity for the equivalent of Transform2D() in GDScript and C++.

Die folgenden Methoden wurden in Eigenschaften konvertiert, deren jeweilige Namen geändert wurden:

GDScript C#
get_rotation() Drehung
get_scale() Scale

Fläche

The following method was converted to a property with a slightly different name:

GDScript C#
center() Center

Rect2

The following field was converted to a property with a slightly different name:

GDScript C#
end End

Die folgende Methode wurde in eine Eigenschaft mit einem neuen Namen konvertiert:

GDScript C#
get_area() Area

Quat

Structs cannot have parameterless constructors in C#. Therefore, new Quat() initializes all primitive members to their default value. Please use Quat.Identity for the equivalent of Quat() in GDScript and C++.

The following methods were converted to a property with a different name:

GDScript C#
length() Length
length_squared() LengthSquared

Array

This is temporary. PoolArrays will need their own types to be used the way they are meant to.

GDScript C#
Array Godot.Collections.Array
PoolIntArray int[]
PoolByteArray byte[]
PoolFloatArray float[]
PoolStringArray String[]
PoolColorArray Color[]
PoolVector2Array Vector2[]
PoolVector3Array Vector3[]

Godot.Collections.Array<T> is a type-safe wrapper around Godot.Collections.Array. Use the Godot.Collections.Array<T>(Godot.Collections.Array) constructor to create one.

Wörterbuch

Nutze Godot.Collections.Dictionary.

Godot.Collections.Dictionary<T> is a type-safe wrapper around Godot.Collections.Dictionary. Use the Godot.Collections.Dictionary<T>(Godot.Collections.Dictionary) constructor to create one.

Variante

System.Object (object) wird genutzt anstelle von Variant.

Kommunikation mit anderen Skriptsprachen

Dies wird detailliert beschrieben in sprachübergreifendes Skripten.

Yield

Something similar to GDScript's yield with a single parameter can be achieved with C#'s yield keyword.

The equivalent of yield on signal can be achieved with async/await and Godot.Object.ToSignal.

Beispiel:

await ToSignal(timer, "timeout");
GD.Print("After timeout");

andere Unterschiede

preload, as it works in GDScript, is not available in C#. Use GD.Load or ResourceLoader.Load instead.

andere Unterschiede:

GDScript C#
Color8 Color.Color8
ist_inf float.IsInfinity
is_nan float.IsNaN
dict2inst TODO
instant2dict TODO