Connect KinematicBody2D to an Area2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By min

Hey there,

I have to deploy a little program for university.
I have an enemy(bird) and collecting items(apples).
What I want is:
If the bird die the apples should disappear.

Do you have any idea how to do so ?

:bust_in_silhouette: Reply From: magicalogic

If I understand your question correctly, you have a bird (kinematic body 2d) collecting apples (area 2ds) and you want all the apples to disapear when the bird dies.

To keep things simple, add an autoload script to your project and call it PlayerInfo (or whatever you like). Declare a signal inside that script:

signal player_died 

Now, inside the bird script, when the bird is just about to die (before you call queue_free()) emit the signal:

PlayerInfo.emit("player_died")

Finally catch that signal inside the apple script.
First connect the signal inside the _ready() function:

PlayerInfo.connect("player_died", self, "_on_player_died")

Then declare the _on_player_died() function:

func _on_player_died() -> void:
    queue_free() // if you want to kill the apple or
    hide() // if if you just want to hide it

Hmm doesn’t work. If the bird dies I got the error:
Invalid call. Nonexistent function ‘emit’ in base ‘Node(PlayerInfor.gd)’.
You know why ?

–Update
It works ! I had to change the emit into emit_signal()

Thank you so much for your help!

min | 2023-01-31 08:37