C# API 与 GDScript 的差异¶
这是C#和GDScript之间API差异的(不完整)列表。
全局作用域¶
全局函数和某些常量必须移动到类中,因为C#不允许在命名空间中声明它们。大多数全局常量都被移动到它们自己的枚举中。
常量¶
全局常量被移动到它们自己的枚举中。例如,ERR_*
常量被移动到 Error
枚举中。
特殊情况:
GDScript | C# |
---|---|
SPKEY |
GD.SpKey |
TYPE_* |
Variant.Type 枚举 |
OP_* |
Variant.Operator 枚举 |
数学函数¶
如 abs
、 acos
、 asin
、atan
和 atan2
这样的全局数学函数位于 Mathf
下,名为 Abs
、Acos
、 Asin
、Atan
和 Atan2
。常数 PI
可以通过 Mathf.Pi
获得。
随机函数¶
如 rand_range
和 rand_seed
等全局随机函数位于 GD
下。例如 GD.RandRange
以及 GD.RandSeed
。
其他函数¶
许多其他的全局函数,如 print
和 var2str
位于C#中的 GD
下。例如:GD.Print
和 GD.Var2Str
。
例外情况:
GDScript | C# |
---|---|
weakref(obj) |
Object.WeakRef(obj) |
is_instance_valid(obj) |
Object.IsInstanceValid(obj) |
提示¶
有时候,使用 using static
指令是很有用的。该指令允许在不指定类名的情况下,访问类的成员和嵌套类型。
示例:
using static Godot.GD;
public class Test
{
static Test()
{
Print("Hello"); // Instead of GD.Print("Hello");
}
}
导出关键字¶
``[Export]``属性可以代替GDScript中的``export``关键词。该属性还可以接受:ref:`PropertyHint<enum_@GlobalScope_PropertyHint>`和``hintString``参数。默认可以设为任何值。
示例:
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]
属性代替GDScript中的 signal
关键字。该属性应该用在 delegate
上,其名称签名将用于定义信号。
[Signal]
delegate void MySignal(string willSendsAString);
另见: c_sharp_signals。
单例¶
单例可以作为静态类使用,而不是使用单例模式。这是为了使代码不像使用 Instance
属性那样冗长。
示例:
Input.IsActionPressed("ui_down")
但是,在极少数情况下,这还不够。例如,您可能希望访问基类 Godot.Object
中的成员,比如 Connect
。对于此类情况,我们提供了一个名为 Singleton
的静态属性,该属性返回单例实例。这个实例的类型是 Godot.Object
。
示例:
Input.Singleton.Connect("joy_connection_changed", this, nameof(Input_JoyConnectionChanged));
字符串¶
使用 System.String
( string
)。所有Godot String方法都由 StringExtensions
类作为扩展方法提供。
示例:
string upper = "I LIKE SALAD FORKS";
string lower = upper.ToLower();
但是有一些区别:
erase
:字符串在C#中是不可变的,因此我们无法修改传递给扩展方法的字符串。出于这个原因,Erase
被添加为StringBuilder
的扩展方法而不是字符串。或者您可以使用string.Remove
。IsSubsequenceOf
/IsSubsequenceOfi
:提供了另一种方法,它是IsSubsequenceOf
的重载,允许明确指定区分大小写:
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
:除了Match
和Matchn
之外还提供了另一种方法,它允许明确指定区分大小写:
str.Match("*.txt"); // Case sensitive
str.ExprMatch("*.txt", true); // Case sensitive
str.Matchn("*.txt"); // Case insensitive
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 |
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
对信号的相同效果。
示例:
await ToSignal(timer, "timeout");
GD.Print("After timeout");
其他差异¶
preload
在GDScript中可用,在C#中不可用。请使用 GD.Load
或 ResourceLoader.Load
替代。
其他差异:
GDScript | C# |
---|---|
Color8 |
Color.Color8 |
is_inf |
float.IsInfinity |
is_nan |
float.IsNaN |
dict2inst |
待完成 |
inst2dict |
待完成 |