In GDScript, how can I store a class type in memory and check for inheritance later?

Godot Version

Godot 4.6

Question

I need a way to store a class type in memory so I can check if a node inherits from the stored class later on, if you know what I mean. In C#, you can achieve this using the Type class:

// Store a class type
Type parentClass = typeof(ParentClass); // You can also use the GetType() method
Type childClass = typeof(ChildClass);

// Check if type inherits another
if (childClass.IsSubclassOf(parentClass))
{
    // ...
}

I wonder if there’s a similar approach to this in GDScript. I’m writing some editor code that injects dependencies on nodes based on the type each property is expecting, so I’d need a Dictionary where each key is a property name, and each value is its expected type. Can someone help me?

Travel up the inheritance hierarchy of the child script using Script::get_base_script() and compare if any of encountered scripts is a parent.

You can also use is_instance_of()

1 Like

Yes! is_instance_of() is exactly what I was looking for! Can’t believe I couldn’t find it anywhere else :confused: but thank you so much!

1 Like