Forum rules - please read before posting.

Move Items Into Ingredient Slots via Actionlist?

Hello!

I've been trying to put together one last part of my crafting system in my current project, and I can't seem to find any action in Adventure Creator that may be able to replicate what I'm attempting to achieve.

Here's a small visual example of what I have working at the moment.

Players are able to move their items into the 3 slots on screen, right below the "mix items" button. They can then successfully craft new items when 3 ingredients are placed in each slot and the "mix items" button is pressed.

What I would like to do is give my players the option to place the items into the Cooking pot. (A simple hotspot that interacts based off what item is used on it)

I want the player to be able to grab an item, click on the cooking pot while holding the item, and when selected it will then transfer the selected item into one of the ingredient slots for them. If the first ingredient slot is available the item will be placed there, if the second is, then so on...

While putting together the crafting system for my game I was almost certain I was going to find an action built in AC for something like: "Inventory: Add or Remove" and an option to "add to Ingredients", or something like: "Inventory: Add To Ingredients" but I'm failing to see anything like that in standard AC.

If there's an update that I'm somehow missing that has this type of action, or if there's action I'm not seeing in base AC that can recreate this please let me know!

Otherwise I assume id have to make use of custom actions here?

Comments

  • Also, I am not very good at writing custom actions so if it ends up being too much of a hassle to design I am more than comfortable leaving this small mechanic out of the game, but I thought I'd ask for some help before completely abandoning it.

  • edited June 2023

    It'll need a bit of custom scripting, but not much.

    Define an "Unhandled Inventory" Interaction to your Hotspot, but don't assign an Interaction ActionList. Then, create a new C# script named AutoPlaceIngredients, copy the code below, and attach to the Hotspot as a component:

    using UnityEngine;
    using AC;
    
    public class AutoPlaceIngredients : MonoBehaviour
    {
    
        public string menu = "Crafting";
        public string ingredientsElement = "Ingredients";
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot.gameObject == gameObject && button != null && hotspot.GetButtonInteractionType (button) == HotspotInteractionType.UnhandledInventory)
            {
                InvInstance invInstance = KickStarter.runtimeInventory.LastSelectedInstance;
                InvCollection craftingInvCollection = (PlayerMenus.GetElementWithName (menu, ingredientsElement) as MenuCrafting).IngredientsInvCollection;
                if (craftingInvCollection.GetCount () < 3)
                {
                    craftingInvCollection.Transfer (invInstance, KickStarter.runtimeInventory.PlayerInvCollection);
                    KickStarter.runtimeInventory.SetNull ();
                }
            }
        }
    
    }
    

    Make sure the names of the Menu and Element that you want to move the items to match those in the component's Inspector. Clicking the Hotspot with an item should then move that item to the item to the element.

  • I really appreciate you taking the time to help! It means more than you can imagine!

    I needed to double check if I had the latest version of AC before I came back but I seem to be all up to date! Anyways...

    I followed your instructions but this method did not work for me, at least on my end. Clicking on the hotspot while holding an item does not move the item into the ingredients.

    If I'm following everything correctly, I believe my hotspot's inspector is supposed to look something like this?

    The menu I am using is a Unity UI Prefab named "CraftMenu" and the element connected to my ingredients is simply called "Ingredients".

    If there's something I am not understanding please let me know! Again, thank you so much for your time.

  • Looks correct - are the items being removed from your regular Inventory?

    I just noticed a minor issue with the script - it may not fix the issue, but you'll need to replace it to avoid another.

    If the issue remains, add some Debug.Log statements to the OnHotspotInteract function to check what's being run.

  • Okay It appears to be working as expected now!

    The problem was coming from the way I set up my inventory system.

    In this particular project, the players are supposed to click on hotspots found within the environment to select their items, instead of using a menu.

    I set every action Inventory: Select Item carry condition to "Ignore Current Inventory" because I had no use for it in this case, but changing that option to "Add If Not Carrying" fixed the issue.

  • The items are now moving to the ingredients slot as expected but I'm receiving this error whenever I load a new scene

    "MissingReferenceException: The object of type 'AutoPlaceIngredients' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object."

  • Did you update your script with the one above? That was the issue I mentioned fixing.

    If the items aren't in your inventory, the script can also be adapted to simply add them to the crafting menu without taking them from the Player's inventory:

    using UnityEngine;
    using AC;
    
    public class AutoPlaceIngredients : MonoBehaviour
    {
    
        public string menu = "Crafting";
        public string ingredientsElement = "Ingredients";
    
        private void OnEnable () { EventManager.OnHotspotInteract += OnHotspotInteract; }
        private void OnDisable () { EventManager.OnHotspotInteract -= OnHotspotInteract; }
    
        private void OnHotspotInteract (Hotspot hotspot, AC.Button button)
        {
            if (hotspot.gameObject == gameObject && button != null && hotspot.GetButtonInteractionType (button) == HotspotInteractionType.UnhandledInventory)
            {
                InvInstance invInstance = KickStarter.runtimeInventory.LastSelectedInstance;
                InvCollection craftingInvCollection = (PlayerMenus.GetElementWithName (menu, ingredientsElement) as MenuCrafting).IngredientsInvCollection;
                if (craftingInvCollection.GetCount () < 3)
                {
                    craftingInvCollection.Add (invInstance);
                    KickStarter.runtimeInventory.SetNull ();
                }
            }
        }
    
    }
    
  • Oh I apologize! I didn't update the script until now.

    Both scripts appear to be working as intended now.

    I can't thank you enough for taking the time to help with this, I appreciate it more than you can imagine.

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.