Generics
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
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, which can use the Array[type] syntax to describe what kind of items they are allowed to contain.
Array[String] is an array of StringsArray[int] is an array of intsArray[Vector2] is an array of Vector2sAnd so on. We call any array that restricts the type of its items a TypedArray.
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]).
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]] to create a typed array of typed arrays. You can use Array[Array], but the internal arrays can't be typed.
Finally, as I said earlier, the only generic type is Array. No other GDScript type is generic.
Still, even though the type inference is limited and the generic aspect too, typing arrays provides more documentation and helpful hints, so we recommend always using it when possible.
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 := 10Then you can do this:
var inventory: Array[Item] = [
Item.new(),
Item.new(),
Item.new()
]Yes, most languages that have generics can apply them across many different situations.
For example, TypeScript allows you to have typed dictionaries. Here is a dictionary (called "record" in TypeScript) that accepts only strings as keys and numbers as values:
var players_scores: Record = {
"player 1": 7,
"player 2": 12
} 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 because it was a feature that was highly requested.
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.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…