Menu

GDScript Static Typing: Types, := and Return Types

How optional static typing works in GDScript: typed variables, := inference, typed parameters and return types, casting with as, and why typed code is faster and safer.

This page includes runnable editors. Edit, run, and see output instantly.

GDScript lets you choose: leave a variable dynamic, or give it a type. A type is written after a colon:

200.0 3 Ana
42

speed and lives are typed. Writing lives = "three" anywhere in this script is an error the editor shows while you type, and the script refuses to run.

Typed Parameters and Return Types

Functions take types too. Put the parameter's type after its name and the return type after ->:

Hit for 40

-> void means the function returns nothing. With a return type, Godot checks every return in the function and every place you use the result.

What Types Catch

Typing turns run-time surprises into errors you see at once. Each of these stops the script before it starts:

var hp: int = "ten"
# Parse Error: Cannot assign a value of type String to variable "hp" with specified type int.

func half(x: int) -> int:
	return "half"
# Parse Error: Cannot return a value of type "String" as "int".

A typed variable can still receive a wrong value from a dynamic one. Godot catches that when the line runs:

SCRIPT ERROR: Trying to assign value of type 'String' to a variable of type 'int'.
          at: _ready (res://main.gd:7)
          GDScript backtrace (most recent call first):
              [0] _ready (res://main.gd:7)

Numbers convert between int and float without an error. A float stored in an int variable drops its decimals, and the editor shows a NARROWING_CONVERSION warning:

3 2.0

Checking and Casting Types

is tests a value's type. as casts a value to a type, and gives null when it is not that type:

an int
a string
a node
float
<null>

In a game you use as on a node you found by name or got from a signal:

func _on_body_entered(body: Node2D) -> void:
	var player := body as Player
	if player:
		player.take_damage(10)

Types You Can Use

  • Built-in types: int, float, bool, String, StringName, Vector2, Vector3, Color, Array, Dictionary, Callable and the rest. See data types.
  • Engine classes: Node, Node2D, Sprite2D, CharacterBody2D, Resource and every other class in the docs.
  • Your own classes, by their class_name.
  • Typed collections: Array[int] (typed arrays) and Dictionary[String, int] (dictionaries).
  • Your enums: var state: State.

When to Type

Type almost everything. Typed code:

  • shows mistakes in the editor before you press Play;
  • gives better autocompletion, because the editor knows what a variable holds;
  • runs faster, since Godot can skip type checks on each operation.

Leave a variable dynamic when it holds different types, such as a value read from a JSON file before you check it. To make Godot insist on types, open Project Settings, turn on Advanced Settings, and under Debug > GDScript set Untyped Declaration to Warn or Error.

Frequently Asked Questions

Is GDScript statically typed?

Optionally. GDScript is dynamic by default, and you add types where you want them: var speed: float = 200.0, func heal(amount: int) -> void:. Typed code is checked as you write it and runs faster.

What does := mean in GDScript?

:= declares a variable and infers its type from the value. var lives := 3 is the same as var lives: int = 3. After that the variable only accepts an int.

Does static typing make GDScript faster?

Yes. When Godot knows the types it uses typed instructions instead of checking each value at run time. Math in loops and calls on typed nodes gain the most. The bigger win is the editor catching mistakes before you run.

How do I make Godot require types?

In Project Settings, turn on Advanced Settings, open Debug > GDScript and set Untyped Declaration to Warn or Error. Godot then flags every variable and function without a type.

Coddy programming languages illustration

Learn to code with Coddy

Get started