Interface IInventory
Interface for an Inventory managing IInventorySlots and the IItems inside. Exposes methods for adding, removing, and getting items.
Namespace: InventorySystem.Inventory
Assembly: InventorySystem.dll
Syntax
public interface IInventory
Properties
| Edit this page View SourceCapacity
Maximum amount of items that can be inside the IInventory.
Declaration
int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
InventorySlots
Get all IInventorySlots in this.
Declaration
IReadOnlyList<IInventorySlot> InventorySlots { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<IInventorySlot> | IReadOnlyList<T> of all IInventorySlots in this IInventory. |
Remarks
WARNING: Also returns empty IInventorySlots. Thus, always returns a list of size equal to Capacity.
IsEmpty
Whether the entire IInventory is empty meaning all IInventorySlots are empty.
Declaration
bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Items
Get all IItems in IInventory.
Declaration
IReadOnlyList<IItem> Items { get; }
Property Value
| Type | Description |
|---|---|
| IReadOnlyList<IItem> | IReadOnlyList<T> of all IItems in this IInventory. |
Remarks
| Edit this page View SourceMaxCapacity
Maximum capacity of this IInventory.
Declaration
int MaxCapacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
See Also
Methods
| Edit this page View SourceClear()
Clear the entire IInventory by clearing all IInventorySlots.
Declaration
void Clear()
Compact()
Makes the IInventory compact meaning that all empty gaps between IInventorySlots are filled with IInventorySlot after the gaps.
Declaration
void Compact()
Remarks
Remains the order of IInventorySlots meaning that the first IInventorySlot after a gap is also the first that fills out that slot with the slots filling out empty slots from left to right.
IsSlotAvailable(IItem, out int)
Whether an IItem item can be added into an IInventorySlot. Does not add the IItem.
Declaration
bool IsSlotAvailable(IItem item, out int addIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| IItem | item | IItem to be checked. |
| int | addIndex | First found index for the IItem item to be added into. Looks for index with equivalent, non-full IInventorySlot and empty slots second. Returns -1, if it cannot be added. |
Returns
| Type | Description |
|---|---|
| bool | Whether the IItem can be added or not. |
Remarks
Does not guarantee that the IItem can be added with a quantity greater than one. Only guarantees that there is either an empty IInventorySlot or a non-full IInventorySlot that is already occupied by the item that is to be added. It is still possible that the quantity of the IItem being added exceeds the MaxStackAmount and thus needs to occupy additional Slots to be fully added.
RemoveItem(IItem, bool, bool)
Remove IItem item from the IInventory. If the IItem is in the IInventory, we clear its IInventorySlot.
Declaration
void RemoveItem(IItem item, bool isRemovingFirstOccurenceOnly = false, bool isReversed = false)
Parameters
| Type | Name | Description |
|---|---|---|
| IItem | item | IItem to be removed. |
| bool | isRemovingFirstOccurenceOnly | Whether only the first occurence of the IItem in the IInventory gets removed (true) or all of its occurrences (i.e., we have the same IItem in multiple IInventorySlots). |
| bool | isReversed | Start to remove items from the back. Does not have side effects. |
Swap(int, int)
Swap the content of the IInventorySlot at indexA with the content of the IInventorySlot at indexB.
Declaration
void Swap(int indexA, int indexB)
Parameters
| Type | Name | Description |
|---|---|---|
| int | indexA | The index of slot A. |
| int | indexB | The index of slot B. |
TryAddItem(IItem, int)
Try to add an IItem into the first possible IInventorySlot.
Declaration
bool TryAddItem(IItem item, int quantity = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| IItem | item | The IItem to be added. |
| int | quantity | The number of IItem item to be added. |
Returns
| Type | Description |
|---|---|
| bool | Whether the IItem item was added. |
Remarks
Wrapper for TryAddItemAt(IItem, int, int). If the quantity exceeds the first found IInventorySlot capacity, it tries to find the next empty IInventorySlot and adds the leftover amount there. Loops recursively until all items are stored in the IInventory or until there is no more space available. If no more space is available, discards/ignores the remaining leftover. Returns true, even if any leftover is discarded.
TryAddItemAt(IItem, int, int)
Try to add an IItem item into the IInventorySlot at index.
Declaration
bool TryAddItemAt(IItem item, int index, int quantity = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| IItem | item | The IItem to be added. |
| int | index | The index of the IInventorySlot that the item is trying to be added into. |
| int | quantity | The number of IItem item to be added. |
Returns
| Type | Description |
|---|---|
| bool | Whether the IItem item was added. |
Remarks
If the quantity exceeds the Quantity of the IInventorySlot capacity, it tries to find the next non-full occupied IInventorySlot by the same IItem item or the next empty IInventorySlot and adds the leftover amount there. Loops recursively until all items are stored in the IInventory or until there is no more space available. If no more space is available, discards/ignores the remaining leftover. Returns true, even if any leftover is discarded. WARNING: If InventorySystem.Inventory.Inventory._handlesOverflow is true, it only tries to add the item to the first found IInventorySlot and returns true if any amount was added.
TryClearSlotAt(int)
Try to clear the IInventorySlot, if it's valid.
Declaration
void TryClearSlotAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | The index of the IInventorySlot to clear. |
TryGetItemAt(int)
Try to get IItem as index.
Declaration
IItem TryGetItemAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | The index to look for IItem item. |
Returns
| Type | Description |
|---|---|
| IItem | Returns IItem, if the index is inside the bounds of the Capacity. Otherwise, or if IInventorySlot IsEmpty, returns null. |
TryGetSlotAt(int)
Try to get the IInventorySlot at slotIndex.
Declaration
IInventorySlot TryGetSlotAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| int | index | Index to look for the IInventorySlot |
Returns
| Type | Description |
|---|---|
| IInventorySlot | IInventorySlot or null, if the index is higher than the Capacity. |
TryIncreaseCapacity(int)
Increases the Capacity of this up to MaxCapacity.
Declaration
bool TryIncreaseCapacity(int addedCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| int | addedCapacity | The amount of capacity to be added. |
Returns
| Type | Description |
|---|---|
| bool | Whether the Capacity was increased at all. Also returns true, if the Capacity was not increased by the full addedCapacity. |
Remarks
Discards negative numbers as this cannot decrease Capacity. If Capacity is already MaxCapacity or adding addedCapacity would increase Capacity to be higher than MaxCapacity Capacity gets set to MaxCapacity.
TryInsertItemAtFront(IItem, int)
Try to insert an IItem at the front of the IInventory, i.e., in IInventorySlot 0.
Declaration
bool TryInsertItemAtFront(IItem item, int quantity = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| IItem | item | The IItem to be added to the IInventory at the front. |
| int | quantity | The quantity of IItem to be added. |
Returns
| Type | Description |
|---|---|
| bool | Whether the IItem was added to the front. |
Remarks
Pushed all other IItems already in the IInventory one slot up. FIXME: If quantity exceeds MaxStackAmount, it is possible that IItems that were previously in the IInventory get removed.
Events
| Edit this page View SourceCapacityChanged
Event invoked when the Capacity of this IInventory is changed.
Declaration
event Action<int> CapacityChanged
Event Type
| Type | Description |
|---|---|
| Action<int> | Returns the new Capacity of this IInventory. |
See Also
| Edit this page View SourceItemsAdded
Event invoked when IItems were successfully added to an IInventorySlot in this IInventory.
Declaration
event Action<IInventorySlot, int> ItemsAdded
Event Type
| Type | Description |
|---|---|
| Action<IInventorySlot, int> | Returns the IInventorySlot the IItem was added to and the index of the IInventorySlot. |
Remarks
Triggered per slot that receives items; each invocation returns the added IItem and the quantity placed into that slot. (i.e., if more IItems are added than MaxStackAmount for one slot, and items are added to a second slot, the event will fire again for the second slot.)
ItemsRemoved
Event invoked when IItems were successfully removed from an IInventorySlot in this IInventory.
Declaration
event Action<IInventorySlot, int> ItemsRemoved
Event Type
| Type | Description |
|---|---|
| Action<IInventorySlot, int> | Returns the IInventorySlot the IItem was removed from and the index of the IInventorySlot. |
Remarks
Fired per slot that loses items; each invocation returns the removed IItem and the quantity taken from that slot.