So you'd need to do this: ($AnimationPlayer as AnimationPlayer).play("walk") Since the play() function is not defined on the inferred type (Node), the engine don't know at this point if it really exists, nor the types for arguments and returns.Ī statically typed language, such as C++ and C#, would force you to explicitly cast to the specific subtype, otherwise it would throw an error. With the type inference, Godot knows that $AnimationPlayer is a Node but can't tell which subtype it really is (in this case an AnimationPlayer). Something very common is to tell an animation player to start like this: $ay("walk") The reasoning behind this is that the duck-typing should still work as it always did, so it's not possible to error if some variable or function is not defined on the accessed class. Introducing the concept of "safe lines"Īs suggested by our dear core developer reduz (Juan Linietsky), GDScript now has "safe lines" marked in the editor. The type can be: a built-in Variant type ( int, Vector3, Color, Basis, etc.) a core class ( Node, Resource, File, etc.) a named constant that contains a Script resource ( const M圜lass = preload("res://my_class.gd") a named script class (one made with the class_name syntax). Var my_sprite : Sprite = $Sprite as Sprite For objects it simply returns null: var number_input : int = $LineEdit.text as int For built-in types, it errors out if it's not possible to convert. For the return type, you use a forward arrow ( ->) along with type before the colon to start the function body: func my_func(arg1 : int, arg2 : String = "") -> void:Ĭasting types is accomplished by using the as keyword. # my_sprite is of type Sprite because of castingįunctions follow the same syntax for the arguments. # direction_vector is of type Vector2 because of the assigned constant
This can make the code more concise: var direction_vector : = Vector2(1, 0) Type can be inferred from the assigned value if you add the colon ( :) but omit the type. Constants don't need type hints, since their type is inferred from the assignment, but you can add one as well: const GRAVITY : Vector2 = Vector2(0, 9.8) This was chosen because GDScript is already similar to Python and this style is easy to integrate in the language parser, also considering that it is still optional.įor variables and constants, you can add a type hint with a colon ( :) after the name when declaring it. We adopted a post-fixed type syntax that is very similar to Python (yes, Python has type hints too). spaces) and everyone has their own preference that's based on their background in other languages.
This is quite a controversial topic (like tabs vs. With type hints, Godot can know beforehand that you are passing the wrong type and show an error while you are editing the related code, even if you never run it. In a larger code base, it's quite easy to miss things like that. With the dynamic nature of GDScript, you can inadvertently overwrite a variable with a different type and break your code logic in a way that might be hard to realize.įor instance, if your function expects a number, but you missed an input validation somewhere and is passing a string to it instead, you only will see an error at runtime (and only if you reach that point). It is quite common to use a variable only with values of the same type. Note: This is a new feature in the master branch, and will be available in Godot 3.1. It is completely optional and old scripts will work as they always did. We're now introducing an additional syntax to add type hints to your GDScript code. This helps avoiding potential bugs and also allows a better code completion.
While GDScript is made with the ease-of-use in mind, many people wanted to add type information into their scripts.