godotengine / godotengine/godot-docs

Variables declared with static type hints initialize differently depending on type

Open
#6,148 0 comments 0 reactions 0 assignees View on GitHub
area:manual enhancement
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

**Godot version:**
3.5

**Issue description:**
When declaring variables, the variable is initialized. When not using static type hints, variables are initialized as `null`. If a static type hint is given, but an initial value is not, the engine chooses a value based on the type. This behavior is **not** documented.

**Examples:**
Assume the user has defined `CustomClass` elsewhere with `class_name CustomClass`.
```
var a
var b: int
var c: Vector2
var d: CustomClass
```

Is equivalent to:

```
var a = null
var b: int = 0
var c: Vector2 = Vector2.ZERO
var d: CustomClass = null
```

**Relevant documentation pages:**
This behavior should be noted on the [Static typing in GDScript](https://docs.godotengine.org/en/stable/tutorials/scripting/gdscript/static_typing.html?highlight=static) page. Additionally, each built-in type/class's page ([int](https://docs.godotengine.org/en/stable/classes/class_int.html) and [Vector2](https://docs.godotengine.org/en/stable/classes/class_vector2.html) in the above example) should state what they initialize as when declared with a static type hint.

**Note:**
I haven't tested this behavior with Godot 4.0-Alpha, but the [latest documentation on static typing](https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/static_typing.html) doesn't mention it either.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.