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.

2 Likes

i don’t like having Super Classes (Meaning: 1 Script Does all).
if i can avoid it, i whould.

Well let me share some other methods
Method the Commenter is talking about is something like.

Coin:

extends Area2D

func Counter(body: Node2D):
	if body is CharacterBody2D:
		body.add_coin()
		queue_free()

CharacterBody2D:

extends CharacterBody2D

signal coin_collected(total_coins: int)

var coins: int = 0

func add_coin() -> void:
	coins += 1
	coin_collected.emit(coins)

Label:

extends Label

func _ready() -> void:
	text = ""

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

Coin Checks for the body for Function add_coin
(YES i know you can use Groups).
Character Body Automagicly Knows about Coins existence,
Character Body tells Label.

ill spend more time on it once i figure it out i will come back…
it is 1am after all haha

Ugh its 2:31am haha…
To Avoid Multiple Signals Connecting you need a CoinManager. (Excessive for this but still)

CoinManager
Coin Manager is a Class that Communicates for the Coins. and Usually Handles all the Logic.

extends Node
class_name CoinManager
signal coin_update(coin:int)

var coins: int = 0

func add_coin() -> void:
	coins += 1
	print("Coins:", coins)
	coin_update.emit(coins)

Coin:
then you got your Coin. Same as Always. the only Difference is im using _ready to Connect the it so it doesn’t show on its UI.

extends Area2D

@onready var coin_bag: CoinManager = %CoinBag

func _ready() -> void:
	body_entered.connect(_on_body_entered)

func _on_body_entered(body: Node2D) -> void:
	if body is CharacterBody2D:
		coin_bag.add_coin()
		queue_free()

UI: label
and the Label Which The Coin Manager has to Pass The Signal so the UI updates.

extends Label

func _ready() -> void:
	text = ""

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

im off to bed…
There is also autoload but that is Excessive for this haha…