variable casting error ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By siska

I have a script that starts with :

class_name HS extends Area

and another script that starts with :

class_name HS_ia extends HS

The HS_ia script is empty.

Why am I not allowed to write this, inside the HS script :

if self is HS_ia: # editor ERROR 

=> editor ERROR : a value of type ‘HS’ will never be an instance of ‘HS_ia’

:bust_in_silhouette: Reply From: Inces

is keyword exists to check if subclass inherits from a class, never the other way.
I usually get around this by checking has_method() for some function only subclass may have

Thanks !
Yes, ‘has_method()’ is a good one to know.

I was just wondering though why what I did doesn’t work ? I mean, isn’t that like downcasting in other languages ?

siska | 2022-05-11 19:41

I only know Godots language, but that is just how it works here. Subclass always knows what its superclass is, but superclass doesn’t need to know what classes inherit from it.
If this check was in HS-ia script, it would work. Only checking for subclass is interpreted as cyclic dependance by compiller.

Inces | 2022-05-11 19:58