C# API differences to 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
와 같은 기타 다른 전역 함수는 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 키워드¶
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.
예시:
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
키워드 대신 [Signal]
속성을 사용하여 시그널을 선언합니다. 이 속성은 delegate에 사용되어야 하며, 이름은 시그널을 정의하는 데 사용됩니다.
[Signal]
delegate void MySignal(string willSendsAString);
See also: C# 시그널.
Singletons¶
싱글톤은 싱글톤 패턴을 사용하는 대신 정적 클래스로 사용할 수 있습니다. 이것으로 Instance
속성으로 작업하는 것과 비슷하면서도 더 적은 문장의 코드를 만들 수 있습니다.
예시:
Input.IsActionPressed("ui_down")
하지만, 일부 드문 경우가 있어 충분하지 않습니다. 예를 들어 Connect
처럼 기본 클래스 Godot.Object
의 멤버로 접근해야하는 경우가 있습니다. 이런 경우를 위해, 싱글톤 인스턴스를 반환하는 Singleton
정적 속성을 제공합니다. 인스턴스의 타입은 Godot.Object
입니다.
예시:
Input.Singleton.Connect("joy_connection_changed", this, nameof(Input_JoyConnectionChanged));
String¶
System.String
(string
)을 사용합니다. 모든 Godot 문자열 메서드는 StringExtensions
클래스에서 확장 메서드 형태로 제공됩니다.
예시:
string upper = "I LIKE SALAD FORKS";
string lower = upper.ToLower();
하지만 몇 가지 차이점은 있습니다:
erase
: C#에서는 문자열이 변경되지 않아서 확장 메서드에 전달된 문자열을 수정할 수 없습니다. 이런 이유로Erase
는 문자열의 확장 메서드가 아닌,StringBuilder
의 확장 메서드 형태로 추가되었습니다. 이를 대체하기 위해string.Remove
를 사용할 수 있습니다.IsSubsequenceOf
/IsSubsequenceOfi
:IsSubsequenceOf
의 오버로드(overload)인 추가 메서드를 제공하여, 명시적으로 대소문자를 구분할 수 있습니다:
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#에서 Struct은 매개변수 없는 생성자를 가질 수 없습니다. 따라서 new Basis()
는 모든 기본 멤버를 기본 값으로 초기화합니다. GDScript와 C++에서 Basis()
에 해당하는 것으로 Basis.Identity
를 사용합니다.
다음 메서드들는 다른 이름으로 변경된 속성들입니다:
GDScript | C# |
---|---|
get_scale() |
Scale |
Transform2D¶
C#에서 Struct는 매개변수 없는 생성자를 가질 수 없습니다. 따라서, new Transform2D()
는 모든 기본 멤버를 기본 값으로 초기화합니다. GDScript와 C++에서 Transform2D()
에 해당하는 것으로 Transform2D.Identity
를 사용해주세요.
다음 메서드들은 관련된 이름으로 변경된 속성들입니다:
GDScript | C# |
---|---|
get_rotation() |
Rotation |
get_scale() |
Scale |
Rect2¶
다음 영역은 약간 다른 이름으로 변경된 속성입니다:
GDScript | C# |
---|---|
end |
End |
다음 메서드들는 다른 이름으로 변경된 속성들입니다:
GDScript | C# |
---|---|
get_area() |
Area |
Quat¶
C#에서 Struct는 매개변수 없는 생성자를 가질 수 없습니다. 따라서, new Quat()
는 모든 기본 멤버를 기본 값으로 초기화합니다. GDScript와 C++에서 Quat()
에 해당하는 것으로 Quat.Identity
를 사용해주세요.
다음 메서드는 다른 이름으로 변경된 속성들입니다:
GDScript | C# |
---|---|
length() |
Length |
length_squared() |
LengthSquared |
배열¶
이것은 일시적인 것입니다. PoolArray도 자신을 의미하는 자체적인 타입이 필요할 것입니다.
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.
딕셔너리¶
Use 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.
Variant¶
Variant
대신 System.Object
(object
)를 사용하세요.
Yield¶
단일 매개변수를 사용하는 GDScript의 yield
와 비슷한 C#의 yield 키워드.
async/await와 Godot.Object.ToSignal
로 시그널에서 yield와 동일한 효과를 얻을 수 있습니다.
예시:
await ToSignal(timer, "timeout");
GD.Print("After timeout");
다른 차이점¶
GDScript에서 쓰이는 preload
는 C#에서 사용할 수 없습니다. 대신 GD.Load
또는 ResourceLoader.Load
를 사용합니다.
다른 차이점:
GDScript | C# |
---|---|
Color8 |
Color.Color8 |
is_inf |
float.IsInfinity |
is_nan |
float.IsNaN |
dict2inst |
TODO |
inst2dict |
TODO |