Inopinans
Inopinans

Reputation: 63

_on_body_entered never triggers

To make interaction between my player and objects possible, I wanted to use composition and made an InteractionArea that I can then attach as the child to every object that should be interacted with. The area has a collision2d and the following (simplified) script:

extends Area2D
class_name InteractionArea


func _on_body_entered(body):
    print_debug('entered interaction area test 1')
    if body.is_in_group('player'):
        print_debug('entered interaction area test 2')

while the player is a CharacterBody2D and has a CollisionShape2D as child. The collision layer and mask of the InteractionArea are the same as the visibility layer of the player. However, the function _on_body_entered never triggers (none of the two messages is printed) and I just don't get why. I'm sure this is a big noob question and the solution is very simpple, but I just don't find it. I'd be very happy if someone could help!

enter image description here

Upvotes: 2

Views: 2137

Answers (1)

Theraot
Theraot

Reputation: 40315

The collision_mask of the Area2D should overlap with the collision_layer of what it will detect.

In general you want to use the collision_layer to specify what the body is, and the collision_mask to specify what it interact with.


The collision layer and mask of the InteractionArea are the same as the visibility layer of the player

The visibility layer (in 2D) is for the Viewport (so you can have things show in one but not another, depending on the canvas_cull_mask). It is not what you want.


Addendum: Since apparently this is not the issue, the next thing to check would be if the body_entered signal is actually connected to the _on_body_entered.

If the signal is connected from the editor, when editing the script there should be a green icon on the left of _on_body_entered which indicates that it is connected.

It is also possible to make the connection from code using connect.

Refer to Using Signals for details.

Upvotes: 1

Related Questions