How to tell if a class in a gd script belongs to a class without new()

Godot Version



For example, I have some classes:

class_name CustomClass extends BaseClass

class_name AnotherClass extends CustomClass

And I get their GDScript from load( and load(
I want to judge if they belong to BaseClass or not without new()

I think this is what you’re looking for @GDScript — Godot Engine (stable) documentation in English

if I use is_instance_of(load(, BaseClass ),I’ll get False always,Because typeof(load( is “GDScript”

Why would you do that though?

A instantiation of a class should always be a variation of:

var someClass :=

I don’t see the reason why you would load a class using ‘load(“”)’

There might be reasons to do that, but so far, I haven’t read anything suggesting that.

I want to implement the following functions: 1. Get all the scripts under a certain path, 2. Determine whether the script belongs to a certain class, if yes, instantiate it, if not, not instantiate

I see… I’ve never tried anything like that, and I don’t know if it’s doable, but I would try to check if there’s a way to open those class files as if they were text files, then parse the first few lines. If the name fits with what you’re looking for, then instantiate them.

If I had to do something like that though, I would rather keep a global file with a dictionary of all the classes from which I could choose and instantiate as needed. But then again, depending on your project and why you do that, it might not be optimal (like if there are hundreds or thousands of classes).


You are right,Hah,I’ve tried this way before, but it’s not generic enough.So I wanted to find the best solution