Is there any way to specify class as the value of a variable in the editor?

Godot Version

Godot 4.x

Question

Is there a way to specify a class using the editor, instead of in script? I don’t mean using static typing itself, but instead specifying a class to be used in static typing.

For example, I have a state script for a generic state machine, extended from a generic state class script. The extended state script calls a function in the state machine’s parent /subject that is only available in certain classes. I want the state to check on enter_tree() or ready() whether the subject is the correct class, and print an error message and disable the state if not.

I can of course use “if subject is class_name” in the state script, or check “subject.has_method()” before calling, but just in case this setup is needed repeatedly, is there a way to @export a variable in the generic state script, to designate the subject class the state is intended for? Or do people really find it necessary at all to automatically check if nodes that require parents to be certain classes are placed correctly? Thanks.

I am not sure if there is a better option. I usually just check if is class. One thing to be cautious of is to check in ascending order of inheritance. For example if you have something that extends Node2D and check if is Node2D it will come back as true. If you start checking from the bottom node, it will not be an issue. Probably you knew that already but i mention it just in case.

1 Like

I don’t think that’s possible with the @export var.

I think your best bet is to check the type by String, see here my post with a script that gets a class name.

2 Likes

I really don’t know that already, so thank you for bringing it up! I’ll look up more info about that.

Thanks! Looks like there’s just nothing that simple, but what we have is sufficient for the purpose.