How to create Godot item pickup system efficiently?

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

I want to be able to pickup items and let the items be affected by gravity. I am currently using this setup:

Rigidbody2d
—Sprite
—CollisionShape2D
—Area2D
------CollisionShape2D

I use the Area2D to detect when the player(KinematicBody2D) touches the item. Problem is with this setup when you have more than 100 items in one spot it starts to lag since it uses 2 CollisionShape2D. How could I still detect the player with only 1 CollisionShape2D?

:bust_in_silhouette: Reply From: Magso

Both Rigidbody2d and Area2D need collision shapes. Instead of keeping all 100+ items in one position, do whatever is aesthetically necessary, store the items in an array and use remove_child to hide them.

These are supposed to be items so the player can stack 100 in the world if they want.

jujumumu | 2019-08-16 01:19

Ok, you could put the Area2D on the player instead of one on each item and swap the code references around from the item script into the player script. Still, I would’ve thought the lag would be due to the large amount of MeshInstances and environment effects.

Magso | 2019-08-16 11:47

What do you mean by MeshInstances and environment effects I think it is due to the number of collision pairs when they are pooled together.

jujumumu | 2019-08-16 23:34

Sorry I forgot you’re using 2D. Have you checked the profiler?

Magso | 2019-08-17 00:24

What do I do in the profiler never used it.

jujumumu | 2019-08-17 03:25

Click start and run the game and tick what you want to see the performance of, in your case it would be Physics2D.

Magso | 2019-08-17 09:47