Forum rules - please read before posting.

Inventory Use Action

Hello -

The short version of my questions is this: How can a get a reference to the inventory item that was clicked from the Use Action that gets executed?

The longer version:

I'm attempting to generate new items in the inventory via script. This works:

        items = ORK.Game.ActiveGroup.Leader.Inventory.Items.GetAll();
        InvItem itemTemplate = KickStarter.inventoryManager.GetItem("ORK Item Template");

        for (int i = 0; i < items.Count; i++) {

            InvItem invItem = new InvItem(itemTemplate);
            invItem.label = items[i].GetName();
            invItem.altLabel = items[i].GUID;
            invItem.tex = items[i].GetIconTexture();
            invItem.canCarryMultiple = true;
            invItem.count = items[i].Quantity;
            invItem.maxCount = 9999;

            InvInstance invInstance = new InvInstance(invItem, invItem.count);
            KickStarter.runtimeInventory.PlayerInvCollection.Add(invInstance);
        }

What this does is take all the items from the ORK framework inventory system and creates new AC items for each using an item template I created in the Inventory Manager. This template has a Standard Use interaction assigned (Action List Asset). This Asset List Asset runs a single custom action which intends to run the action associated with the item in the ORK Framework system (for example click a health potion would heal the character). The problem is I don't know how to get the reference to the inventory item I just clicked from the custom action. I've tried two things so far:

  • Using a string parameter on the assigned Action List Asset and then setting the parameter to the ORK item GUID in the code above. This works, except that it's setting the Action List Asset parameter globally (not for an individual instance used by the inventory item) so the param is always equal to the last item created. If there was some way I could instance the Asset List Asset for each generated item and then set the param for the instance, this approach would probably work.

            List<ActionParameter> newActionParameterList = invItem.useActionList.GetParameters();
            newActionParameterList[0].SetValue(items[i].GUID);
            invItem.useActionList.AssignParameterValues(newActionParameterList);
    
  • Change the param type to "Inventory Item" hoping that the clicked inventory item would be automatically passed into this variable. I had read a forum post stating that this is how the GameObject param type works with Hotspots, so I thought it might work with clicked Inventory Items in this way. It does not appear to.

Thanks for your help!
-Mike

Comments

  • Welcome to the community, @indoflaven.

    Normally, you can make use of AC's Set Inventory Interaction Parameters component to assign parameter values for an Inventory interaction. However, this won't be viable if you're generating your Items at runtime.

    In script, you can get a reference to the clicked item instance by hooking into the OnInventoryInteract_Alt custom event:

    private void OnEnable () { EventManager.OnInventoryInteract_Alt += OnInventoryInteract_Alt; }
    private void OnDisable () { EventManager.OnInventoryInteract_Alt -= OnInventoryInteract_Alt; }
    
    private void OnInventoryInteract_Alt (InvInstance invInstance, int iconID)
    {
        Debug.Log ("Clicked item " + invInstance.InvItem.label + ", " + invInstance.InvItem.id);
    }
    

    Updating the original ActionList asset's parameters is often more convenient - indeed, this is how Set Inventory Interaction Parameters works. You can check Revert to default parameter values after running? to prevent the ActionList asset from retaining its parameter values.

    If you did need to access the runtime instance of the ActionList, however, you can do that by hooking into the OnBeginActionList event. This will be fired after OnInventoryInteract_Alt, so you could combine the two to first record the item, and then modify the parameter:

    private InvItem clickedItem;
    public ActionListAsset useAsset;
    
    private void OnEnable ()
    {
        EventManager.OnInventoryInteract_Alt += OnInventoryInteract_Alt;
        EventManager.OnBeginActionList += OnBeginActionList;
    }
    
    private void OnDisable ()
    {
        EventManager.OnBeginActionList -= OnBeginActionList;
        EventManager.OnInventoryInteract_Alt -= OnInventoryInteract_Alt;
    }
    
    private void OnInventoryInteract_Alt (InvInstance invInstance, int iconID)
    {
        clickedItem = invInstance.InvItem;
    }
    
    private void OnBeginActionList (ActionList actionList, ActionListAsset actionListAsset, int startingIndex, bool isSkipping)
    {
        if (clickedItem && actionListAsset && actionListAsset == useAsset)
        {
            // actionList.GetParameter (0).SetValue (...);
            clickedItem = null;
        }
    }
    
  • Thanks so much for pointing me in the right direction. You're right. It ultimately wasn't necessary to access the runtime ActionList instance and I was able to achieve my goal by simply manipulating the original ActionList asset when the player clicks an inventory item.

    Great response and exactly what I needed.
    -Mike

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.