How to: lock/unlock recipe?

filod

Member
I want to build a crafting system which unlock/show recipe only when player retrive one of ingredients(guide player and reduce redundancy infomation). Any suggestion how ?
 
The Crafter component has a serialized field for setting the recipes in the inspector.
But instead of setting them in the inspector you can set them through code.

There should be a Utility script that lets you know what recipes the item is an ingredient of. I use it in the Editor window to know show this window:
1646674827216.png
Code:
var itemDefinition = InventorySystemManager.GetItemDefinition("Apple");
var allRecipesArray = InventorySystemManager.CraftingRecipeRegister.GetAll().ToArray();
var directRecipes = InventoryDatabaseUtility.DirectItemDefinitionRecipes(allRecipesArray, itemDefinition);
//or get the inherited recipes if you want.
var inheritedRecipes = InventoryDatabaseUtility.InheritedItemDefinitionRecipes(allRecipesArray, itemDefinition);
//set those recipes in the crafter
var crafter = GetComponent<Crafter>();
crafter.SetRecipes(directRecipes);
//or add them
for (int i = 0; i < inheritedRecipes.Count; i++) {
    crafter.AddRecipe(inheritedRecipes[i]);
}

Note that those utility functions are not very performant since I made them to be used in the Editor. I'd recommend you look into caching the results and saving them in a dictionary so you don't have to recompute the recipes for the same items each time.
I hope that helps
 
Top