0 votes

The player character I created is able to cast spells, so I want to make enemies die whenever they are hit, but for some reason I can't get that to work.

I used the onArea2Dbodyentered signal and created this:

func _on_Area2D_body_entered(body):
        if body.name == "Greenslime":

My question is what body.name should be equal to because it doesn't seem to detect "greenslime" which is the name of the kinematicbody used in the greenslime scene!

in Engine by (23 points)

Is "greenslime" capital or lowercase? When typing in the enemies name, the name has to be exactly what it's called in the node.

For example, if I have a player and the name is "Player" you have to say

if body.name == "Player":

If you use a lowercase "p", it won't work.

Thank you for the quick reply!
The scene is named "greenslime" but the kinematicbody is called "Greenslime"

I'd add a...


as the first line in ...body_entered() method to see what is being hit. That should shed some light on the problem...

GREAT idea! I just did and it seems like it was "greenslime"!

Just another side question, I preloaded the greenslime scene into a constant, and then did the .instance() of it in a variable, but by doing greenslime.dead() in the _on_Area2D_body_entered(body) function nothing happened, however by doing body.dead() inside _on_Area2D_body_entered(body) it worked, what's the difference?

(also, by doing body.dead i could remove the preoload and everything still works)

So, in the _on_Area2D_body_entered() method, body contains a reference to the specific body instance that was contacted. So, in that method, you want to use body to reference that instance.

Without seeing some code, I'm just guessing, but I assume the the greenslime instance you created "manually" via .instance() (in your above example) is a different, separate instance of the same scene. So, calling dead() on that instance should work (for that instance), it will not impact any other instance.

So, bottom line, I assume you have (at least) 2 independent greenslime instances in the scene in your test. Interacting with one will not impact the other.

Please log in or register to answer this question.

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.