Pick up one item on many overlapping items?

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

Hi, I’ve come to a problem while making my 2D game tho I might find a clumsy way to work around, I’d like to ask the most efficient way to do it.

I have a ‘player’ that can pick up item on the ground, while checking its “colliding bodies” is in “items” group.
The problem that when many items are in the group, my ‘player’ pick up everything in one go.
Edit: All my items using same ‘scene’ for items.

So what is the most efficient way to make it to pick up one item at a time?

You could keep an array of all items you are overlapping with. Then, when pressing the key/button to pick an item up, select the first item from the array. This is both efficient and fast.

FortunePilot | 2020-04-20 15:09

:bust_in_silhouette: Reply From: Magso

Make a variable to keep the item in and check if anything has been assigned to the variable when picking the item up.

var item

#func/if pick item up:
    if item:
        pass #any other code
        item = body/closest item reference
    #or just
    if !item:
        item = body/closest item reference

#func/if drop item:
    item = null