Can't convert Array to Array[Vector2i]

Godot Version

Godot 4.3

Question

How to make the Array type more strict?

Relevant code:

func mirror_piece(piece: Array[Vector2i]) -> Array[Vector2i]:
	var mirrored_piece: Array[Vector2i] = piece.duplicate(true)
	var max_y : int = mirrored_piece.map(func(p: Vector2i) -> int: return int(p.y)).max()
	var mirrored := ( mirrored_piece.map(func(p: Vector2i) -> Vector2i: return Vector2i(p.x, max_y - p.y)) as Array[Vector2i] )
	return mirrored 

Error:

Trying to return an array of type "Array" where expected return type is "Array[Vector2i]".This text will be hidden

Try casting the return value:

return mirrored as Array[Vector2i]

See my reply in this thread Ype Mismatch Error When Assigning Array[Enemy] in GDScript - #3 by gentlemanhal

1 Like

Updated code:

func mirror_piece(piece: Array[Vector2i]) -> Array[Vector2i]:
	var mirrored_piece: Array[Vector2i] = piece.duplicate(true)
	var max_y : int = mirrored_piece.map(func(p: Vector2i) -> int: return int(p.y)).max()
	var mirrored :=  ( mirrored_piece.map(func(p: Vector2i) -> Vector2i: return Vector2i(p.x, max_y - p.y)) ) as Array[Vector2i]
	return ( mirrored as Array[Vector2i] )

Same error:

Trying to assign an array of type "Array" to a variable of type "Array[Vector2i]".

The docs sais that it returns a Variant

  • Can be used to convert safely between datatypes.

Is there any other way to convert the type?

Thanks, the .assign workaround fixed it!

Code:

var mirrored : Array[Vector2i] = []
	mirrored.assign( mirrored_piece.map(func(p: Vector2i) -> Vector2i: return Vector2i(p.x, max_y - p.y))) 
1 Like