Not able to cast an typed Array into another variable

Godot Version

4.6

Question

Hi everyone!

I’m trying to cast an Arrayas Array[TacticsAction]but Godot still doesn’t recognize the content of best_plan[NodeKeys.ACTIONS]as Array[TacticsAction]:

var best_plan_actions: Array[TacticsAction] = best_plan[NodeKeys.ACTIONS] as Array[TacticsAction]

The engine throws an error:

Trying to assign an array of type “Array” to a variable of type “Array[TacticsAction]”.

The content of best_plan[NodeKeys.ACTIONS] it is indeed an Array, and its members are indeed TacticsAction. The best_plan is a Dictionary, but I didn’t type its values because they are of different types: one is an int, the other an Array, etc. I thought that casting it as an Array[TacticsAction] would be enough, but I guess I’m wrong.

Do I have a proper way to cast it as I want?

Try Array.assign, this will do what you need:

var best_plan_actions: Array[TacticsAction] = []
best_plan_actions.assign(best_plan[NodeKeys.ACTIONS])

It throws an error if an element can’t be converted.