Skip to content

Godot:Script

godot 스크립팅.

처리(Process)

정적 처리

매 프레임마다 호출되는 정적 처리:

public override void _Process(float delta)
{
    // Do something...
}

_Process()가 호출되는 비중은 응용 프로그램이 초 당 몇 프레임 (FPS)으로 실행되는지에 따라 다르다는 것을 마음에 새기는 것이 중요합니다. 이 비율은 시간과 기기에 따라 다를 수 있습니다.

이러한 가변성을 관리하기 위해 delta 매개 변수에는 _Process()에 대한 이전 호출 이후 부동 소수점 숫자로 경과 된 시간(초)이 포함됩니다.

동적 처리

항상 동적 단계는 고정된 시간 간격으로 호출된다. 기본은 초 당 60회. 프로젝트 설정(Project Settings)에서 Physics -> Common -> Physics Fps에서 시간 간격을 변경할 수 있다:

public override void _PhysicsProcess(float delta)
{
    // Do something...
}

재정의 가능 함수

public override void _EnterTree()
{
    // When the node enters the Scene Tree, it becomes active
    // and  this function is called. Children nodes have not entered
    // the active scene yet. In general, it's better to use _ready()
    // for most cases.
    base._EnterTree();
}

public override void _Ready()
{
    // This function is called after _enter_tree, but it ensures
    // that all children nodes have also entered the Scene Tree,
    // and became active.
    base._Ready();
}

public override void _ExitTree()
{
    // When the node exits the Scene Tree, this function is called.
    // Children nodes have all exited the Scene Tree at this point
    // and all became inactive.
    base._ExitTree();
}

public override void _Process(float delta)
{
    // This function is called every frame.
    base._Process(delta);
}

public override void _PhysicsProcess(float delta)
{
    // This is called every physics frame.
    base._PhysicsProcess(delta);
}

그룹

거대한 씬을 조직하는데 유용한 기능. GUI에서 Node 패널 하단의 Groups 에서 추가할 수 있다.

코드로 추가하고 싶다면:

public override void _Ready()
{
    base._Ready();

    AddToGroup("enemies");
}

모든 그룹의 특정 함수를 호출하고 싶다면 SceneTree.CallGroup()을 사용하면 된다.

public void _OnDiscovered() // This is a purely illustrative function.
{
    GetTree().CallGroup("enemies", "player_was_discovered"); // 그룹 enemies 의 모든 구성원이 player_was_discovered 함수를 호출합니다.
}

또한 SceneTree.GetNodesInGroup()을 호출하여 그룹의 전체 노드를 가져올 수도 있습니다:

var enemies = GetTree().GetNodesInGroup("enemies");

노드 (Node)

다음과 같이 노드를 만들 수 있다.

private Sprite _sprite;

public override void _Ready()
{
    base._Ready();

    _sprite = new Sprite(); // Create a new sprite!
    AddChild(_sprite); // Add it as a child of this node.
}

노드 삭제는 Free()함수를 사용하면 된다. 함수가 호출되면 그 즉시 모든 하위 노드도 삭제됩니다.

WARNING

현재 "차단된" 노드를 삭제하려는 상황이 발생할 수 있습니다. 차단된 노드는 시그널을 방출하고 있거나 함수를 호출하고 있죠. 이것은 게임을 망칠 것입니다. 디버거(Debugger)로 Godot를 실행하면 이 경우를 발견하고 경고를 표시하는 경우가 많습니다.

노드를 삭제하는 가장 안전한 방법은 Node.QueueFree()를 사용하는 것입니다. 이것은 대기 상태일 때 노드를 안전하게 삭제합니다.

public void _SomeAction()
{
    _sprite.QueueFree(); // Removes the node from the scene and frees it when it becomes safe to do so.
}

씬 인스턴스

코드에서 씬을 인스턴스하는 작업은 두 단계로 이루어집니다. 첫 번째 방법은 하드 드라이브에서 씬을 불러오는 것입니다:

var scene = GD.Load<PackedScene>("res://myscene.tscn"); // Will load when the script is instanced.

그러나 씬(Scene)은 아직 노드가 아닙니다. 이것은 PackedScene (포장된 씬)이라는 특수 리소스에 들어 있습니다. 실제 노드를 만들려면 PackedScene.instance() 함수를 호출해야 합니다. 그러면 PackedScene은 활성 씬에 추가할 수 있는 노드 트리를 반환합니다:

var node = scene.Instance();
AddChild(node);

이 두 단계 처리의 이점은 PackedScene이 불러온 상태로 유지되고 언제나 사용될 준비를 합니다. 이렇게 해서 인스턴스를 원하는 만큼 만들 수 있습니다. 이것은 특히 활성 씬에서 많은 적, 총알, 그 외 다른 개체를 신속하게 인스턴스하는 데 유용합니다.

Script

See also

Favorite site