Trying to return an array of type "Array" where expected return type is "Array[Vector2]"

Godot Version

4.3.dev2

Question

I want to use a dictionary like a set in other programming languages and then return the keys as an array.

Is there a way to tell Godot “this is an array of type Array[Vector2i] and not an array of type Array”?

Both of these attempts don’t work…

@export var neighbors_right: Array[WaveFunction2DNeighborEntry] = []
@export var neighbors_right_atlas_coords: Array[Vector2i]:
	get:
		var out := {}
		
		for entry: WaveFunction2DNeighborEntry in neighbors_right:
			out[entry] = null
	
		return out.keys()
@export var neighbors_up: Array[WaveFunction2DNeighborEntry] = []
@export var neighbors_up_atlas_coords: Array[Vector2i]:
	get:
		var temp := {}
		var out: Array[Vector2i] = []
		
		for entry: WaveFunction2DNeighborEntry in neighbors_up:
			temp[entry.atlas_coords] = null
		
		out = temp.keys()
		
		return out

The return type of the Dictionary.keys() function is Array, so you can’t assign it to a variable of type Array[Something]. The workaround here is the Array.assign() function:

Instead of doing

out = temp.keys()

you can do

out.assign(temp.keys())
1 Like

Alright, thanks! Works just as expected.

@export var neighbors_left: Array[WaveFunction2DNeighborEntry] = []
@export var neighbors_left_atlas_coords: Array:
	get:
		var temp := {}
		var out: Array[Vector2i] = []
		
		for entry: WaveFunction2DNeighborEntry in neighbors_left:
			temp[entry.atlas_coords] = null
		
		out.assign(temp.keys())
		
		return out

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.