0 votes

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?

in Engine by (74 points)

1 Answer

0 votes

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.

by (864 points)

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.

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 Frequently asked questions and 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 [email protected] with your username.