Forum rules - please read before posting.

Can ActionList get the result inventory item as a parameter when the craft item is created?

https://streamable.com/4o9bic
As shown in the figure, can ActionList get the result inventory item as a parameter when the craft item is created?

Comments

  • No, but it's a good suggestion - I'll look into this as an option.

    In the meantime, you can set your "ActionList on create" asset to a single ActionList: Run Action that runs a separate asset - which does have an Inventory item parameter - and sets the parameter value manually at the same time.

  • I have no way to know WHAT the output is. How do I set the parameters manually?

  • You'd set the parameter manually to the ingredient item that the recipe creates. Though, in pratice this would refer to the first-found instance of that item in your Inventory. If you have multiple instances of the item and want to refer to the specific item instance that was created, you'll need to rely on scripting.

    Can you describe what it is you intend to do with the item, within the ActionList? It may be that a custom script is more appropriate.

  • I'm making a cooking system. The player can use the ingredients in the backpack to cook at will, and then different combinations will produce different final food, and depending on the final food will have a different time of cooking process. In this process, the player has to wait for the food to be ready instead of getting it right away.

  • All combinations are pairwise, and any combination except for a particular combination will only produce a strange dish.

  • If you want to delay the resulting item from showing, uncheck Result is automatic? in the Crafting Output element, and use a script to set the output after a delay by hooking into the OnInventoryAdd/Remove custom events.

    This timer can optionally be passed to a UI Slider element as well:

    using UnityEngine;
    using UnityEngine.UI;
    using AC;
    
    public class CookingTimer : MonoBehaviour
    {
    
        public string menuName = "Crafting";
        public string craftingOutputName = "Crafting";
        public Slider slider;
        public readonly float cookingTimer;
        public readonly float maxCookingTime;
    
        void OnEnable ()
        {
            EventManager.OnInventoryAdd_Alt += OnInventoryUpdate;
            EventManager.OnInventoryRemove_Alt += OnInventoryUpdate;
        }
    
        void OnEnable ()
        {
            EventManager.OnInventoryAdd_Alt -= OnInventoryUpdate;
            EventManager.OnInventoryRemove_Alt -= OnInventoryUpdate;
        }
    
        void OnInventoryUpdate (InvCollection invCollection, InvInstance invInstance, int amount)
        {
            MenuCrafting outputBox = PlayerMenus.GetElementWithName (menuName, craftingOutputName) as MenuCrafting;
            InvCollection ingredients = outputBox.IngredientsInvCollection;
            if (ingredients == invCollection)
            {
                Recipe activeRecipe = KickStarter.runtimeInventory.CalculateRecipe (ingredients);
                if (activeRecipe != null)
                {
                    // Calculate cooking time here
                    cookingTimer = 1f;
                }
                else
                {
                    cookingTimer = 0f;
                }
                maxCookingTime = cookingTimer;
            }
        }
    
        void Update ()
        {
            if (cookingTimer > 0f)
            {
                cookingTimer -= Time.deltaTime;
                if (cookingTimer <= 0f)
                {
                    MenuCrafting outputBox = PlayerMenus.GetElementWithName (menuName, craftingOutputName) as MenuCrafting;
                    outputBox.SetOutput ();
                }
            }
    
            if (slider)
            {
                slider.value = cookingTimer / maxCookingTime;
            }
        }
    
    }
    
  • https://streamable.com/eqvr0uThere are some errors in the code snippet.

  • edited September 2022
        void OnInventoryUpdate(InvCollection invCollection, InvInstance invInstance, int amount)
        {
            InvCollection ingredients = KickStarter.runtimeInventory.CraftingInvCollection;
            if (ingredients == invCollection)
            {
                Recipe activeRecipe = KickStarter.runtimeInventory.CalculateRecipe();
                if (activeRecipe != null)
                {
                    InvInstance resultInvInstance = new InvInstance(activeRecipe.resultID);
                    InvVar invVar = resultInvInstance.GetProperty("cookingTime");
                    cookingTimer = invVar.FloatValue;
                }
                else
                {
                    cookingTimer = 0f;
                }
                maxCookingTime = cookingTimer;
            }
        }
    

    I changed it to this, and it works well!

  • The code is for the latest release, which made changes to AC's Recipe system. If and when you update your AC package, you'll need to revert to the original code.

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.