godot
godot copied to clipboard
GDScript 2.0: Cannot use default values with typed arrays
Godot version
4.0.alpha12
System information
Windows 10
Issue description
When using typed arrays as type for function arguments you cannot use default values.
For untyped arrays this is not an issue
func _ready():
foo()
bar()
# OK
func foo(optional_arr: Array = []):
print(optional_arr) # []
# Also OK
func bar(optional_arr: Array = [1]):
print(optional_arr) # [1]
But when using typed arrays it always results in error.
Type of default value for parameter "optional_typed_arr" (Array) is not compatible with parameter type (Array[ *INSERT TYPE* ]).
func _ready():
foo()
bar()
func foo(optional_typed_arr: Array[Resource] = []):
print(optional_typed_arr)
func bar(optional_typed_arr: Array[int] = [1]):
print(optional_typed_arr)
Steps to reproduce
Just copy and paste the above example to any project
Minimal reproduction project
No response
The naive workaround to cast the default value with as leads to a silent crash as soon as the function is supposed to run (4.0.beta3, Windows 10):
extends Node
func foo(x: Array[int] = [] as Array[int]):
pass
func _ready():
foo() # crashes without any output from debugger
A small addition:
I'm using PackedStringArray but Error says Array[String]