Forum rules - please read before posting.

Crafting Items with Multiple Outputs

I'm working on a crafting mechanism that allows the user to combine door keys based on the primary colors (red, yellow, blue) into new keys based on secondary colors (red+blue=purple, yellow+blue=green....) but part of the puzzle is that need to be able to "uncraft" an item back to its component parts(e.g. break down a purple key back to red and blue)

I can get the inventory management working using custom action lists, but I can't figure out a way to change the crafting output window to display multiple items (even using a custom action list, it still seems to just display whatever is listed in the "Resulting Item" drop down in the recipe manager.

Is there a way? Should I code a custom crafting UI instead?

Comments

  • edited October 2020

    Welcome to the community, @frankyp.

    The crafting system is limited to one "created" item per recipe. A custom UI may be necessary (though this should be made easier by the updated inventory API in v1.72.0), but it may be possible to cheat things a little bit.

    Taking your example of uncrafting purple to red and blue, I can think of a couple of hacky ways you could go about it:

    1. Create a special "red key + blue key" inventory item, and set this to be the resulting item. This would have both keys visible in its icon. Then, as part of the ActionList when create, you could then run a series of Inventory: Add or remove Actions to replace this special item with two separate "red key" and "blue key" items.
    2. If you wanted two show the two resulting keys in separate crafting boxes, you could set the recipe's resulting item to "red key", and then use a bit of scripting to show the "blue key" graphic in a separate UI element.

    It should be a case of hooking into the OnInventoryAdd_Alt custom event, which can be used to run custom code in the event that the Player added an item to the crafting table. If so, check which Recipe is now valid - and update a RawImage texture with a specific item if so:

    using AC;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class SecondRecipeOutput : MonoBehaviour
    {
    
        public RawImage otherItemImage;
    
    
        private void OnEnable ()
        {
            otherItemImage.enabled = false;
    
            EventManager.OnInventoryAdd_Alt += OnInventoryAdd;
            EventManager.OnInventoryRemove_Alt += OnInventoryRemove;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnInventoryAdd_Alt -= OnInventoryAdd;
            EventManager.OnInventoryRemove_Alt -= OnInventoryRemove;
        }
    
    
        private void OnInventoryAdd (InvInstance invInstance, InvCollection invCollection, int amount)
        {
            if (invCollection == KickStarter.runtimeInventory.CraftingInvCollection)
            {
                Recipe recipe = KickStarter.runtimeInventory.CalculateRecipe ();
                if (recipe != null)
                {
                    // Succesful
                    if (recipe.id == 2)
                    {
                        // ID 2, so show item 3
                        otherItemImage.enabled = true;
                        otherItemImage.texture = KickStarter.inventoryManager.GetItem (3).tex;
                    }
                }
            }
        }
    
    
        private void OnInventoryRemove (InvInstance invInstance, InvCollection invCollection, int amount)
        {
            otherItemImage.enabled = false;
        }
    
    }
    

    In this test script, it'll check if Recipe 2 is valid, and then displays Inventory Item 3 in a Raw Image component. You can change the numbers / put things into a custom data set if that's the approach you want, but this is just a quick proof of concept.

  • Thanks so much for the detailed answer! Was just about resigned to giving the key building task to an NPC and dialogue tree. I’ll give your suggestion a try.

    I’m new to Unity, but not to programming in general, already Adventure Creator has saved me countless hours, and was well worth the price!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.