I’m trying to expand on a 2D game tutorial that I recently finished and I’ve run in to a big of a snag. I’ve created a Coin scene with the intention of grouping all instantiated coins in the active level scene and connecting a custom signal that emits when the Player touches the coin. The coin itself is
Node2d
Area2D
CollisionShape2D
AnimatedSprite2D
I have added the coin scene to the group “coins” and grouped them in the level script. Here is the coin script:
signal coin_collected
func _on_area_2d_body_entered(body):
if body is Player:
coin_collected.emit()
And here is the relevant part of the level script:
func _ready():
var coins = get_tree().get_nodes_in_group("coins")
print(coins)
for coin in coins:
coin.coin_collected.connect(_on_coin_collected)
func _on_coin_collected():
print("Coin collected")
When I run the game I get an accurate print out of all the coins in the coins group, but when the Player touches the coin the _on_coin_collected function does not go off. I’m at a loss because I have a trap scene that is made of the same nodes and grouped in the same way that works as intended when the Player touches it.
Any insight would be beyond helpful.