How to check if object is instance of script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jan Heemstra

I’m making a shooter game, and when raycasting I want to check if I’m hitting a player. As far as I can tell, the following line in my “” script should check if “hit” is an instance of “”:

if hit is get_script():

However, it results in the following error:

Invalid "is" test: the right operand isn't a type (neither a native type nor a script).

I don’t quite understand this; it’s saying that get_script() is not a script? How do I figure out if I’m hitting a player? Is there any information that details how the “is” operator works? I have a hard time looking for it, since the word “is” is rather common.

:bust_in_silhouette: Reply From: Mike Trevor

get_script() is only going to return a Reference, which is why the is operator might not work. You could use if hit.get_script() == get_script() instead, to check if they’re using the same referenced script.

I don’t recommend this approach however, I’d rather use groups instead. You could assign the node to a group, or do it in in func _ready() like this: add_to_group("Player")
hit.is_in_group("Player"): To check if it’s part of the Player group