How to Remove an Item from Player's Inventory and add it to an NPC's Inventory

Godot Version 4.5

Hello! I used the Michael Games’ inventory tutorial on YouTube to build my inventory. I will start off with the fact that I am very much a beginner at Godot and have a lot to learn! I am making a 2D pixel game with the Dialogic2 plugin. Basically when you speak to an NPC you have two options: “Talk” or “Give Gift.” But I dunno how to remove an item from the Player’s inventory and add it to the NPC’s. I think I can figure out how to get them to react to each unique item- but I just don’t know how get started with those particular functions. If you need screenshots of anything- lmk. Thank you in advance! :smiley:

It depends on a lot of things and you need to be more specific about what you want and what you already have.
For example is this a one time thing or something you will do like 3 or up to hundreds or more times? Possible to give stuff to one or a large numbers of different NPCs?
Depending on if it is something you will do a few times or if it is a core gameplay element you will want different solutions.

Just as a simple general example you could maybe do something similar to this to

var inventory: Dictionary
var NPC_to_get_gift
func give_item(caller, inventory_key):
     NPC_to_get_gift = caller
	if inventory.has(inventory_key):
             var item = inventory[inventory_key]
	         NPC_to_get_gift.inventory[inventory_key] = item
		     inventory.erase(inventory_key)
		
		

Your NPC would also need a dictionary with the name inventory.
If you have multiples NPCs, just call the function including “self” as the first argument. If only one NPC will call, you can skip that argument and remove it from the function above as well.
Not 100% this will work as i haven’t tested it but it is a hopefully working solution that you can change a bit depending on your needs.

Also wanted to add that this solution will overwrite other items with the same key. When you wrote “unique items” i assumed that it meant that these items were one of a kind type of quest items, but it looks like you mean each type of item (like potion, arrow, egg)?
In that case, better to use an array and append whatever you want to give so it doesn’t overwrite anything already there

1 Like

Thank you for your response. I’m looking into other solutions but this gives me something to think on!