godot icon indicating copy to clipboard operation
godot copied to clipboard

GDScript 2.0: Cannot use default values with typed arrays

Open Kubulambula opened this issue 3 years ago • 2 comments

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

Kubulambula avatar Jul 26 '22 17:07 Kubulambula

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

jonas-bischofberger avatar Oct 19 '22 15:10 jonas-bischofberger

A small addition: Screenshot from 2022-12-06 14-27-14 I'm using PackedStringArray but Error says Array[String]

MaasTL avatar Dec 06 '22 13:12 MaasTL