How can I filter out the correct collision area?

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

Hi all.

I have a few Area2Ds. One is called BaseArea, and is the only one that doesn’t move. LazerShot and Enemy are the moving Area2Ds. What I was wondering is if there is a way to have Enemy emit a signal if it only collides with LazerShot.

extends Area2D

signal kill
export (int) var enemy_speed


func _ready():
	set_process(true)


func _process(delta):
	position -= Vector2(enemy_speed * delta, 0)



func _on_VisibilityNotifier2D_screen_exited():
	if position.x < 0:
		print("Deleting…")
		queue_free()


func _on_Enemy_area_shape_exited(area_id, area, area_shape, self_shape):
	queue_free()

The above code is from Enemy.gd.

:bust_in_silhouette: Reply From: p7f

Hi, you can use body_entered(body). In body you can use the parameter to check if the body who entered the area is lasershot. For example, you can define a function in lasershot that does nothing, but you can use to identify a lasershot:

#this goes in lasershot
func lasershot():
    pass

Then in your enemy, you check if body has that method:

#don't forget to connect the body_entered signal with this function
func on_body_entered(body):
     if body.has_method("lasershot"):
        #here you put whatever you wanna do when laser shots hits enemy.

Perhaps there is a better way, but i usually do this. You can also set collission masks/layers to set enemy only can be collided by lasershot. For example, you put lasershot in layer 2, and you only enable second bit on mask of enemy, or something like that.