- Home /
What's the easiest way to check a crafting recipe table?
I have a script that collects the objects being crafted into a list. I just can't find a good tutorial that checks what the combination is and then I need it to spawn a certain game object based on the combo.
all I have is this line
public List(InventoryItem) craft = new List(InventoryItem)();
The list is populated by an outside source, so it fills up. And inside InventoryItem there is an object ID that is an int. So I can check the combo of IDs by a number.
I just need a way to go: if (1,2,3){spawn gameObject}
Answer by ShadyProductions · Jun 15, 2020 at 12:09 AM
Make a scriptable object that contains your recipe data (make these in your inspector)
[CreateAssetMenu(fileName = "Recipe", menuName = "Recipe")]
public class Recipe : ScriptableObject
{
public int[] ItemsToCombine;
public int ResultItemId;
}
Then in your inventory class you can then assign your recipes files in the inspector if you keep a reference to them like this:
public Recipe[] Recipes;
Then all you need is a method to retrieve all available recipes based on your current inventory I took "Items" here as an example, idk what your property is called in your inventory. Then I basically check each recipes 'to combine' list that all the id's are in the inventory.
// Gets all recipes available based on current inventory items
public List<Recipe> GetAvailableRecipes()
{
var list = new List<Recipe>();
foreach (var recipe in Recipes)
{
if (recipe.ItemsToCombine
.All(a => Items.Select(a => a.ItemId)
.Contains(a)))
{
list.Add(recipe);
}
}
return list;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613000941im_/https://answers.unity.com/themes/thub/images/avi.jpg)