Crafting
Crafting is often used when working with a lot of items. There are so many types of crafting systems that it would be impossible to create a system that fits all. The included crafting system is very generic and scalable while at the same time can do very simple crafting by default.
There are a few key components when dealing with crafting:
- Crafting Category: Used to organize recipes, similar to Item Category for organizing items.
- Crafting Recipe: The data used to specify the ingredients and default output.
- Crafting Ingredients: A collection of objects used as inputs for a recipe.
- Crafting Output: A collection of items used as output for a recipe.
- Crafting Processor: The logic that crafts items using a recipe as a guideline.
- Crafting Result: The result from the crafting process containing a crafting output (the craft items) and additional data such as whether the crafting process was successful or not.
- Crafter : The component which has the Crafting processor and a list of the recipes which can be crafted.
These components can be extended to design an extremely complex crafting system. For example the crafting processor could take the player data as input to know whether they can craft an item, a recipe could give different outputs depending on the actual ingredient used, etc.
The crafting system will not do exactly what you want out of the box. The following will showcase the default functionality which allows for a slightly complex crafting scenario.
Crafting Recipe, Ingredients, and Output
As specified before, crafting recipes are guidelines for the crafting processor. Therefore they are quite abstract. Out of the box the recipe has ingredients and an output.
Ingredients contain a list of amounts of Item Categories, Item Definitions and Items. In simple crafting systems you would use Item Definitions as ingredients, such that any item of the Item Definitions specified could be used as ingredients in the crafting process.
Output is a list of item amounts. Out of the box you can define a simple recipe such as
2 Heal weed + 1 spring water => 1 heal potion
But you can also make a more specific recipe:
1 heal potion (with quality == good as an item attribute) + 1 magic orb => 1 health magic orb
Or something more abstract
1 sword + 5 material => 1 great sword
where material is a category and can be any item of that category. For example:
1 sword + (2 iron ingot + 1 cotton + 2 rocks) => 1 great sword
With a little creativity the out of the box solution can be used in interesting ways. The extensibility and scalability makes it easy to create your very own crafting system.
Crafting Processor
Recipes can be bent a little to your liking since they are guidelines and not rules. This means that two crafting processors which use the same recipe can have different results.
The processor takes in a recipe and a list of selected items and then tells you if it can craft or not. If it can, then you can craft the item, which will remove the selected items from the inventory and add the crafted item.
/// <summary> /// Craft the items. /// </summary> /// <param name="recipe">The recipe.</param> /// <param name="inventory">The inventory containing the items.</param> /// <param name="selectedIngredients">The items selected to craft the recipe.</param> /// <param name="quantity">The quantity to craft.</param> /// <returns>Returns a crafting result.</returns> public CraftingResult Craft(CraftingRecipe recipe, IInventory inventory, ListSlice<ItemInfo> selectedIngredients, int quantity = 1)
For convenience the processor can also auto select valid items to craft directly from the inventory. It will do so by default if you do not specify a list of items.
The Crafting Processor is quite basic, and it is meant to be easily extendable to suit your needs. You may decide to extend the crafting processor to allow crafting only when your character has a certain crafting level, or change the result of the crafting processing depending on the quality of the ingredients used, etc…
Crafter
The crafter is the component which has the Crafting Processor and the list of Item Categories which can be crafted. By default it uses a “Simple Crafting Processor With Currency” which may be changed in the inspector using the dropdown. Your custom Processor should automatically appear as an option once created.
- Remove Ingredients Exernally: In some cases you may want to remove the ingredient item differently than normal, if true the event “EventNames.c_InventoryGameObject_OnCraftRemoveItem_CraftingProecessor_ItemInfoListSlice_ActionBoolSucces” will be used to remove the ingredients.
- Crafting Categories: All the recipes in that category will be added in the list of craftable recipes.
- Miscellaneous Recipes: A list of craftable recipes.
// Edit the list of recipes which can be crafter by adding/removing recipes from the list List<CraftingRecipes> recipes = m_Crafter.CraftinRecipes; // Change the Crafting Processor at anytime m_Crafter.Processor = newProcessor;
The Crafter is used by the Crafting Menu. Learn more about it here