+1 vote

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?

in Engine by (18 points)

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.

1 Answer

+2 votes

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
by (3,249 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.