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,