Connect to "body_entered" signal not working

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

Below is my code. When player (RigidBody2D) is entering the Apple (Area2D), only _on_Apple_body_entered be called, eating_apple never be called.

func _on_Apple_body_entered(body):
    # this function will be called
    print("body entered")

var apple
# ...
func create_apple():
    apple = preload("res://Apple.tscn").instance()
    apple.connect("body_entered", self, "eating_apple")

func eating_apple():
    # this function never be called
:bust_in_silhouette: Reply From: Thomas Karcher

Check your debugger! It is called, but as soon as this happens, an error message tells you what’s wrong with your function definition:

Error calling method from signal 'body_entered': 'Node2D(': Method expected 0 arguments, but called with 1.

Changing the function to func eating_apple(body): fixes this issue.

Hi Thomas, I forgot to mention that I have set the Gravity Scale of the Player to 0. If I set it to 1, then the signal will be triggered, so the error occurs.

I don’t understand why setting the Gravity Scale to 0 will cause the signal not be triggered

ddhxbgn | 2022-03-22 23:45

This could be caused by your RigidBody2D object going to sleep without movement. Set “can_sleep” to false in the inspector to avoid that.

Thomas Karcher | 2022-03-23 08:02