How to design item pickup functionality for an inventory system.

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

Disclaimer: I am a coding novice.

Hello!
I have recently been learning about inventory systems in GDScript and have been trying to implement it in a personal project. This tutorial has been a huge help in this regard: GitHub - ThriveVolt/inventory-tutorial at 8-display-item-tooltip

The setup works great but I am struggling with implementing an actual item pickup system. The idea is a simple 2d space where the player character can press a button to pick up an item present in the world.

I was wondering if people had some pointers or tips on how to approach this based on that code. Any assistance would be much appreciated!

Also, based on a previous question I saw here I found a fairly easy way to handle removing the actual object after “collecting”. But that’s about as far as I could make it without having everything break :slight_smile:

func _physics_process(delta: float) -> void:
	if Input.is_action_just_pressed("interact"):
	  for body in $InteractRange.get_overlapping_bodies():
		if body.is_in_group("herbs"):
			body.queue_free()

Gebbu5 | 2023-02-24 12:20

:bust_in_silhouette: Reply From: exuin

I didn’t read the tutorial, disclaimer

Anyway, you don’t need to check every frame. Just check once when the event is created. Also, you’ll need some kind inventory system that you can call a function on and store the item on the body.

func _unhandled_input(event: InputEvent) -> void:
  if event.is_action_pressed("interact"):
    for body in $InteractRange.get_overlapping_bodies():
      if body.is_in_group("herbs"):
          # One way
          # Inventory.add_item(body.pickup_item)
          # Another way, make sure to connect the signal to Inventory
          # emit_signal("add_item", body.pickup_item)
          body.queue_free()

thank you that already helps me quite a bit! The inventory system itself is already in place and functional if I preload some items into it. I’ll have a look at the documentation around signals again because I’m still learning how they work.

Gebbu5 | 2023-02-25 11:45