Coin Area2D Example! (GDScript)

a Simple Method to get Coins or any Touchable Object a simple Counter using only Signals.

AREA2D

Make it into its own Scene and Save it You need to have it:

Area2D - (Coin)
|-> Icon - (CoinImage)
|-> CollisionShape2D - (CollectBox)

extends Area2D
signal Touched_Coin()

func Counter(body: Node2D):
	Touched_Coin.emit()
	queue_free()

Remember to Connect: Signal Touched_Cointo To Label
This is where you should Split it off if you plan to make a Bag System.
but for now ill Attach it to a Label to Change its Text

Canvas Layer
|-> Label - (Counter)

extends Label

var coin:int = 0 

func _on_coin_touched_coin() -> void:
	coin += 1
	text = str(coin)

we need to initialized the Coin so it has a Number
Then Giving +1 per each Emit it Receive.|
Change the int to String.
apply it to the Text of the label

NOTE:

Remember to Connect the Signals Using the GUI: Area2D To Label.

if you have Multiple Coins!. First Connect the Signals then Copy Paste it!.
Incase that din’t Happen. Well… Prepare to Manually Connect all the Coins.

Final Things to say

Yes there are Better method for Coin management.
but i wanted to give and example just using Signals.
as i haven’t found much examples in the matter,

I feel like your approach is a little backwards, and this might come back to bite you later on.

Having to manually connect an arbitrary number of coins should be the first red flag that something in your architecture is incorrect or wrong.

On top of that, in your current setup, assuming that Counter is connected up to the Area2D’s body_entered signal, ANY body will trigger the coin being picked up, even if it’s not the player.

In my opinion, a much better approach is to simply have a coin that monitors when a Body enters, check if the body is the player, then call a function in the player that will increase the number of coins they currently hold.

Your label shoud only be responsible for displaying a number, not actually to keep track of it. In my opinion this is where you CAN use a signal. Your player could have a signal that gets emitted when they have collected a coin, and your label can listen to that signal and update itself when needed.

This will completely avoid having to manually connect or even copy the connections of potentially hundreds of coins all around the level.