Custom classes with @tool annotation are interpreted as the class they extend from.

Godot Version

4.6.2

Question

I’m building a hierarchical state machine, with a custom class “State”.

The state class has the boolis_superstate, which exposes different variables depending on if its true or false. To do this, the class (and each script extending State) needs the @tool annotation.

The problem: the StateMachine class has a function which iterates over all its children, connects them to the player, and then does the same for each of their children. Since I might want to add components or other nodes under the state machine, the function also ignores the child if child == State fails:

extends Node
class_name StateMachine

func _assign_actor(_actor: Player, parent: Node) -> void:
	for child in parent.get_children():
		if child == State:
			child.actor = _actor
			_assign_actor(_actor, child)

However, a custom class starting with the @tool annotation is not recognised as the custom class, but as the class it inherits from (in this case Node). I also tried to get the script of the child and match its name to the class string, but child.get_script().get_global_name() returns blank.

I could do without the @tool annotation, I just think it’s neat to use, and was mainly wondering if anyone knows a solution to this?

Shouldn’t it be child is State instead of child == State?

Add a method to the base State class called “is_state”. Then, check if the child node has the method “is_state”. There’s probably an easier way, but I’m not at my computer. Let me know if it works!

func is_state() -> bool:
    return true
        if child.has_method(“is_state”):

You’re very correct, my bad!