0 votes

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 
    print("+")

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

Godot version 3.4
in Engine by (101 points)

1 Answer

0 votes

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

by (8,099 points)

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 ?

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.