"is_class" function for Objects of a custom class

Godot Version

4.2.2

Question

I am trying to use the is_class function to determine what nodes in my scene extend from a given class. However, the is_class function, a built-in function of all Objects, specifically ignores class_name declarations in the object’s script.

https://docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-is-class

Is there any alternative to this function that does account for custom classes?

EDIT:

Here is an example:

extends Node
class_name MyCustomClass

func _ready() -> void:
        print(is_node_of_class(self, "MyCustomClass")) # Should print true

        # Examples of what I don't want
        print(self.is_class("MyCustomClass")) # Prints false regardless of class_name declaration
        print(self.is_class("Node")) # Prints true regardless of class_name declaration

func is_node_of_class(node: Node, class_string: String) -> bool:
        ... # Return true if the node inherits from that class, otherwise false

You can use if object is ClassName:

I’m sorry, I should have been more specific. I do not want to use that syntax so that I can pass a string into a function. Example:

func is_node_of_class(node: Node, class_string: String) -> bool:
        ... # Return true if the node inherits from that class, otherwise false

How did you declare class_name. It should be like this: class_name MyClass extends SomeClass:

Yes, I declare class_name using that syntax. The problem is that the function is_class specifically disregards class_name declarations. I updated my example code.

just another thought. although it would be a hack, but if you are desperate you can use Meta properties.

E.g.

extends Button

class_name MyCustomClass
const CLASS_NAME="MyCustomClass"

func _ready() -> void:
	set_meta(CLASS_NAME, get_class())
	
	var x=Area2D.new()
	print(is_node_of_class(x, CLASS_NAME)) #Area2D is not 
	
	var y=CheckBox.new()
	print(is_node_of_class(y, CLASS_NAME)) # Checkbox is of class
	
	pass
	
func is_node_of_class(node: Node, class_string: String) -> bool:
	return node.is_class(get_meta(class_string))