GDScript
GDScript를 만든 동기는 무엇입니까?
초기에는 엔진이 Lua 스크립팅 언어를 사용했습니다. Lua는 LuaJIT 덕분에 빠를 수 있지만 객체 지향 시스템에 대한 바인딩 생성(폴백 사용)은 복잡하고 느리며 엄청난 양의 코드가 필요했습니다. Python을 사용한 몇 가지 실험 후에 는 포함하기 어려운 것으로 판명되었습니다.
Godot용 맞춤 스크립팅 언어를 만드는 주된 이유는 다음과 같습니다:
- 대부분의 스크립트 VM에서 스레딩 지원이 좋지 않으며 (Lua, Python, Squirrel, JavaScript, ActionScript 등) Godot는 스레드를 사용합니다.
- 대부분의 스크립트 VM에서 클래스 확장 지원이 부족하고 (Lua, Python, JavaScript) Godot 작동 방식에 적응하는 것은 매우 비효율적입니다.
- 많은 기존 언어에는 C++에 바인딩하기 위한 끔찍한 인터페이스가 있어 많은 양의 코드, 버그, 병목 현상 및 일반적인 비효율성이 발생합니다. (Lua, Python, Squirrel, JavaScript 등)
- 우리는 많은 수의 통합이 아닌 훌륭한 엔진에 집중하고 싶었습니다.
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:
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
- Godot:Examples:FileOpenScene - GDScript를 사용하여 파일 오픈 대화상자 구현.