Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
C# language features¶
이 페이지는 C#과 Godot에서 일반적으로 사용되는 기능과 어떻게 이 둘이 함께 사용되는 지에 대한 개요입니다.
형 변환(Type conversion)과 캐스팅(casting)¶
C#은 정적으로 타입형 언어입니다. 그러므로 다음을 할 수 없습니다:
var mySprite = GetNode("MySprite");
mySprite.SetFrame(0);
The method GetNode()
returns a Node
instance.
You must explicitly convert it to the desired derived type, Sprite2D
in this case.
이를 위해, C#에서는 다양한 설정이 있습니다.
캐스트(Cast)와 타입 체크(Type Check)하기
Throws InvalidCastException
if the returned node cannot be cast to Sprite2D.
You would use it instead of the as
operator if you are pretty sure it won't fail.
Sprite2D mySprite = (Sprite2D)GetNode("MySprite");
mySprite.SetFrame(0);
AS 연산자 사용하기
The as
operator returns null
if the node cannot be cast to Sprite2D,
and for that reason, it cannot be used with value types.
Sprite2D mySprite = GetNode("MySprite") as Sprite2D;
// Only call SetFrame() if mySprite is not null
mySprite?.SetFrame(0);
일반 메서드 사용하기
일반 메서드는 이 타입 변환을 투명하게 만들기 위해 제공됩니다.
GetNode<T>()
은 반환하기 전에 노드를 캐스트 합니다. 노드가 원하는 타입으로 캐스트 되지 않는다면 InvalidCastException
을 발생시킵니다.
Sprite2D mySprite = GetNode<Sprite2D>("MySprite");
mySprite.SetFrame(0);
GetNodeOrNull<T>()
는 as
연산자를 사용하고 노드가 원하는 타입으로 캐스트 되지 않는다면 null
을 반환합니다.
Sprite2D mySprite = GetNodeOrNull<Sprite2D>("MySprite");
// Only call SetFrame() if mySprite is not null
mySprite?.SetFrame(0);
IS 연산자를 사용하여 타입 체크하기
To check if the node can be cast to Sprite2D, you can use the is
operator.
The is
operator returns false if the node cannot be cast to Sprite2D,
otherwise it returns true. Note that when the is
operator is used against null
the result is always going to be false
.
if (GetNode("MySprite") is Sprite2D)
{
// Yup, it's a Sprite2D!
}
if (null is Sprite2D)
{
// This block can never happen.
}
You can also declare a new variable to conditionally store the result of the cast
if the is
operator returns true
.
if (GetNode("MySprite") is Sprite2D mySprite)
{
// The mySprite variable only exists inside this block, and it's never null.
mySprite.SetFrame(0);
}
더 많은 고급 타입 체크하기의 경우, 패턴 일치에서 찾아볼 수 있습니다.
전처리기 지시문¶
Godot은 컴파일하는 환경에 따라 C# 코드를 변경할 수 있도록 여러 전처리기 지시문을 제공합니다.
참고
Godot 3.2 버전 이전에 프로젝트를 생성했다면, csproj 파일을 수정하거나 재생성해야 이 기능(<DefineConstants>
를 3.2 이후의 프로젝트와 비교해보십시오)을 사용할 수 있습니다.
예제¶
예를 들어, 다음과 같이 플랫폼에 따라 코드를 변경할 수 있습니다:
public override void _Ready()
{
#if GODOT_SERVER
// Don't try to load meshes or anything, this is a server!
LaunchServer();
#elif GODOT_32 || GODOT_MOBILE || GODOT_WEB
// Use simple objects when running on less powerful systems.
SpawnSimpleObjects();
#else
SpawnComplexObjects();
#endif
}
혹은 여러분의 코드를 실행하는 엔진의 상태를 확인하는데 사용할 수도 있습니다. 크로스-엔진 라이브러리를 제작하는데 유용한 기능입니다:
public void MyPlatformPrinter()
{
#if GODOT
GD.Print("This is Godot.");
#elif UNITY_5_3_OR_NEWER
print("This is Unity.");
#else
throw new NotSupportedException("Only Godot and Unity are supported.");
#endif
}
Or you can write scripts that target multiple Godot versions and take advantage of features that are only available on some of those versions:
public void UseCoolFeature()
{
#if GODOT4_3_OR_GREATER || GODOT4_2_2_OR_GREATER
// Use CoolFeature, that was added to Godot in 4.3 and cherry-picked into 4.2.2, here.
#else
// Use a workaround for the absence of CoolFeature here.
#endif
}
모든 지시문¶
GODOT
은 항상 Godot 프로젝트를 정의합니다.TOOLS
is defined when building with the Debug configuration (editor and editor player).GODOT_REAL_T_IS_DOUBLE
is defined when theGodotFloat64
property is set totrue
.컴퓨터 환경이 64-bit인지 32-bit인지에 따라서
GODOT_64
와GODOT_32
중 하나가 정의됩니다.One of
GODOT_LINUXBSD
,GODOT_WINDOWS
,GODOT_OSX
,GODOT_ANDROID
,GODOT_IOS
,GODOT_HTML5
, orGODOT_SERVER
depending on the OS. These names may change in the future. These are created from theget_name()
method of the OS singleton, but not every possible OS the method returns is an OS that Godot with .NET runs on.GODOTX
,GODOTX_Y
,GODOTX_Y_Z
,GODOTx_OR_GREATER
,GODOTX_y_OR_GREATER
, andGODOTX_Y_z_OR_GREATER
, whereX
,Y
, andZ
are replaced by the current major, minor and patch version of Godot.x
,y
, andz
are replaced by all values from 0 to the current version number for that component.참고
These defines were first added in Godot 4.0.4 and 4.1. Version defines for prior versions do not exist, regardless of the current Godot version.
For example: Godot 4.0.5 defines
GODOT4
,GODOT4_OR_GREATER
,GODOT4_0
,GODOT4_0_OR_GREATER
,GODOT4_0_5
,GODOT4_0_4_OR_GREATER
, andGODOT4_0_5_OR_GREATER
. Godot 4.3.2 definesGODOT4
,GODOT4_OR_GREATER
,GODOT4_3
,GODOT4_0_OR_GREATER
,GODOT4_1_OR_GREATER
,GODOT4_2_OR_GREATER
,GODOT4_3_OR_GREATER
,GODOT4_3_2
,GODOT4_3_0_OR_GREATER
,GODOT4_3_1_OR_GREATER
, andGODOT4_3_2_OR_GREATER
.
내보내기 를 할 때, 내보내기 기능에 따라서 아래의 것들이 정의될 수 있습니다:
플랫폼에 따라서
GODOT_PC
,GODOT_MOBILE
, 또는GODOT_WEB
중 하나가 정의됩니다.One of
GODOT_WINDOWS
,GODOT_LINUXBSD
,GODOT_MACOS
,GODOT_ANDROID
,GODOT_IOS
, orGODOT_WEB
depending on the platform.
예제 프로젝트를 확인하려면 OS 테스트 데모를 확인하세요: https://github.com/godotengine/godot-demo-projects/tree/master/misc/os_test