Can't assign a nested array

Godot 4.5

Invalid assignment of property or key ‘data’ with value of type ‘Array’ on a base object of type ‘Sprite2D (Ability)’.

class_name Ability
extends Sprite2D

@export var data: Array[Array]

Thats the ability in question ^

new_ability.data = [[2, 2], [2, 3], [2, 4]]

And thats the script that instantiates it and tries to set the nested array’s data. Returns the error message above. Can’t set data, a nested array, as a nested array? Sorry if this is super obvious but i’m mystified

I believe Godot doesn’t like the Array[Array] syntax, but if you create an untyped array, you’ll be able to put arrays in it.

1 Like

What @phoenixdk said. Or type the internal Array.

@export var data: Array
@export var data: Array[Array[int]]

data: Array returns the same error, for some reason
I tried typing it, but godot says it doesnt support typed nested arrays. Im on 4.5.1 stable
”Nested type collections are not supported,” specifically

this is not supported


GDScript does have trouble with promoting values to typed arrays, Array[Array] != Array and your [[2, 2], [2, 3], [2, 4]] is only an Array.

I believe you can make a temporary typed variable or use as to upcast

var new_data: Array[Array] = [[2, 2], [2, 3], [2, 4]]
new_ability.data = new_data
# or
new_ability.data = [[2, 2], [2, 3], [2, 4]] as Array[Array]

What’s the exact error message you recieve?

1 Like

My bad. I thought I had done that.

What about using a Variant?

@export var data: Variant

There might be something more insidious happening here, because I stopped getting an error message at all, it just tells me the breakpoint. And I tried

 		var new_data: Array[Array] = [[2, 2], [2, 3], [2, 4]]
		new_ability.data = new_data

and the breakpoint is on the line var new_data and not the one after. Like as if it just breaks from having a nested array exist in general

Is there a different way you could model the data? Like in a Resource?

2 Likes

I can, and I think i’ll have to. Didn’t wanna do that cause I’m a couple resources deep already but I suppose it’s the same either way. Thank you all for the help!

1 Like

Like the error says “breakpoint”? If so click the red dot beside this line in the script editor, a breakpoint intentionally stops the game so you can debug a non-failing function.

wth. now it does work (var new_data → ability.data)
i tried recreating it to see the breakpoint
Idk if I trust that. Sorry I cant get the info for you, godot doesnt want to give it anymore

breakpoints can be added by clicking the “line gutter” in godot’s script editor, it’s not uncommon to accidentally make a breakpoint. If “breakpoint” appeared as your error it was probably a misclick, not a scripting issue.

Typed arrays are still a little funky too though; mostly when assigning [...] evaluates to Array rather than a more specific type, for some reason initializing a new variable will try harder to correct the type, and as is the keyword to specify types.

1 Like