CodeBlanks Logo Icon CodeBlanks

Type hints

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 = true

Type 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

Player.cs
extends Node

func _ready():
    # Tip belirtecleriyle title ve level tanimlayip yazdir
    pass

Sample solution

Player.cs
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

Question
var max_health: ____ = 100
Answer
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

All lessons in Beginner