Generics

2026/05/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
MIT
Game Assets
2016-2026, GDQuest© - All rights reserved
All else
2016-2026, GDQuest© - All rights reserved
Created
2026/06/06
Updated
2026/05/27

Generics

Generic types (often abbreviated to "Generics") is a feature of some programming languages that allows types to use parameters.

In GDScript, this applies only to arrays, and dictionaries which can use the Array[type] or Dictionary[key_type, value_type] syntax to describe what kind of items they are allowed to contain.

And so on. We call any array that restricts the type of its items a "typed array" and any dictionary that restricts the type of its keys and values a "typed dictionary".

Why do I need it?

For example, if you have an array of Strings like so:

var letters_list: Array[String] = ["a", "b", "c"]

Then trying to add something that isn't a String would result in an error:

var letters_list: Array[String] = ["a", "b", "c", 15]
# Error: Cannot include a value of type "int" as "String"

The following also gives an error, but unfortunately only at runtime instead of inside the editor:

letters_list.append(15)
# Error: Attempted to push_back a variable of type 'int' into a TypedArray of type 'String'.

It also gives us strong type guarantees when retrieving an item. In this example, you do not need to type the letter variable:

var letters_list: Array[String] = ["a", "b", "c"]
var letter := letters_list[2]

Godot can infer that the type is a String, thanks to the Array[String] type hint (otherwise, you would have to write var letter: String = letters_list[2]).

For dictionaries, the same applies. If you have a dictionary of String keys and int values:

var players_scores: Dictionary[String, int] = {
	"player 1": 7,
	"player 2": 12
}

It's particularly useful when retrieving items, or looping over a dictionary; consider the example below, where a dictionary is used to store the location of players and enemies on a grid in a turn-based game:

var entities_positions: Dictionary[Vector2i, Node] = {
	Vector2i(1, 2): $Player,
	Vector2i(5, 8): $Enemy
}

for position in entities_positions.keys():
	var entity := entities_positions[position]
	print("Entity at position ", position, " is ", entity.name)

Here, Godot can infer that position is a Vector2i and entity is a Node, which allows us to access their properties and methods without needing to type them explicitly.

Limitations

The type inference isn't perfect. For example:

var letters_list: Array[String] = ["a"]
var letter := letters_list[20]

In this example, letter is null (there aren't 20 items in the array) but Godot assumes it to be a String. Always be aware that Godot cannot know if an array index is valid; it doesn't check boundaries.

It is also not possible to use Array[Array[type]] or Dictionary[Array[type]] to create a typed array of typed arrays. You can use Array[Array], Dictionary[Array], Dictionary[Dictionary] and so on, but the internal elements can't be typed; there can be only one level of typing.

Finally, as I said earlier, the only generic types are Array and Dictionary. No other GDScript type is generic.

Still, even though the type inference is limited and the generic aspect too, typing arrays and dictionaries provides more documentation and helpful hints, so we recommend always using it when possible.

Your own classes

You can also use your own classes. For example, if you had an Item class that contained a String and an int:

class_name Item

var name := ""
var strength := 10

Then you can do this:

var inventory: Array[Item] = [
	Item.new(),
	Item.new(),
	Item.new()
]

Or:

var inventory: Dictionary[String, Item] = {
	"sword": Item.new(),
	"shield": Item.new()
}

Other languages have more generic types?

Yes, most languages that have generics can apply them across many different situations.

For example, TypeScript allows you to have your own custom generics. Here is a dictionary (called "record" in TypeScript) that accepts only strings as keys, but a custom generic type T as values:

interface Record {
	[key: string]: T;
}

const players_scores: Record = {
	"player 1": 7,
	"player 2": 12
}

const players_names: Record = {
	"player 1": "Alice",
	"player 2": "Bob"
}

Here, the term T is replaced by number in the players_scores variable and by string in the players_names variable.

GDScript's limitation is because it doesn't actually have generics like other languages do; the GDScript developers only implemented a specific way to type arrays and dictionaries because it was a feature that was highly requested.

Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!

Site in BETA!found a bug?