Signal Parameter Unused but Needed

Godot Version

4.2.2

Question

I have been following the 2D game tutorial and had a warning saying the “body” parameter was not used in the following part of the “player.gd” script.

func _on_body_entered(body):
hide()
hit.emit()
$CollisionShape2D.set_deferred(“disabled”, true)

Here is the part of the tutorial the script is from Coding the player — Godot Engine (stable) documentation in English

And as it said i couldn’t see “body” used in this function/signal. However if i remove the “body” parameter collisions are no longer detected in the game.

Where is the “body” parameter being used?
Also where does “body” get its value(s) from?

body is the physics object that entered the area 2d. It is part of the parameters passed to the signal by default when it’s emitted.

You don’t necessarily have to do anything with the body. What you see in the console is just a warning, nothing more.

If you add a line like print(body.name), the warning will be removed since the body parameter is now used in your code.

2 Likes

The same warning message should be telling you that if you rename the body to _body (underscore body), the warning will go away.
Like this:

func _on_body_entered(_body):
    hide()
    hit.emit()
    $CollisionShape2D.set_deferred(“disabled”, true)
2 Likes

Thanks.
So if I understand the physics object is passed with the signal and becomes the “body” parameter.
I’m not trying to get rid of the warning I was just trying to figure out why “body” is needed in the function parameter for the game to work when there is no place in the function it is used.

I would have thought since “body” isn’t used in the function i could have defined the function without “body” like so.

func _on_body_entered():

However since this makes the game not detect collisions the “body” parameter must be needed by something that is not seen in the script right?

Because you can’t add or remove parameters of a signal that’s already implemented. You have to connect a method with the same signature.

“body” is the Node that enters the Area2D. In general, collisions are there to detect other objects and do something with that object, or get information from that object to do something to the Player. If your game doesn’t do any of that, that’s fine, just rename the parameter to _body to avoid the warning.

The warning is useful, because in general you’re supposed to use the body parameter, so it’s likely you forgot to use it and the game warns you about it.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.