Skip to content

GDScript

GDScript를 만든 동기는 무엇입니까?

초기에는 엔진이 Lua 스크립팅 언어를 사용했습니다. Lua는 LuaJIT 덕분에 빠를 수 있지만 객체 지향 시스템에 대한 바인딩 생성(폴백 사용)은 복잡하고 느리며 엄청난 양의 코드가 필요했습니다. Python을 사용한 몇 가지 실험 후에 는 포함하기 어려운 것으로 판명되었습니다.

Godot용 맞춤 스크립팅 언어를 만드는 주된 이유는 다음과 같습니다:

  • 대부분의 스크립트 VM에서 스레딩 지원이 좋지 않으며 (Lua, Python, Squirrel, JavaScript, ActionScript 등) Godot는 스레드를 사용합니다.
  • 대부분의 스크립트 VM에서 클래스 확장 지원이 부족하고 (Lua, Python, JavaScript) Godot 작동 방식에 적응하는 것은 매우 비효율적입니다.
  • 많은 기존 언어에는 C++에 바인딩하기 위한 끔찍한 인터페이스가 있어 많은 양의 코드, 버그, 병목 현상 및 일반적인 비효율성이 발생합니다. (Lua, Python, Squirrel, JavaScript 등)
    • 우리는 많은 수의 통합이 아닌 훌륭한 엔진에 집중하고 싶었습니다.
  • 기본 벡터 유형(vector3, matrix4 등)이 없으므로 (Lua, Python, Squirrel, JavaScript, ActionScript 등) 사용자 지정 유형을 사용할 때 성능이 크게 저하됩니다.
  • 가비지 컬렉터는 정지 또는 불필요하게 많은 메모리 사용을 초래합니다. (Lua, Python, JavaScript, ActionScript 등)
  • 코드 완성, 라이브 편집 등(모두)을 제공하기 위해 코드 편집기와 통합하는 데 어려움이 있습니다.

GDScript는 위의 문제 등을 줄이기 위해 설계되었습니다.

GDScript reference

# Everything after "#" is a comment.
# A file is a class!

# (optional) icon to show in the editor dialogs:
@icon("res://path/to/optional/icon.svg")

# (optional) class definition:
class_name MyClass

# Inheritance:
extends BaseClass

# Member variables.
var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2: 3}
var other_dict = {key = "value", other_key = 2}
var typed_var: int
var inferred_type := "String"

# Constants.
const ANSWER = 42
const THE_NAME = "Charly"

# Enums.
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
enum Named {THING_1, THING_2, ANOTHER_THING = -1}

# Built-in vector types.
var v2 = Vector2(1, 2)
var v3 = Vector3(1, 2, 3)


# Functions.
func some_function(param1, param2, param3):
    const local_const = 5

    if param1 < local_const:
        print(param1)
    elif param2 > 5:
        print(param2)
    else:
        print("Fail!")

    for i in range(20):
        print(i)

    while param2 != 0:
        param2 -= 1

    match param3:
        3:
            print("param3 is 3!")
        _:
            print("param3 is not 3!")

    var local_var = param1 + 3
    return local_var


# Functions override functions with the same name on the base/super class.
# If you still want to call them, use "super":
func something(p1, p2):
    super(p1, p2)


# It's also possible to call another function in the super class:
func other_something(p1, p2):
    super.something(p1, p2)


# Inner class
class Something:
    var a = 10


# Constructor
func _init():
    print("Constructed!")
    var lv = Something.new()
    print(lv.a)

Classes

# Inherit from 'Character.gd'.
extends "res://path/to/character.gd"

# Load character.gd and create a new node instance from it.

var Character = load("res://path/to/character.gd")
var character_node = Character.new()

Single-line extends

If you want to use extends too, you can keep both on the same line:

class_name MyNode extends Node

GDScript: An introduction to dynamic languages

GDScript exports

GDScript documentation comments

GDScript style guide

Static typing in GDScript

GDScript warning system

GDScript format strings

Examples

See also

Favorite site