Coins Left
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 17 of 27.
Each coin here counts the coins left when you take it. The first one prints Coins left: 3, but only two are left.
queue_free() waits until the end of the frame. Until then the coin is still in its group, so it counts itself.
Take the coin out of the group first with remove_from_group(). It works at once:
func _on_body_entered(body):
remove_from_group("coins")
print("Coins left: ", get_tree().get_nodes_in_group("coins").size())
queue_free()Lines run from top to bottom, so remove_from_group must come before the count.
Challenge
MediumFix the count. Take the coin out of the group coins before it counts. Taking the three coins should print Coins left: 2, Coins left: 1, then Coins left: 0.
Try it yourself
extends Area2D
func _ready():
add_to_group("coins")
body_entered.connect(_on_body_entered)
func _on_body_entered(body):
print("Coins left: ", get_tree().get_nodes_in_group("coins").size())
queue_free()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
Practice on your own: Godot playground