Area2D.get_overlapping_bodies() returns unexpected results

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

I have a very simple project that I just started. The main scene consists of an instanced scene containing an Area2D and an instanced scene containing a KinematicBody2D. The Area is in Layer 2 and masks Layer 3. The Body is in Layer 3 and masks Layer 1, 2, and 3. Calling get_overlapping_bodies() on the Area while overlapping the Body, returns an empty array, when I would expect it to return an array containing the Body.

Now here’s where it gets extra weird. If I set the Area to mask Layer 1, then the overlap registers. I’ve tried messing around with different combinations of layers and it seems as though the only determining factor for whether or not the Body appears in that array is if the Area masks Layer 1, even though the Body isn’t even in Layer 1. Is this a bug?

I feel like I always run into some confusion when working with collision layers, but I usually am able to get to a solution, but this time its really busting my brain!

(Btw the context for this is that I want to create a system for a top-down rpg where you can walk up to something and press a button to interact with it. The way I thought to do it is that when the user presses the button, the Area will check if there’s anything inside it that it could interact with. I’m doing a class check to check if its something that it can interact with, and that works, but theoretically the Area should also only have to check layer 3, because that is the layer I will be putting all of my interactive things on.)

:bust_in_silhouette: Reply From: gracieart

I solved it! This answer might be useful to some who get confused with instancing.

I went back to the main scene and looked at the inspector for the Body instance, and the collision and mask layers were both just set to 1, the default values, and that was overriding the layer settings of the underlying scene. I assumed that changed properties would update in every unaltered instance of that scene, which is usually what happens, so I don’t know why it didn’t happen here. So always double check your instances because sometimes you have to hit that little reset button next to the properties.