Cast untyped array to typed array

Godot Version

4.2.1

Question

How do I cast an untyped array (or an array of supertype) to a typed array?

For example, this does not work:

node.find_children("", "Interactable") as Array[Interactable]

Error:

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

I do wish gdscript had some form of generics, or if types could be variables, so we could just do something like this:

node.find_children_of_type(Interactable)

Use Array.assign() like:

var array:Array[Node2D]
array.assign(find_children("*", "Node2D", true, false))
2 Likes

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