selectedIngredients GetValue() override value?

jimi

Member
I'm building a custom crafting processor and attempting to get the mutable variant property of an attribute on the selected item while crafting. However, I can't quite figure out how to get the current item with Custom Item Attributes opposed to the default values.

Does the selectedIngredients get the actual item in the inventory with custom Item Attributes or is it just a reference to the default item?

C#:
            var  ingredientWidthValue = 10;
            var ingredient = selectedIngredients.Last.Item;
            var ingredientWidth = ingredient.GetAttribute<Attribute<int>>("Width");
            //ingredientWidth.SetVariantType(VariantType.Override);
            ingredientWidthValue = ingredientWidth.GetValue();




Full Method:

C#:
protected override CraftingResult CraftInternal(CraftingRecipe recipe, IInventory inventory, int quantity,
            ListSlice<ItemInfo> selectedIngredients)
        {
            // Check that the recipe can be crafted.
            if (CanCraftInternal(recipe, inventory, quantity, selectedIngredients) == false)
            {
                return new CraftingResult(null, false);
            }

            // Remove all the ingredient items from the inventory.
            if (RemoveIngredients(inventory, selectedIngredients) == false)
            {
                return new CraftingResult(null, false);
            }


            // Get the result of the craft.
            var  ingredientWidthValue = 10;
            ItemAmount[] resultItemAmounts;

            var ingredient = selectedIngredients.Last.Item;
            var ingredientWidth = ingredient.GetAttribute<Attribute<int>>("Width");
            ingredientWidth.SetVariantType(VariantType.Inherit);
            ingredientWidthValue = ingredientWidth.GetValue();

            Debug.Log(ingredientWidthValue);

            resultItemAmounts = new ItemAmount[recipe.DefaultOutput.ItemAmounts.Count];
            for (int i = 0; i < resultItemAmounts.Length; i++)
            {
                var itemAmount = recipe.DefaultOutput.ItemAmounts[i];
                var craftedItem = InventorySystemManager.CreateItem(itemAmount.Item);


                var qualityAttribute = craftedItem.GetAttribute<Attribute<int>>("Width");
                if (qualityAttribute != null)
                {
                    qualityAttribute.SetOverrideValue(ingredientWidthValue);
                }

                var craftedItemAmount = new ItemAmount(craftedItem, itemAmount.Amount * quantity);
                resultItemAmounts[i] = craftedItemAmount;
            }


            // Add the crafted items in the inventory.
            for (int i = 0; i < resultItemAmounts.Length; i++)
            {
                inventory.AddItem((ItemInfo)resultItemAmounts[i]);
            }

            // Convert the crafted items into an crafting output.
            var output = new CraftingOutput(resultItemAmounts);

            // Return a success crafting result.
            return new CraftingResult(output, true);
        }
 
This has been solved. Noticed I was using a Item Category rather than an Item Definition to define the recipe and therefore getting unexpected results.

The above code does work as expected and will leave up if anyone wants to craft a new item based on a custom item attribute in the inventory.
 
Yes this is all correct, I'm glad you figured out the issue.

For a bit of background knowledge on why the recipes have an ItemCateogry, itemDefinition and Item ingredient list, it is such that you may create more abstract or more precise recipes. Most of the times though the item definition ingredient list is enough.

Of course if you wish to have even more data in your recipes, you can create a custom recipe type and set it in your crafting category.
 
Top