Finding all objects with a specific script attached in the scene

Godot Version

v4.2.1.stable.mono.official [b09f793f5]


As per title, I’m trying to find all objects with a specific script attached. The way I’m doing it now is that I set up a group in wich I put all the objects I want, and then use GetNodesInGroup. Problem is that this only gives me access the node, not the script, so this is the solution I came up with:

        var nodes = GetTree().GetNodesInGroup("Player Character");
        foreach(Node n in nodes)
			if (n is Character) party.Add(GetNode<Character>(n.GetPath())); 

This works, but feels a bit hacky, So I was wondering if there was a better way?

You can examine the attached script by getting it with get_script() then use the function in the Script class Script — Godot Engine (stable) documentation in English to get more info about them.