GODOT3 GDSCRIPT 문법 메모
<코드순서>
01. tool
02. class_name
03. extends
04. # docstring
05. signals
06. enums
07. constants
08. exported variables
09. public variables
10. private variables
11. onready variables
12. optional built-in virtual _init method
13. built-in virtual _ready method
14. remaining built-in virtual methods
15. public methods
16. private methods
++++++++++++++++++++++++++++++++++++++
if param1 < local_var:
elif param2 > 5:
else:
for s in strings:
while i < strings.size():
match x:
1:
2:
"test":
enum {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT}
var arr = []
var b = arr[1]
var dic = {4: 5, "A key": "A value", 28: [1, 2, 3]}
d["Hi!"] = 0
Setter(세터)/Getter(게터)
var variable = value setget setterfunc, getterfunc
삼항(Ternary) 연산자
next_state = "fall" if not is_on_floor() else "idle"
불리언(Boolean)
if (foo and bar) or baz:
print("hello world")
print('hello "world"')
노드 만들기, 지우기
s = Sprite.new()
add_child(s)
s.queue_free()
씬 인스턴스 하기
var scene = preload("res://myscene.tscn")
var node = scene.instance()
add_child(node)
스크립트를 클래스로 등록하기
class_name ScriptName, "res://path/to/optional/icon.svg"
var cppNode = MyCppNode.new() # new instance of a class named MyCppNode
선택 정적 타이핑
var health: int = 0
func heal(amount: int) -> void:
onready var health_bar: ProgressBar = get_node("UI/LifeBar")
시그널
signal health_changed
emit_signal("health_changed", old_health, health)
func _on_Character_health_changed(old_value, new_value):
yield를 가진 코루틴(Coroutine)
yield()
y.resume()
yield(button_func(), "completed")
시그널+yield
# Resume execution when animation is done playing.
yield(get_node("AnimationPlayer"), "animation_finished")
onready var my_label = get_node("MyLabel")
스크립트를 로딩해서 클래스 타입으로 사용
const Rifle = preload("res://player/weapons/Rifle.gd")
var my_rifle: Rifle
class_name을 사용한다면, Godot는 Rifle 타입을 편집기에서
전역으로 등록합니다, 그리고 상수에 미리 불러올 필요 없이 어디에나 그것을 쓸 수 있습니다:
extends Node2D
class_name Rifle
var my_rifle: Rifle
변수 캐스팅
func _on_body_entered(body: PhysicsBody2D) -> void:
var player := body as PlayerController
화살표로 함수의 반환 타입 정의하기
func _process(delta: float) -> void:
pass