Is it possible to detect only 1 area2D overlapping another?

Godot Version

Godot 4.2.2

Question

When the players item(area2D) overlaps with multiple items the player picks up all items at the same time. I was wondering if it’s possible to only detect one?

All items have this script.

if $Area2D.get_collision_mask_value(1) and $Area2D.get_overlapping_areas():
if pickup:
if Input.is_action_just_pressed(“drop”):
holding = true

Have some way for the player’s item to track whether it picked up an item on this frame or not. In the other items’ code, check that value and do nothing if some other item already activated it.

Alternatively, if the other items don’t care about each other, it might be simpler and more efficient for just the player (or their item) to iterate over possible items to pick up, and stop after the first one. Then you’re only running the pickup-function once, and not calling $Area2D.get_overlapping_areas() on each item for all other items, when all we really care about is the player’s.

1 Like

If I understood you right, you want to pickup only the last item that you have detected, I guess it would be better to detect it through the Player’s script (example below):

You can see that I’ve added “current_detecting_items” variable which contains all items that’re detecting right now. Then, if you want to pickup not the last detected item - you simply can modify my 15th line of code to whatever you want (it’s not the best solution, I would prefer to add separate component for it, but it will work fine)

1 Like

Would that work or should I use body overlapping instead?

[This reply is about clean code]
To be honest, I don’t understand your question :sweat_smile:
However, you definitely should try to write a bit cleaner code)

  1. 22-29 and 35-42 lines are similar, wouldn’t it be better to add a separate function for it?) [in this case I’d recommend another option below]
  2. The line 33 is unnecessary cause you have checked it on 20th line

As I said, 22-29 and 35-42 lines are similar, you should avoid duplicates in code. There’re only holding variable is different from each other

Instead You can do this:

1 Like

Got you right now, if all your items 100% has Area2D and it could be detected - you can use area overlapping
Body overlapping is for bodies, that also has the same Mask_Value but not in Area2D (like CharacterBody2D, StaticBody2D, …)

lol my apologies still learning and thank you for helping out. Basically the player holds an item(area2d) and when that item overlaps other item(aread2d) and holds “drop” the player’s item drops and picks up the new item but if 2 items overlap the players item both get pick up and I was wondering if it was possible to detect multiple area2Ds overlapping and when “drop” is pressed nothing happens.

Don’t worry, everybody learns)
If you want to know - are there more than 1 overlapping areas - you just need to add this:
If $Area2D.get_overlapping_areas().size() > 1:
# More than 1

But this method isn’t the fastest, it’ll still work fine. However, I’d recommend you to learn about Signals in Godot (like Area2D has signals area_entered and area_exited)

Tutorial about signals by GDQuest: https://youtu.be/Qlq8pBB2htg?si=_rqRU39gPNNnlkJW

1 Like

That worked! Thank you :slight_smile:

You’re welcome! Good luck with your game :grin:

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