How to get the name of the node that emits the signal on CollisionShape2D using the connect method?

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

Hello, I’m new to game dev and Godot is my first game engine.
I was trying to make some shapes with coins but I realised that when their areas collide they disappear just like when the character’s area collide with them.
I’m trying to do some logic to disappear only when the character’s area touch them but I don’t know how to get the name of the node to do so. Or perhaps there’s a better approach that would solve the problem better.

Here’s the code I’m using for the Coin scene.

extends Node2D

func _ready():
	$Area2D.connect("area_entered", self, "on_area_entered")

func on_area_entered(_area2d):
	$AnimationPlayer.play("pickup")
	call_deferred("disable_pickup")
	print("Coin gathered")

func disable_pickup():
	$Area2D/CollisionShape2D.disabled = true

Thank you so much beforehand.

:bust_in_silhouette: Reply From: Gluon

Well there are several ways to do this but the method I use is to put things in groups and check the groups so for example if you go to your coins and where you see the insepector click node, then inside that you will see groups. You can assign a group to it so for example try adding “Coin”, similarily you can add “Player” to your player group. You can then do something like this

func _on_Area2D_body_entered(body):
	if body.is_in_group("Player"):
		do something

Thank you, I’m going to give it a try!

Jojota | 2023-02-23 14:33

Your solution worked for me. Thank you so much. :smiley:
I also found another solution. I made a 2D Area for the Player and the Coins with a 2D Shape Area as a child. On the 2D Area I made them share one slot and let the Layer checked for the Coins and the Mask checked for the Player.

Jojota | 2023-02-23 15:01

Glad it worked for you, and yes there are quite a few ways this can potentially be done but as long as you have a solution you are happy with thats good!

Gluon | 2023-02-23 15:55