Professional Documents
Culture Documents
# Inheritance
extends Node2D
# Member variables
var x = 8 # int
var y = 1.2 # float
var b = true # bool
var s = "Hello World!" # String
var a = [1, false, "brown fox"] # Array - similar to list in
Python,
# it can hold different types
# of variables at once.
var d = {
"key" : "value",
42 : true
} # Dictionary holds key-value pairs.
var p_arr = PoolStringArray(["Hi", "there", "!"]) # Pool arrays can
# only hold a
certain type.
# Constants
const ANSWER_TO_EVERYTHING = 42
const BREAKFAST = "Spam and eggs!"
# Enums
enum { ZERO, ONE , TWO, THREE }
enum NamedEnum { ONE = 1, TWO, THREE }
# Functions
func foo():
pass # pass keyword is a placeholder for future code
# Printing values
func printing():
print("GDScript ", "is ", " awesome.")
prints("These", "words", "are", "divided", "by", "spaces.")
printt("These", "words", "are", "divided", "by", "tabs.")
printraw("This gets printed to system console.")
# Math
func doing_math():
var first = 8
var second = 4
print(first + second) # 12
print(first - second) # 4
print(first * second) # 32
print(first / second) # 2
print(first % second) # 0
# There are also +=, -=, *=, /=, %= etc.,
# however no ++ or -- operators.
print(pow(first, 2)) # 64
print(sqrt(second)) # 2
printt(PI, TAU, INF, NAN) # built-in constants
# Control flow
func control_flow():
x = 8
y = 2 # y was originally a float,
# but we can change it's type to int
# using the power of dynamic typing!
if x < y:
print("x is smaller than y")
elif x > y:
print("x is bigger than y")
else:
print("x and y are equal")
var a = true
var b = false
var c = false
if a and b or not c: # alternatively you can use &&, || and !
print("This is true!")
while x > y:
printt(x, y)
y += 1
x = 2
y = 10
while x < y:
x += 1
if x == 6:
continue # 6 won't get printed because of continue statement
prints("x is equal to:", x)
if x == 7:
break # loop will break on 7, so 8, 9 and 10 won't get
printed
match x:
1:
print("Match is similar to switch.")
2:
print("However you don't need to put cases before each
value.")
3:
print("Furthermore each case breaks on default.")
break # ERROR! Break statement is unnecessary!
4:
print("If you need fallthrough use continue.")
continue
_:
print("Underscore is a default case.")
# Casting
func casting_examples():
var i = 42
var f = float(42) # cast using variables constructor
var b = i as bool # or using "as" keyword
# Override functions
# By a convention built-in overridable functions start with an
underscore,
# but in practice you can override virtually any function.
# When overriding you can call parent's function using the dot
operator
# like here:
func get_children():
# Do some additional things here.
var r = .get_children() # call parent's implementation
return r
# Inner class
class InnerClass:
extends Object
func hello():
print("Hello from inner class!")
func use_inner_class():
var ic = InnerClass.new()
ic.hello()
ic.free() # use free for memory cleanup
extends Node2D
func _process(delta):
# Now we can reuse the reference in other places.
prints("Sprite has global_position of", sprite.global_position)
var hp = 10
func apply_damage(dmg):
var hp_old = hp
hp -= dmg
emit_signal("hurt", hp_old, hp) # emit signal and pass arguments
if hp <= 0:
emit_signal("died")
func _ready():
# connect signal "died" to function "_on_death" defined in self
self.connect("died", self, "_on_death")
func _on_death():
self.queue_free() # destroy Player on death
extends Node