Hi - I'm using the ContentChanged event to monitor for changes to the player inventory and then bridge those changes over to another inventory system. I have a ContentChangeHandler function which gets called on the event:

ORK.Game.ActiveGroup.Leader.Inventory.ContentChanged += ContentChangedHandler;

void ContentChangedHandler(Inventory inventory, int quantity, IInventoryShortcut shortcut, bool showNotification, bool showConsole)
{
Debug.Log(shortcut.GUID);
}
I'm noticing that the GUID field of the IInventoryShortcut is blank, making me think this maybe isn't an actual reference to the item in player inventory. Is there a way to use this IInventoryShortcut to get the ItemShortcut reference in the actual item in the player inventory? For example I tried this:


ORK.Game.ActiveGroup.Leader.Inventory.Items.Get(shortcut)


This however seems to return null and would return an IShortcut anyway, not an ItemShortcut which is what I'm looking for.

Thanks!
-Mike
  • The passed on shortcut here isn't guaranteed to be the same as in the inventory and have a valid/matching GUID. That depends on the inventory setup and state of the inventory, e.g. when auto stacking items, an already added (matching) item's quantity will just be increased, so the shortcut that was passed to the inventory (and forwarded to the event handler) isn't actually used in the inventory.

    Generally, you can cast any IShortcut instance (i.e. also IInventoryShortcut, which is just an extension of it) to the type you need in 2 ways:
    ItemShortcut item = inventoryShortcut as ItemShortcut;
    if(item != null)
    {
    // do stuff
    }

    Or:
    if(inventoryShortcut is ItemShortcut)
    {
    ItemShortcut item = (ItemShortcut)inventoryShortcut;
    // do stuff
    }
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • edited May 31
    This was helpful, thanks. For anyone that may happen upon this thread in the future here is some code that bridges two inventory systems. Inventory items from ORK are copied over at Start and then any subsequent changes are copied over as they happen. In this case I'm copied ORK inventory items over to Adventure Creator:
        private void OnEnable()
    {
    ORK.Game.ActiveGroup.Leader.Inventory.ContentChanged += ContentChangedHandler;
    }

    // Start is called before the first frame update
    void Start()
    {
    InitializeInventory();
    }

    void ContentChangedHandler(Inventory inventory, int quantity, IInventoryShortcut shortcut, bool showNotification, bool showConsole)
    {

    InvInstance tempInvInstance = KickStarter.runtimeInventory.PlayerInvCollection.GetFirstInstance(shortcut.GetName());

    if (tempInvInstance == null)
    {
    ItemShortcut foundItem = ORK.Game.ActiveGroup.Leader.Inventory.Items.Get(shortcut) as ItemShortcut;
    AddORKItemToAC(foundItem);
    }
    else
    {
    tempInvInstance.Count += quantity;
    }

    }

    public void InitializeInventory()
    {

    List<ItemShortcut> items = ORK.Game.ActiveGroup.Leader.Inventory.Items.GetAll();

    for (int i = 0; i < items.Count; i++)
    AddORKItemToAC(items[i]);

    List<CurrencyShortcut> currencies = ORK.Game.ActiveGroup.Leader.Inventory.Currencies.GetAll();

    for (int i = 0; i < currencies.Count; i++)
    AddORKItemToAC(currencies[i]);

    }

    private void AddORKItemToAC(IShortcut itemShortcut)
    {
    InvItem itemTemplate = KickStarter.inventoryManager.GetItem("ORK Item Template");
    InvItem invItem = new InvItem(itemTemplate);
    Debug.Log(itemShortcut.GetName() + " " + itemShortcut.ID);
    invItem.id = itemShortcut.ID;
    invItem.label = itemShortcut.GetName();
    invItem.tex = itemShortcut.GetIconTexture();
    invItem.canCarryMultiple = true;
    invItem.count = itemShortcut.Quantity;
    invItem.maxCount = 9999;
    InvInstance invInstance = new InvInstance(invItem, invItem.count);
    KickStarter.runtimeInventory.PlayerInvCollection.Add(invInstance);
    }
    Post edited by indoflaven on
  • @indoflaven thanks for sharing
Sign In or Register to comment.