GDScript
Table of Contents
- Overview of GDScript
- Signals
- Coroutines with
yield
- Inheritance
- Define virtual methods
- GDScript file naming convention?
match
Overview of GDScript tutorial
# A file is a class!
# Inheritance
extends BaseClass
# Member Variables
var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2:3}
# 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)
# Function
func some_function(param1, param2):
var local_var = 5
if param1 < local_var:
print(param1)
elif param2 > 5:
print(param2)
else:
print("Fail!")
for i in range(20):
print(i)
while param2 != 0:
param2 -= 1
var local_var2 = param1 + 3
return local_var2
# Functions override functions with the same name on the base/parent class.
# If you still want to call them, use '.' (like 'super' in other languages).
func something(p1, p2):
.something(p1, p2)
# Inner Class
class Something:
var a = 10
# Constructor
func _init():
print("Constructed!")
var lv = Something.new()
print(lv.a)
Signals discussion
func _callback_no_args():
print("Got callback!")
func _callback_args(a,b):
print("Got callback with args! a: ", a, " and b: ", b)
func connect_signals():
instance.connect("your_signal_name", self, "_callback_no_args")
instance.connect("your_signal_name_with_args", self, "_callback_args")
# bind arguments to a signal that lacks them
instance.connect("your_signal_name", self, "_callback_args", [22, "hello"])
func emit_signals():
emit_signal("your_signal_name")
emit_signal("your_signal_name_with_args", 55, 128)
some_instance.emit_signal("some_signal")
Coroutines with yield
discussion
func my_func():
print("Hello")
yield()
print("world")
func _ready():
var y = my_func()
# Function state saved in 'y'.
print("my dear")
y.resume()
# 'y' resumed and is now an invalid state.
func my_func():
print("Hello")
print(yield())
return "cheers!"
func _ready():
# `y` is just a GDScriptFunctionState instance.
# There appears no way to get values from the coroutine
# with intermediate return value.
var y = my_func()
# Function state saved in 'y'.
print(y.resume("world"))
# 'y' resumed and is now an invalid state.
When calling
yield(object, signal)
, the coroutine will automatically resume when the signal in the object emits.
- http://docs.godotengine.org/en/3.0/classes/class_@gdscript.html#class-gdscript-yield
- http://docs.godotengine.org/en/3.0/classes/class_gdscriptfunctionstate.html#class-gdscriptfunctionstate
- https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html#coroutines-with-yield
Inheritance discussion
# Inherit/extend a globally available class.
extends SomeClass
# Inherit/extend a named class file.
extends "somefile.gd"
# Inherit/extend an inner class in another file.
extends "somefile.gd".SomeInnerClass
# Use 'is' to check inheritance.
if (entity is Enemy):
entity.apply_damage()
func some_func(x):
.some_func(x) # Calls same function on the parent class.
Define virtual methods howto
- There appears nothing called virtual method, but you can only override methods.
- So, if you want a pure virtual method, you can just define the method signature with
pass
.
GDScript file naming convention? discussion
- The popular vote seems to be lower snakecase for the actual file name.
- Like
player.gd
instead ofPlayer.gd
- This is because most of OS-compatibility.
match
reference
# constant
match x:
1:
print("We are number one!")
2:
print("Two are better than one!")
3, 4, 5:
print("Three or four or five!")
"test":
print("Oh snap! It's a string!")
_:
print("Default")
- You can also use
binding pattern
,array pattern
,dictionary pattern
.