How do I create a coin, of sorts? Collision

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

How would I go about creating a Coin? Say killing an enemy causes coins to rain down, and the player upon running over them would get some value. I started by designing a RigidBody2D that is the coin, have it collide with the player layer and the obstacles layer. However, the coin collides with the player when it should trigger on the player. I want to free the coin when it collides specifically with the player and inform the player that something happened.

I was thinking that signals could do this, but the player would have to connect to many, possibly hundreds of coin’s signals when they are created. I am not sure how to go about this. This is what I have:

extends RigidBody2D

export (String) var type = "coin"
export (int) var value = 4

signal item_pickup

func _ready():
	pass

func _on_Coin_body_entered(body):
	emit_signal("item_pickup", type, value)
	queue_free()

Now I know this is wrong, as it cannot call _on_Coin_body_entered on itself, but, as I stated the emit_signal for the player will be a problem as well. I could not find any solutions from the searches I tried.

:bust_in_silhouette: Reply From: DrewS

The solution was rather simple. Add an Area2D to the object with its own collision like so:

Coin #RigidBody2D
+--- Sprite
+--- CollisionShape2D
+--- Area2D
       +--- CollisionShape2D

And the code for the Coin node:

func _on_Aread2D_body_entered(body):
    if body.get_name() == "Player":
        if body.add_coins(amount):
            queue_free()

Now when the player runs over the coin, the coin calls a specific function in the player script. In this case, if the player has the spaced to add more coin (not hit a max) the player can increment his coin count and the coin is removed if the player function responds correctly.