Forum rules - please read before posting.

Add item to container / specific inventory box

Hi,

I have a custom inventory system with slots for wearable items. Item with property "head" goes to inventory box (container) "head_Slot", item with property "boots" goes to inventory box (container) with name "boots_Slot", etc.
I can add item to runtime inventory box but I don't know how to add item to specified inventory box.
Would be great if somebody could help me with below code:

public void PutAway(int itemID)
{
    InvItem invItem = KickStarter.inventoryManager.GetItem(itemID);
    string whereToPut = invItem.GetProperty(10).PopUpValue;
    string whereToPutID = whereToPut.Remove(0, 3);
    string suffix = "_Slot";
    string slotName = whereToPutID + suffix;


    MenuElement slot = PlayerMenus.GetElementWithName(suffix, slotName) as MenuInventoryBox;
    Debug.Log("Where to put: " + slot.title);

    slot.AddItem(invItem);
}

Comments

  • An InventoryBox element doesn't store items - only displays them from a separate source, such as a Container.

    If you've manually mapped a Container to such an element, you can read its OverrideContainer property to extract it:

    MenuInventoryBox inventoryBox = PlayerMenus.GetElementWithName(suffix, slotName) as MenuInventoryBox;
    Container container = inventoryBox.OverrideContainer;
    

    Otherwise, create a public Container variable in your script that you can assign in the Inspector.

    Once you have the Container, you can access its InvCollection class to modify its items:

    container.InvCollection.Add (new InvInstance (itemID, 1));
    
  • Thanks, you answer made me realise that in fact my idea to send item to container or store them in another InventoryBox doesn't make any sense. I've successfully emulated another inventory with categories (binID) instead and this works pretty well.
    Although I've encountered one problem with it - when I change item category at runtime - it stays changed even when I exit play mode. Probably there's a good reason behind it but is it possible to make these categories to come back to original states when exit runtime ?

  • Editing an item's category involves making changes to the Inventory Manager asset. I don't recommend making such changes - an item's category should remain fixed.

    An example of the use of item properties for this kind of situation can be found in the "Equipment system" package over on the Downloads page.

  • edited January 11

    This "Equipment system" is really interesting solution - could have saved me hours of development if I had discovered it sooner ;)
    Anyway, my approach is very similar, all this categories and properties are used in the same way. I failed at creating workaround for another inventory for equipped items. Initially I thought about using containers but then I remembered a passage from the manual:

    A Container is a scene-based list of Inventory items

    Now I'm going attach containers to player prefab as in "Equipment system" example, I hope this will not create problems with saving.

    Also I couldn't figured how this drag'n drop system is done.

  • Now I'm going attach containers to player prefab as in "Equipment system" example, I hope this will not create problems with saving.

    So long as your Player is persistent, i.e. spawned in at runtime, then Remember components attached to the prefab will be saved/loaded. Just be sure to check Retain in prefab? on them so that they have persistent ID values.

    Also I couldn't figured how this drag'n drop system is done.

    I'm not sure of your meaning. Drag-and-drop behaviour can be enabled in the Settings Manager - once checked, item selection/deselection behaviour should still be the same, only the inputs used to manage this.

  • I managed to build equipment system similar to the one in example available in downloads. In my case I use custom selection system - after clicking on item, SelectedItem menu is displayed with item details, bigger texture and item specific options, ex. equip, use etc. When I click equip item is being transferred to corresponding container.
    If I turn on Drag-and-drop behaviour then I cannot select item - it stays selected only as long as left mouse button is pressed.
    What I'd like to archive is dual mode system where player could select Item and press equip or drag item to equipment container. While the former works pretty well, the latter seems not possible in standard AC functionality.

    I use Direct movement, Context sensitive interaction method and AC ver. 1.77.3

  • it stays selected only as long as left mouse button is pressed.

    By design. An item's "selection", in AC terms, refers to an item under cursor control so that it can be used elsewhere.

    If I'm following your situation correctly, you'll want to separate this selection from the "item info" behaviour. Clicking an item wouldn't "select" it in terms of AC's understanding, but instead update your own script's InvInstance variable that is used to populate your SelectedItem menu manually.

  • edited January 13

    Yes! That's it! I was so focused on selecting items that I didn't noticed that I don't need to. Thanks for this out-of-the-box idea.

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.