I want crafting recipes to show only if a list (the inventory) contains all the items of a recipe. if an Apple requires 1 Log and 1 Apple and you have the required items you will get two Apples in the crafting menu
var recipeIndex = 0;
var itemIndex = 0;
foreach (Recipes recipe in available)
{
foreach (string item in recipe.items)
{
Debug.Log(recipe.name + " items: " + item);
if (!inventoryItems.Contains(item))
{
return;
}
if (inventoryItems.Contains(item))
{
craftable.Add(recipe);
//craftable = new HashSet<Recipes>(available).ToList();
}
itemIndex++;
}
recipeIndex++;
}
for (int a = 0; a < craftable.Count; a++)
{
instantiateSlotsScript.craftingSlots[a].GetComponent<SlotCrafting>().item[0] = craftable[a].name;
}
foreach (Recipes recipe in available)
foreach (string item in recipe.items)
if (inventoryItems.Contains(item))
craftable.Add(recipe);
for (int a = 0; a < craftable.Count; a++)
instantiateSlotsScript.craftingSlots[a].GetComponent<SlotCrafting>().item[0] = craftable[a].name;
I used return so I will not end up with multiple of the same recipe in the crafting menu. This works fine but if I have 2 Logs now the Apple recipe is gone. This is when I have a second recipe like an Axe that requires 2 Logs as well
Well, we don’t know all details about your inventory and if items can actually be “stacked” or if recipes can require multiple of the same item. However if you really just have a list ot items required and the pure existance is enough, you should do
craftable.Clear();
foreach (Recipes recipe in available)
{
bool allItemsAvailable = true;
foreach (string item in recipe.items)
{
if (!inventoryItems.Contains(item))
{
allItemsAvailable = false;
break;
}
}
if (allItemsAvailable)
craftable.Add(recipe);
}
This will fill the craftable List / hashtable only with recipes where all necessary items are available.
Instead of a string, you’ll want to use an object to represent the required item so that you can store not just a name/identifier, but also an amount to go along with it.
Yes. Games like Minecraft have the concept of an “ItemStack”. So an inventory slot actually stores item stacks which is an item reference as well as an amount. Still no string required. The actual item types can be ScriptableObject instances for example.
I still get two of the same recipe in the crafting menu because if I have 1 Log the Apple will show up and then if I have 1 Log and 1 Apple, two Apple recipes will show