Cnajf
1
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]
Cnajf
4
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?
Cnajf
5
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