Modding:Basic Inventory Handling: Difference between revisions

From Vintage Story Wiki
No edit summary
Line 41: Line 41:


If you need access to other inventories like e.g. the backpack inventories, you can use the <code>InventoryManager.Inventories</code> Property. This contains a dictionary with the name of the inventory (for example "hotbar-APlayerUID") and the corresponding inventory. Another cleaner looking way if you only need a single inventory would be the <code>InventoryManager.GetOwnInventory()</code> method. Also note that most of the string names are already in <code>GlobalConstants</code>.
If you need access to other inventories like e.g. the backpack inventories, you can use the <code>InventoryManager.Inventories</code> Property. This contains a dictionary with the name of the inventory (for example "hotbar-APlayerUID") and the corresponding inventory. Another cleaner looking way if you only need a single inventory would be the <code>InventoryManager.GetOwnInventory()</code> method. Also note that most of the string names are already in <code>GlobalConstants</code>.
A thing to remember is that the backpack inventory contains the bags as well as the slots, so you have to check if it is valid to put the item there, you can use the <code>ItemSlot.CanHold</code> method for example.  
 
Because backpack inventory contains the bags as well as the slots, you have to check if it is valid to put the item there. For this you can use the <code>ItemSlot.CanHold()</code> method.


If for example you want to fill every empty slot in the backpack with single torches you could do the following:
If for example you want to fill every empty slot in the backpack with single torches you could do the following:
47

edits