Arrays and types

Godot Version

4.2.1

Question

How can I initialise a typed variable to an empty array?

The code:
var new_walls : Array[Polygon2D] = Array() as Array[Polygon2D]
complains that I can’t assign and Array to a variable of type Array[Polygon2D]

What is the correct magical syntax to do this?

This should work

var new_walls : Array[Polygon2D] = []

You have already declared the type with :Array[Polygon2D]

1 Like

Oh, that’s so obvious in retrospect. I can’t believe I didn’t try it.
Thanks.

1 Like

:slightly_smiling_face: It’s easy to miss the simple things sometimes.

1 Like

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