C# API 与 GDScript 的差异

这是C#和GDScript之间API差异的(不完整)列表.

一般差异

C# 基础 中所述,C#通常使用 PascalCase 而不是GDScript和C++中使用的 snake_case.

全局作用域

全局函数和某些常量必须移动到类中, 因为C#不允许在命名空间中声明它们. 大多数全局常量都被移动到它们自己的枚举中.

常量

全局常量被移动到它们自己的枚举中. 例如, ERR_* 常量被移动到 Error 枚举中.

特殊情况:

GDScript

C#

SPKEY

GD.SpKey

TYPE*

Variant.Type 枚举

OP*

Variant.Operator 枚举

数学函数

abs, acos , asin , atanatan2 这样的全局数学函数位于 Mathf 下, 名为 Abs , Acos, Asin , AtanAtan2. 常数 PI 可以通过 Mathf.Pi 获得.

随机函数

rand_rangerand_seed 等全局随机函数位于 GD 下. 例如 GD.RandRange 以及 GD.RandSeed.

其他函数

许多其他的全局函数, 如 printvar2str 位于C#中的 GD 下. 例如:GD.PrintGD.Var2Str.

例外情况:

GDScript

C#

weakref(obj)

Object.WeakRef(obj)

is_instance_valid(obj)

Object.IsInstanceValid(obj)

提示

有时候, 使用 using static 指令是很有用的. 该指令允许在不指定类名的情况下, 访问类的成员和嵌套类型.

示例:

  1. using static Godot.GD;
  2. public class Test
  3. {
  4. static Test()
  5. {
  6. Print("Hello"); // Instead of GD.Print("Hello");
  7. }
  8. }

导出关键字

使用 [Export] 特性替代GDScript的 export 关键字. 该特性还可以接受 PropertyHinthintString 参数. 默认可以设为任何值.

示例:

  1. using Godot;
  2. public class MyNode : Node
  3. {
  4. [Export]
  5. private NodePath _nodePath;
  6. [Export]
  7. private string _name = "default";
  8. [Export(PropertyHint.Range, "0,100000,1000,or_greater")]
  9. private int _income;
  10. [Export(PropertyHint.File, "*.png,*.jpg")]
  11. private string _icon;
  12. }

信号关键字

使用 [Signal] 特性代替GDScript中的 signal 关键字. 该特性应该用在 delegate 上, 其名称签名将用于定义信号.

  1. [Signal]
  2. delegate void MySignal(string willSendsAString);

另见: C# 信号 .

Onready 关键字

GDScript能够推迟成员变量的初始化, 直到用 onready 调用ready函数(参见 Onready 关键字 ). 例如:

  1. onready var my_label = get_node("MyLabel")

然而C#没有这个能力. 为了达到同样的效果, 你需要这样做.

  1. private Label _myLabel;
  2. public override void _Ready()
  3. {
  4. _myLabel = GetNode<Label>("MyLabel");
  5. }

单例

单例可以作为静态类使用, 而不是使用单例模式. 这是为了使代码不像使用 Instance 属性那样冗长.

示例:

  1. Input.IsActionPressed("ui_down")

但是, 在极少数情况下, 这还不够. 例如, 您可能希望访问基类 Godot.Object 中的成员, 比如 Connect. 对于此类情况, 我们提供了一个名为 Singleton 的静态属性, 该属性返回单例实例. 这个实例的类型是 Godot.Object.

示例:

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

字符串

使用 System.String ( string ). 所有Godot String方法都由 StringExtensions 类作为扩展方法提供.

示例:

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

但是有一些区别:

  • erase: 字符串在C#中是不可变的, 因此我们无法修改传递给扩展方法的字符串. 出于这个原因, Erase 被添加为 StringBuilder 的扩展方法而不是字符串. 或者您可以使用 string.Remove .

  • IsSubsequenceOf / IsSubsequenceOfi : 提供了另一种方法, 它是 IsSubsequenceOf 的重载, 允许明确指定区分大小写:

  1. str.IsSubsequenceOf("ok"); // Case sensitive
  2. str.IsSubsequenceOf("ok", true); // Case sensitive
  3. str.IsSubsequenceOfi("ok"); // Case insensitive
  4. str.IsSubsequenceOf("ok", false); // Case insensitive
  • Match / Matchn / ExprMatch : 除了 MatchMatchn 之外还提供了另一种方法, 它允许明确指定区分大小写:
  1. str.Match("*.txt"); // Case sensitive
  2. str.ExprMatch("*.txt", true); // Case sensitive
  3. str.Matchn("*.txt"); // Case insensitive
  4. str.ExprMatch("*.txt", false); // Case insensitive

Basis

结构体在 C# 中不能有无参数构造函数。因此 new Basis() 会将所有原始成员初始化为其默认值。使用 Basis.Identity 作为 GDScript 和 C++ 中的 Basis() 的等价物。

以下方法已转换为具有不同名称的属性:

GDScript

C#

get_scale()

Scale

Transform2D

结构在C#中不能有无参数构造函数, 因此 new Transform2D() 将所有原始成员初始化为其默认值. 请使用 Transform2D.Identity, 相当于GDScript和C++中的 Transform2D() .

下列方法已转换为属性, 其各自名称已被更改:

GDScript

C#

get_rotation()

Rotation

get_scale()

Scale

Plane

以下方法已转换为名称 稍有 不同的属性:

GDScript

C#

center()

Center

Rect2

以下字段已转换为名称 稍有 不同的属性:

GDScript

C#

end

End

以下方法已转换为具有不同名称的属性:

GDScript

C#

get_area()

Area

Quat

结构在C#中不能有无参数构造函数. 因此 new Quat() 将所有原始成员初始化为其默认值. 请使用 Quat.Identity, 相当于GDScript和C++中的 Quat() .

以下方法已转换为具有不同名称的属性:

GDScript

C#

length()

Length

length_squared()

LengthSquared

数组

这是暂时的.PoolArrays将需要使用它们自己的类型才能按预期的方式使用.

GDScript

C#

Array

Godot.Collections.Array

PoolIntArray

int[]

PoolByteArray

byte[]

PoolFloatArray

float[]

PoolStringArray

String[]

PoolColorArray

Color[]

PoolVector2Array

Vector2[]

PoolVector3Array

Vector3[]

Godot.Collections.Array<T>Godot.Collections.Array 的类型安全包装器. 可使用 Godot.Collections.Array<T>(Godot.Collections.Array) 构造器创建一个.

字典

使用 Godot.Collections.Dictionary.

Godot.Collections.Dictionary<T>Godot.Collections.Dictionary 的类型安全包装器. 可使用 Godot.Collections.Dictionary<T>(Godot.Collections.Dictionary) 构造器创建一个.

Variant

System.Object ( object )用来代替 Variant .

与其他脚本语言通信

跨语言脚本 中有详细解释。

Yield

可以使用 C# 的 yield 关键字 来实现类似于 GDScript 中带有单个参数的 yield 的功能。

通过 async/await 和 Godot.Object.ToSignal 可以实现 yield 对信号的相同效果。

示例:

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

其他差异

preload 在 GDScript 中可用,在 C# 中不可用。请使用 GD.LoadResourceLoader.Load 替代。

其他差异:

GDScript

C#

Color8

Color.Color8

is_inf

float.IsInfinity

is_nan

float.IsNaN

dict2inst

待完成

inst2dict

待完成