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

Godot Version

Godot 4.5.1 Stable

Question

For some reason, even though both of the options are clearly String arrays in this line of code:

var dash_input : Array[String] = ["Right", "Right"] if facing_right else ["Left", "Left"]

…It’s still giving me this error message:

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

1 Like

cast those arrays to Array[String]:

["Right", "Right"] as Array[String]
2 Likes

FYI, your first Array is of type Array[Variant] as that is the default for any Array that is not specifically declared as such.

There’s also probably a simpler way to do what you’re doing there.

1 Like

I tried just using “as Array[String]” like normalized suggested, but it doesn’t seem to be working. What did you have in mind for a simpler solution?

Does this work:

var dash_input : Array[String] = (["Right", "Right"] as Array[String]) if facing_right else (["Left", "Left"] as Array[String])

Or just let dash_input be untyped as well. Why type it when rest of your arrays are not typed.

I see… So I guess they have to be in parentheses? In any case, thank you very much!