Best way to check if a node is an instance of a specific scene?

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

I’ve tried using names, but when there are more instances of the same scene they will start changing names, for example when there is more than one player character they won’t all have the exact name “Player”.

I’ve considered checking if their name contains the string “Player” cause at least so far it seems like it will only add something to the name, and will always still contain the original name. Would that work? Or is there a cleaner way to do something like this?

:bust_in_silhouette: Reply From: Jowan-Spooner

Godot3.1

If you are using godot3.1 then you can use the class_name keyword, explained here.
In this case you can just do

if body is Enemy:    # or whatever your class_name is

Older versions

In older versions you can use a group that the objects are automatically assigned to (for example in the ready function). Then use:

if body.is_in_group("group name"):    # or whatever your groups name is

That are the ways I would recommend, hope it helped.

So, I thought the class name thing would work for what I had in mind, but I ran into a problem instantly. I apparently can’t use the “Player” class name in the script that has “class_name Player”, because it “creates a ciclic reference”, which I really don’t see how it would, but I suppose I don’t know how it functions exactly.

Very impractical since I can’t check if other nodes are of the Player class in that script. Any way to avoid this issue or is groups my best bet? Also if you know why it would create a cyclic reference I would like to know that, because I don’t see it.

MOSN | 2019-05-15 09:42