Type hints
- Game Development course
- Beginner
- Lesson 71/83
- 4 min
Learn type hints in GDScript with the : marker.
Type Hints
GDScript is dynamically typed; however, placing a colon : after a variable name introduces static type hints. This enables the editor to catch type mismatch errors early while authoring code.
var player_name: String = "Hero"
var health: int = 100
var speed: float = 8.5
var is_active: bool = trueType hints also significantly accelerate Godot's autocomplete and contextual documentation engine.
Type hints in functions
Beyond parameter typing, the exact return type of a function can be declared using the -> arrow. If the function returns no value, its return type is marked as void.
func add_health(current: int, bonus: int) -> int:
return current + bonus
func reset_game() -> void:
print("Reset")This disciplined pattern maximizes maintainability and stability across large projects.
Practice Declare typed variables and print
Assign "Mage" to a String variable title and 5 to an int variable level. Print Title: Mage | Level: 5 to the console.
Starter code
extends Node
func _ready():
# Tip belirtecleriyle title ve level tanimlayip yazdir
passSample solution
extends Node
func _ready():
var title: String = "Mage"
var level: int = 5
print("Title: " + title + " | Level: " + str(level))Expected output
Title: Mage | Level: 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which symbol is used right after the variable name in GDScript to explicitly add a type hint?
- ->
- =>
- : (Colon) Correct answer
- #
Explanation: A colon ':' placed immediately after a variable name declares the type acceptable for that variable.
2 Add integer type hint
var max_health: ____ = 100
var max_health: int = 100
Explanation: In GDScript, the keyword 'int' is used for integer type hints.
3 Type hint elements
| Term | Meaning |
|---|---|
Type Hint |
Hint declaring variable data type |
String |
Type hint representing text data |
int |
Type hint representing integers |
-> |
Arrow indicating return type |