GameDevWannaBe your character must have an Area2D
of it's own to collide on a separate layer
, put the item (food) in this layer
.
what you connect is the player's Area2D
to the player script.
when the Area2D
collides with something, it will send a signal
to the player.
this signal will cause a function to be executed.
func _Area2D_collide(body):
pass
and will send the parameter body
, this is a reference to the thing it just collided with.
in here you can delete body
with queue_free()
, or do something else:
func _Area2D_collide(body):
body.queue_free()
because this function belongs to player, you can do changes to player here:
func _Area2D_collide(body):
make_player_bigger()
body.queue_free()
the point .
can be followed by a function or variable, you have to use a function that the node has (look in the docs) or one that you created in a script attached to the node.
body
: Area2D just collided with food, so this is that node food.
queue_free()
: this function is available for every node, it deletes the node.
body.queue_free()
: you are calling the function queue_free()
of the node body
if you don't specify any node, the function will be called for the current script, in this case player.
for some reason, the brakeys tutorial and many other tutorials teach you to do it the other way around and emit signals from the items. Maybe it's easier for beginners, maybe there's some other reason, but it's not an optimal way to do it.