Const Array[PackedStringArray] weirdness / bug?

Godot Version

4.3

Question

Iterating through a const Array of PackeStringArray causes weird issues:

const ARRAY: Array[PackedStringArray] = [
	["a1", "b1", "c1"],
	["a2", "b2", "c2"],
]

func _ready() -> void:
	for arr: PackedStringArray in ARRAY:
		print(arr)
		print(arr.size())
		print(arr[0])

It prints:

[“a1”, “b1”, “c1”]
0
Attempted to push_back a variable of type ‘Array’ into a TypedArray of type ‘PackedStringArray’.
core/variant/array.cpp:285 - Condition “!_p->typed.validate(value, “push_back”)” is true.
Attempted to push_back a variable of type ‘Array’ into a TypedArray of type ‘PackedStringArray’.
core/variant/array.cpp:285 - Condition “!_p->typed.validate(value, “push_back”)” is true.

And then it gives an “Out of bounds get index ‘0’” error.

So it actually prints the array (the first line in print), but then prints 0 for it’s size. And then it crashes when you attempt to get the first element.

It only happens with const.
Am I being dumb or is this a bug?

Can you try the following:

print(ARRAY)
print(ARRAY[0], ARRAY[1])
print(ARRAY[0][0])
print(ARRAY[1][0])

and see what happens

It prints everything as it should:

[[“a1”, “b1”, “c1”], [“a2”, “b2”, “c2”]]
[“a1”, “b1”, “c1”][“a2”, “b2”, “c2”]
a1
a2

Does your loop work with out statictyping the variable?

func _ready() -> void:
	for arr in ARRAY:
		print(arr)
		print(arr.size())
		print(arr[0])

Nope, the exact same thing happens.
It works if I remove the PackedStringArray type from ARRAY itself, and just make it’s type Array.

Then this is probably a godot bug. You should open an issue on github

1 Like

Actually the reason why this doesnt work is, because you cant statictype Elements of Array inside a Array. For example the following is not allowed:

var arr: Array[Array[int]]

This is probably the reason why Array[PackedStringArray] or any other packedarray doesnt work

1 Like

Hmm… that might has to do something with it, but it does work if you don’t make it a const. I use Array[PackedStringArray] no problem, but this was the first time I tried to make one const.

This bug is likely related
Which links to this
What I gather out of these is that it is not yet supported to make any of the packed arrays constant.

1 Like