Skip to content
Search

GDScript

lastmod 2023-04-13created 2022-05-229 edits

What were the motivations behind creating GDScript?

In the early days, the engine used the Lua scripting language. Lua can be fast thanks to LuaJIT, but creating bindings to an object-oriented system (using fallbacks) was complex and slow and required an enormous amount of code. After some experiments with Python, it also proved difficult to embed.

The main reasons for creating a custom scripting language for Godot were:

  • Poor threading support in most script VMs (Lua, Python, Squirrel, JavaScript, ActionScript, etc.), and Godot uses threads.
  • Poor class-extending support in most script VMs (Lua, Python, JavaScript), and adapting to the way Godot works is highly inefficient.
  • Many existing languages have horrible interfaces for binding to C++, resulting in large amounts of code, bugs, bottlenecks, and general inefficiency. (Lua, Python, Squirrel, JavaScript, etc.)
    • We wanted to focus on a great engine, not a great number of integrations.
  • No native vector types (vector3, matrix4, etc.) (Lua, Python, Squirrel, JavaScript, ActionScript, etc.), so performance drops considerably when using custom types.
  • Garbage collectors result in stalls or unnecessarily large memory usage. (Lua, Python, JavaScript, ActionScript, etc.)
  • Difficulty integrating with the code editor to provide code completion, live editing, etc. (all of them).

GDScript was designed to reduce the above issues and more.

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:

class_name MyNode extends Node

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

See also

Favorite site

author Zer0 sitemap