Crafting help checking for more than one material

Well I need help with my checking if inventory contains the itemID and the itemAmount,
my code works just fine but it checks for only one of the materials at a time.
Lets say I need: 3 wood and 5 rocks to make a firepit.

So I run this:

//Getting all the recipes.
        for (int i = 0; i < recipes.Count; i++)
        {
            //Getting the materials for the recipe.
            foreach (KeyValuePair<int, int> materials in recipes*.materials)*

{
//Splitting up the materials in the recipe [itemID = materials.Key, itemAmount = materials.Value]
//And checking if the inventory contains the itemID and the itemAmount.
print("Material: " + materials.Key + " Amount: " + materials.Value);

if (inventory.CheckForItemIDAndItemAmount(materials.Key, materials.Value))
{
//If inventory contains the itemID and itemAmount add blank item to the craftible list.
craftableItems.Add(new ItemTest());
}
}
}
The proplem is lets say I do have the 3 wood. it returns true and adds the blank item even though I don’t have the 5 rocks. or vise versa
Because its only checking for the materials one at a time and returning true.
public bool CheckForItemIDAndItemAmount(int itemID, int itemAmount)
{
//Checks how many items are in the inventory
for (int i = 0; i < inventoryItems.Count; i++)
{
//For every item in the inventory it checks the itemID and the itemAmount
if (inventoryItems_.item.itemID == itemID && inventoryItems*.itemAmount >= itemAmount)
{
//So the inventory does contain the item and the right amount*
return true;
}
}
//The inventory either doesn’t have that item or not enough of that item
return false;
}
So how could I check if the inventory contains all the materials and amounts before adding the blank item._

Something like that should work with the info you provide

for (int i = 0; i < recipes.Count; i++)
        {
            
                if (CheckRecipesMats(i))
                {
                    //If inventory contains the itemID and itemAmount add blank item to the craftible list.
                    craftableItems.Add(new ItemTest());
                }
            }
        }

bool CheckRecipesMats(int recID)
    {
        foreach (KeyValuePair<int, int> materials in recipes[recID].materials)
        {
            print("Material: " + materials.Key + " Amount: " + materials.Value);
            
            if (!inventory.CheckForItemIDAndItemAmount(materials.Key, materials.Value))
            {
                return false;       //Breaks the loop and returns false on the first missing item
            }
        }
        return true;    //Inventory Contains All
    }

All that does is Checkin all the materials in a single function (it uses your function CheckForItemIDAndItemAmount to do so) and if all are present it returns true if any of them missing it will break the loop and return false. Hope that helps. For clarifications ask away.

for (int i = 0; i < recipes.Count; i++)
{
bool flag = true;
foreach (KeyValuePair<int, int> materials in recipes*.materials)*

  • {*

  •  print("Material: " + materials.Key + " Amount: " + materials.Value);*
    
  •  if (!inventory.CheckForItemIDAndItemAmount(materials.Key, materials.Value))*
    
  •  {*
    
  •  	flag = false;*
    
  •  }*
    
  • }*

  • if (flag)*

  •  craftableItems.Add(new ItemTest());*
    

}
Just use a flag variable.
If all materials are available then flag will remain true → show item.
If missing one ore more then flag == false → not show.

Yeah, that fixed my propblem. I never thought that its was something so simple either.

Thank you. Thanks alot!!!