2021-11-01 21:12:55 +00:00
|
|
|
---
|
2024-12-09 11:34:00 +00:00
|
|
|
name: GDScript
|
2021-11-01 21:12:55 +00:00
|
|
|
contributors:
|
|
|
|
- ["Wichamir", "https://github.com/Wichamir/"]
|
2024-05-21 15:31:08 +00:00
|
|
|
- ["zacryol", "https://github.com/zacryol"]
|
2021-11-01 21:12:55 +00:00
|
|
|
filename: learngdscript.gd
|
|
|
|
---
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
GDScript is a dynamically and statically typed scripting language
|
|
|
|
for the free and open source game engine Godot. Its syntax is vaguely
|
|
|
|
similar to Python's. Its main advantages are ease of use and tight
|
|
|
|
integration with the engine. It's a perfect fit for game development.
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
## Basics
|
|
|
|
|
2024-04-08 14:07:03 +00:00
|
|
|
```gdscript
|
2021-11-01 21:12:55 +00:00
|
|
|
# Single-line comments are written using hash symbol.
|
|
|
|
"""
|
|
|
|
Multi-line
|
|
|
|
comments
|
|
|
|
are
|
|
|
|
written
|
|
|
|
using
|
2024-05-21 15:31:08 +00:00
|
|
|
triple
|
|
|
|
quoted
|
|
|
|
strings
|
2021-11-01 21:12:55 +00:00
|
|
|
"""
|
|
|
|
|
2024-09-07 12:45:07 +00:00
|
|
|
# Doc Comments can add a description to classes and fields
|
2024-05-21 15:31:08 +00:00
|
|
|
# which can be viewed in the in-engine docs.
|
|
|
|
|
|
|
|
## This class is a demonstration of GDScript
|
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
# Script file is a class in itself and you can optionally define a name for it.
|
|
|
|
class_name MyClass
|
|
|
|
|
|
|
|
# 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.
|
2024-05-21 15:31:08 +00:00
|
|
|
var p_arr = PackedStringArray(["Hi", "there", "!"]) # Packed Arrays can
|
|
|
|
# only hold a certain type.
|
|
|
|
|
|
|
|
# Doc comments can apply to properties
|
|
|
|
|
|
|
|
## How many times this object has jumped
|
|
|
|
var jump_count = 0
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
# Built-in vector types:
|
|
|
|
var v2 = Vector2(1, 2)
|
|
|
|
var v3 = Vector3(1, 2, 3)
|
|
|
|
|
|
|
|
# Constants
|
|
|
|
const ANSWER_TO_EVERYTHING = 42
|
|
|
|
const BREAKFAST = "Spam and eggs!"
|
|
|
|
|
|
|
|
# Enums
|
|
|
|
enum { ZERO, ONE , TWO, THREE }
|
|
|
|
enum NamedEnum { ONE = 1, TWO, THREE }
|
|
|
|
|
|
|
|
# Exported variables are visible in the inspector.
|
2024-05-21 15:31:08 +00:00
|
|
|
#
|
|
|
|
# Either a type hint (explained later) or a default value are needed in order
|
|
|
|
# for the editor to know what options to give
|
|
|
|
@export var age: int
|
|
|
|
@export var height: float
|
|
|
|
@export var person_name = "Bob"
|
|
|
|
# But both is also acceptable
|
|
|
|
@export var favorite_color: String = "Green"
|
|
|
|
@export var favorite_food := "Pizza"
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
# Functions
|
|
|
|
func foo():
|
|
|
|
pass # pass keyword is a placeholder for future code
|
|
|
|
|
|
|
|
func add(first, second):
|
|
|
|
return first + second
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
# Doc Comments on functions
|
|
|
|
|
|
|
|
## Increases the Jump Count
|
|
|
|
func jump():
|
|
|
|
jump_count += 1
|
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
# 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.")
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
# Lambdas
|
|
|
|
var my_lambda = func(): print("hello from lambda!")
|
|
|
|
|
|
|
|
my_lambda.call()
|
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
# 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,
|
2022-02-14 15:51:26 +00:00
|
|
|
# but we can change its type to int
|
2021-11-01 21:12:55 +00:00
|
|
|
# using the power of dynamic typing!
|
2024-05-21 15:31:08 +00:00
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
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")
|
2024-05-21 15:31:08 +00:00
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
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!")
|
|
|
|
|
|
|
|
for i in range(20): # GDScript's range is similar to Python's
|
|
|
|
print(i) # so this will print numbers from 0 to 19
|
|
|
|
|
2022-02-14 15:58:43 +00:00
|
|
|
for i in 20: # unlike Python, you can loop over an int directly
|
|
|
|
print(i) # so this will also print numbers from 0 to 19
|
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
for i in ["two", 3, 1.0]: # iterating over an array
|
|
|
|
print(i)
|
2024-05-21 15:31:08 +00:00
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
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.")
|
2024-05-21 15:31:08 +00:00
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
# ternary operator (one line if-else statement)
|
|
|
|
prints("x is", "positive" if x >= 0 else "negative")
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
# _init is called when object gets initialized
|
|
|
|
# This is the object's constructor.
|
|
|
|
func _init():
|
|
|
|
# Initialize object's internal stuff here.
|
|
|
|
pass
|
|
|
|
|
|
|
|
# _ready gets called when script's node and
|
|
|
|
# its children have entered the scene tree.
|
|
|
|
func _ready():
|
|
|
|
pass
|
|
|
|
|
|
|
|
# _process gets called on every frame.
|
|
|
|
func _process(delta):
|
|
|
|
# The delta argument passed to this function is a number of seconds,
|
|
|
|
# which passed between the last frame and the current one.
|
|
|
|
print("Delta time equals: ", delta)
|
|
|
|
|
|
|
|
# _physics_process gets called on every physics frame.
|
|
|
|
# That means delta should be constant.
|
|
|
|
func _physics_process(delta):
|
|
|
|
# Simple movement using vector addition and multiplication.
|
|
|
|
var direction = Vector2(1, 0) # or Vector2.RIGHT
|
|
|
|
var speed = 100.0
|
|
|
|
self.global_position += direction * speed * delta
|
|
|
|
# self refers to current class instance
|
|
|
|
|
|
|
|
# When overriding you can call parent's function using the dot operator
|
|
|
|
# like here:
|
|
|
|
func get_children():
|
|
|
|
# Do some additional things here.
|
2024-05-21 15:31:08 +00:00
|
|
|
var r = super() # call parent's implementation
|
2021-11-01 21:12:55 +00:00
|
|
|
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
|
|
|
|
```
|
|
|
|
|
|
|
|
## Accessing other nodes in the scene tree
|
|
|
|
|
2024-04-08 14:07:03 +00:00
|
|
|
```gdscript
|
2021-11-01 21:12:55 +00:00
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
var sprite # This variable will hold the reference.
|
|
|
|
|
|
|
|
# You can get references to other nodes in _ready.
|
|
|
|
func _ready() -> void:
|
|
|
|
# NodePath is useful for accessing nodes.
|
|
|
|
# Create NodePath by passing String to its constructor:
|
|
|
|
var path1 = NodePath("path/to/something")
|
|
|
|
# Or by using NodePath literal:
|
2024-05-21 15:31:08 +00:00
|
|
|
var path2 = ^"path/to/something"
|
2021-11-01 21:12:55 +00:00
|
|
|
# NodePath examples:
|
2024-05-21 15:31:08 +00:00
|
|
|
var path3 = ^"Sprite" # relative path, immediate child of the current node
|
|
|
|
var path4 = ^"Timers/Firerate" # relative path, child of the child
|
|
|
|
var path5 = ^".." # current node's parent
|
|
|
|
var path6 = ^"../Enemy" # current node's sibling
|
|
|
|
var path7 = ^"/root" # absolute path, equivalent to get_tree().get_root()
|
|
|
|
var path8 = ^"/root/Main/Player/Sprite" # absolute path to Player's Sprite
|
|
|
|
var path9 = ^"Timers/Firerate:wait_time" # accessing properties
|
|
|
|
var path10 = ^"Player:position:x" # accessing subproperties
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
# Finally, to get a reference use one of these:
|
2024-05-21 15:31:08 +00:00
|
|
|
sprite = get_node(^"Sprite") as Sprite # always cast to the type you expect
|
2021-11-01 21:12:55 +00:00
|
|
|
sprite = get_node("Sprite") as Sprite # here String gets
|
|
|
|
# implicitly casted to NodePath
|
|
|
|
sprite = get_node(path3) as Sprite
|
|
|
|
sprite = get_node_or_null("Sprite") as Sprite
|
|
|
|
sprite = $Sprite as Sprite
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
# Now we can reuse the reference in other places.
|
|
|
|
prints("Sprite has global_position of", sprite.global_position)
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
# Use @onready annotation to assign a value to
|
2021-11-01 21:12:55 +00:00
|
|
|
# a variable just before _ready executes.
|
|
|
|
# This is a commonly used syntax sugar.
|
2024-05-21 15:31:08 +00:00
|
|
|
@onready var other_sprite = $Sprite as Sprite
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
# You can export NodePath, so you can assign it within the inspector.
|
2024-05-21 15:31:08 +00:00
|
|
|
@export var nodepath = ^""
|
|
|
|
@onready var reference = get_node(nodepath) as Node
|
|
|
|
|
|
|
|
# Or export Node directly
|
|
|
|
@export var other_reference: Node
|
2021-11-01 21:12:55 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Signals
|
|
|
|
|
|
|
|
Signal system is Godot's implementation of the observer programming
|
|
|
|
pattern. Here's an example:
|
|
|
|
|
2024-04-08 14:07:03 +00:00
|
|
|
```gdscript
|
2021-11-01 21:12:55 +00:00
|
|
|
class_name Player extends Node2D
|
|
|
|
|
|
|
|
var hp = 10
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
# Doc comments can go on signals too
|
|
|
|
|
|
|
|
## Emitted when the player dies
|
2021-11-01 21:12:55 +00:00
|
|
|
signal died() # define signal
|
|
|
|
signal hurt(hp_old, hp_new) # signals can take arguments
|
|
|
|
|
|
|
|
func apply_damage(dmg):
|
|
|
|
var hp_old = hp
|
|
|
|
hp -= dmg
|
2024-05-21 15:31:08 +00:00
|
|
|
hurt.emit(hp_old, hp) # emit signal and pass arguments
|
2021-11-01 21:12:55 +00:00
|
|
|
if hp <= 0:
|
2024-05-21 15:31:08 +00:00
|
|
|
died.emit()
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
# connect signal "died" to function "_on_death" defined in self
|
2024-05-21 15:31:08 +00:00
|
|
|
died.connect(_on_death)
|
|
|
|
# Alternate way
|
|
|
|
# needed if the target object is not self
|
|
|
|
# died.connect(Callable(self, &"_on_death"))
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
func _on_death():
|
2024-05-21 15:31:08 +00:00
|
|
|
queue_free() # destroy Player on death
|
2021-11-01 21:12:55 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Type hints
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
GDScript can optionally use static typing, for both code clarity and
|
|
|
|
performance benefits.
|
2021-11-13 23:28:21 +00:00
|
|
|
|
2024-04-08 14:07:03 +00:00
|
|
|
```gdscript
|
2021-11-01 21:12:55 +00:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
var x: int # define typed variable
|
|
|
|
var y: float = 4.2
|
|
|
|
var z := 1.0 # infer type based on default value using := operator
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
var a: Array[int] = [1, 2, 3] # Array can also have its type content specified
|
2021-11-01 21:12:55 +00:00
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
enum NamedEnum { ONE = 1, TWO, THREE }
|
|
|
|
var n: NamedEnum = NamedEnum.ONE # Enums can be used as types as well
|
|
|
|
|
|
|
|
@onready var node_ref_typed := $Child as Node
|
|
|
|
|
|
|
|
@export var speed := 50.0
|
2021-11-01 21:12:55 +00:00
|
|
|
|
|
|
|
const CONSTANT := "Typed constant."
|
|
|
|
|
2024-05-21 15:31:08 +00:00
|
|
|
signal example(arg: int)
|
|
|
|
|
2021-11-01 21:12:55 +00:00
|
|
|
func _ready() -> void:
|
|
|
|
# function returns nothing
|
|
|
|
x = "string" # ERROR! Type can't be changed!
|
2024-05-21 15:31:08 +00:00
|
|
|
a.append("q") # ERROR! Array[int] can't hold strings!
|
2021-11-01 21:12:55 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
func join(arg1: String, arg2: String) -> String:
|
|
|
|
# function takes two Strings and returns a String
|
|
|
|
return arg1 + arg2
|
|
|
|
|
|
|
|
func get_child_at(index: int) -> Node:
|
|
|
|
# function takes an int and returns a Node
|
|
|
|
return get_children()[index]
|
|
|
|
```
|
|
|
|
|
|
|
|
## Further Reading
|
|
|
|
|
|
|
|
* [Godot's Website](https://godotengine.org/)
|
|
|
|
* [Godot Docs](https://docs.godotengine.org/en/stable/)
|
|
|
|
* [Getting started with GDScript](https://docs.godotengine.org/en/stable/getting_started/scripting/gdscript/index.html)
|
|
|
|
* [NodePath](https://docs.godotengine.org/en/stable/classes/class_nodepath.html)
|
|
|
|
* [Signals](https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html)
|
|
|
|
* [GDQuest](https://www.gdquest.com/)
|
2022-02-14 15:58:43 +00:00
|
|
|
* [GDScript.com](https://gdscript.com/)
|