How to make all members of a group automatically interact with an Area2D?

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

Greetings! Good to see you all, folks.

So, I’m still starting with Game Design in general, and I’ve chosen to learn Godot (I’ve been using it for ~2 months or so now, and I’m having a blast), but I’ve hit a brick wall:

I’m trying to show a label whenever my character enters an area2D that is part of a specific group (Let’s call it “interactable”).



Here is Miller, chilling in front of this knife.

IF MILLER ENTERS THIS AREA2D (that makes part of the “Interactable” group, I’d like to make a label with an “!” appear (with .show()).

Nice, a Knife

I know I could use something like:

"func _on_KnifeInteract_body_entered(body):
  >  $"

Where notification is a label (or image) with the ! symbol.

But I’d end up replicating a lot of code (for every interactable object I would end up using this, and that’s not cool!)

I’m wondering on how should I approach this.
Thanks in advance!

:bust_in_silhouette: Reply From: avencherus

One alternative is to make these interactive objects Area2Ds (Their root is an Area2D and their script extends Area2D as well). Then put an Area2D on your character, and connect the area_entered signal for that character.

Then you may want to set up a special collision layer / mask for these to limit what triggers it.

When these interactive objects enter, the character code will just check which area has entered it, show his ! mark, and process how you wish from there. The interactive object should be the area that comes in, and its script will be accessible too.

There are many ways of mixing and matching these concepts, hard to say which best fits your project.

I did exactly that a moment ago and it worked. Thanks!

momentanius | 2019-03-11 17:42