1

I'm working on an Inventory System with a Crafting System. When I first made it it was working, it crafted items if the player had enough resources in Inventory to craft it and then removed the items that were used to craft the item and add the item we just crafted. But now the part where it should delete items that were used to craft the object doesn't work.

Function where everything starts:

public bool CraftItem() {

    //if CanCraft returned false we return false from function
    if(!CanCraft()) {

        return false;

    }

    //start crafting
    ParentCraftingSlot.StartCrafting();

    //call RemoveIngredientsFromInventory to remove item from inventory
    RemoveIngredientsFromInventory();

    //return true
    return true;

}

RemoveIngredientsFromInventory function:

//remove ingredients from inventory
//because we don't want to have the ingredients after we crafted something
private void RemoveIngredientsFromInventory() {

    foreach(Ingredient ingredient in ingredients) {

        //call RemoveItems function with ingredient item and amount
        Inventory.instance.RemoveItems(ingredient.item, ingredient.amount);

    }

}

RemoveItems function:

//RemoveItems from inventory
public void RemoveItems(Item item, int amount) {

    //loop through amount of items
    for(int i = 0; i < amount; i++) {

        //remove item from Inventory
        RemoveItem(item);

    }

}

and then RemoveItem function (i have it as a different function because I need to remove the item from the list and then invoke the changes so it's displayed and then I don't have to write it every time I just have to call the function):

//Remove Item function
public void RemoveItem(Item item) {

    //remove item from IntentoryItemList list
    InventoryItemList.Remove(item);
    onItemChange.Invoke();

}

I tried to write something into console inside the RemoveItem function and it wrote it into console but for some reason, it didn't remove the ingredients.

Ratstool
  • 5
  • 3
  • I'd suggest examining `InventoryItemList` before and after the Remove(item) to confirm if it is actually removing from that list. If it is not removing, then I'd guess that the local `item` isn't the same item that's in your list. If it is removing the item there, then it's likely that `InventoryItemList` in your `RemoveItem()` function isn't the real list but a copy of it that got created somewhere. It's also possible that `InventoryItemList` isn't even a copy of your real list, but just an empty version with the same name locally. (I see that you are not checking Remove's return value). – RBarryYoung Feb 25 '21 at 16:21
  • @RBarryYoung i tried to look at the InventoryItemList in the editor while it should remove the item, and the item must be the same that is in the list because i tried to craft something with just one thing in my inventory (that recipe required that same thing once) and it still didn't work, and i don't know why it should create copy of the InventoryItemList, i don't remember i ever did something like that, and also i didn't change like 99% of the code that was there before when it worked –  Feb 25 '21 at 16:25
  • I've updated my comment some (above). You need to look at the entries in the list with Watch in the debugger, do not rely on your crafting code for a low-level explicit examination like this. You can (should?) also check the return value from the Remove(), if it isn't True then something's wrong. – RBarryYoung Feb 25 '21 at 16:28
  • Also, to re-emphasize, the `Item` that you pass to the `Remove(item)` function *must* be *the same object* that is in the List, that is it *must* have the same address in memory. Merely being the same class-type and having the same values is not enough (unless there's special equality code for this). If `Item` is a value-type then none of this applies, but if it is an object-type then *usually* Remove will only find and remove it if it is the same actual object that you passed in, a clone will not match. – RBarryYoung Feb 25 '21 at 16:33
  • 1
    @RBarryYoung you were right, for some reason the Item in Remove(item) isn't the same, i tried to first add the item to InventoryItemList and it added the same type of items inside InventoryItemList –  Feb 25 '21 at 16:41

0 Answers0