0 votes

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 "player.gd" script should check if "hit" is an instance of "player.gd":

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.

in Engine by (70 points)

1 Answer

0 votes

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")
groups
hit.is_in_group("Player"): To check if it's part of the Player group

by (189 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories