How to use a body_enter function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ezangillz
:warning: Old Version Published before Godot 3 was released.

Hey, guys, I really don’t get how to use a body_enter function, please explain.
I got two nodes Player and Enemy and they both are KinematicBodies2D.
Enemy node has got an Area2D node in it with script like this:

extends Area2D

func _ready():
	connect("body_enter", self, "_on_enemy_body_enter")

func _on_enemy_body_enter(body):
	if (body.get_name == "Player")

I also have tried to add an Area2D to Player node and to change areas to RigidBodies2D but nothing seems to be working at all.
What am I doing wrong?

Is there any error in the console or debugger?

This line is missing stuff: if (body.get_name == "Player"). It should be if (body.get_name() == "Player"): (notice the colon at the end).

Also, have you defined shapes for the bodies and areas?

vnen | 2016-07-07 14:45

omg, I’m so dumb, thank you a lot! It works perfectly now, but one more thing, please, is it okay for performance and stuff to have such thing? I mean two CollisionShapes and KinematicBody, and if it’s not, then is there any way to improve that?enter image description here

ezangillz | 2016-07-07 19:52

The more physics stuff the more the CPU needs to work. But why do you have your enemy as an Area and a Body as child? Isn’t better to make the enemy as body (so you don’t need to update the parent transform via code)? If you really need the area for some other test, add it as a child of the body.

vnen | 2016-07-07 20:30

you’re right, it’s much better, thank you a lot for your help!

ezangillz | 2016-07-07 20:45