GDScript Reference
Overview
- GDScript is a high-level, gradually typed programming language for Godot Engine.
- It uses indentation-based syntax similar to Python but is independent of Python.
- GDScript is optimized for integration with Godot Engine.
GDScript Syntax
- Comments: Use
# for single-line comments; ## for documentation comments.
- Identifiers: Must not start with a digit, case-sensitive, can include
_ and Unicode characters.
- Keywords: Reserved words like
if, else, for, while, match, class, etc.
- Operators: Include
+, -, *, /, &&, ||, ==, !=, etc.
- Literals: Include
null, true, false, strings, integers, and floats.*_
Variables and Constants
- Variables: Defined with
var, can have inferred or explicit types.
- Constants: Defined with
const, values must be known at compile-time.
- Enums: Used to define a set of constants.
Functions
- Definition: Use
func keyword; can have typed arguments and return types.
- Lambda Functions: Anonymous functions defined with
func keyword.
- Static Functions: Functions that do not access instance member variables.
Classes
- Inheritance: Use
extends keyword.
- Inner Classes: Defined using the
class keyword within another class.
- Named Classes: Use
class_name to register globally accessible classes.
- Static Variables: Shared across all instances of a class.
Control Flow
- Conditions: Use
if, elif, else for conditional logic.
- Loops: Use
for and while for iteration.
- Match Statement: Similar to
switch but with more features, uses patterns.
- Assert: Used for debugging, ignored in release builds.
Properties
- Setters/Getters: Use
set and get to define property access behavior.
Annotations
- Usage: Modify script behavior, e.g.,
@export, @onready, @tool.
Signals
- Definition: Use
signal keyword, used for event-driven programming.
- Connection: Signals can connect to methods using
connect().
Memory Management
- Reference Counting: Used for memory management;
RefCounted instances are freed automatically.
- WeakRef: Prevents circular references, allowing objects to be garbage collected.
Miscellaneous
- Tool Mode: Enable script execution in the Godot editor with
@tool.
- Await: Used to pause execution until a signal is emitted or a coroutine finishes.
- Casting: Use
as for type casting.
These notes encapsulate the basic structure and functionality of GDScript as outlined in the documentation.