Array of class set to extended class error

Godot Version

4.2.2.stable

Question

It’s simple. Like this:

var companions : Array[Companion] = []

Then:

	companions = [Hiro, Nhala, Heric]

The three companions above are classes that extend Companion.

The error:

Line 8: Cannot have an element of type "Hiro" in an array of type "Array[Companion]".

This should work, right? I don’t think my brain melted. Yet. So, this should work. I think. Please, point me in the right direction. I might have futzed it somewhere.

I’m sorry if it’s too concise. If more info is needed, just ask.

Thanks.

Is Hiro, Nhala, and Heric a value or the type defined by class_name? Can you point to each of their definitions?

1 Like

I guess there was too much procrastination today.

The companion classes were not initialized. Obviously adding a .new() to them fixed the whole thing.

headdesk

The error message certainly does not lead you to the right solution! Looks like the analyzer has a tough time reporting a Type vs Value of type.

1 Like

Exactly. My brain went immediately where the error was pointing me to. The type was wrong. Oh well. It’s fixed now, but yes, maybe a different error message would help.

:man_shrugging: