0 votes

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

Apple.gd

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

Main.gd

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

func eating_apple():
    # this function never be called
    print("eating_apple")
Godot version 3.4.3
in Engine by (15 points)

1 Answer

0 votes
Best answer

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(Node2D.gd)::eating_apple': Method expected 0 arguments, but called with 1.

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

by (1,756 points)
selected by

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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.