See all glossary terms

Generics

Generic types (often abbreviated to "Generics") is a feature of some languages that allow 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 Strings
  • Array[int] is an array of ints
  • Array[Vector2] is an array of Vector2s
And so on. We call any array that restricts the type of its items a TypedArray.

Why do I need it?

For example, if you had 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.
For example, in this example, you do not need to type the letter variable:
var letters_list: Array[String] = [ "a", "b", "c", 15 ]
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]).

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]] 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.

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()
]

Other languages have more generic types?

Yes, most languages that do have generics can apply them accross many different situations.
For example, Typescript allows 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<string, number> = {
    "player 1": 7
    "player 2": 12
}
GSscript'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 really requested.

See Also

Related terms in the Glossary