> For the complete documentation index, see [llms.txt](https://espergames.gitbook.io/inventool/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://espergames.gitbook.io/inventool/scripting/inventory.md). Agents can also search and read this documentation through its [MCP server](https://espergames.gitbook.io/inventool/~gitbook/mcp).

# Inventory

A class that handles all grid-based logic and stores items and currencies.

## Notes

* All properties visible in the component inspector can be accessed through code.

## Inventory Error

An `Inventory.Error` is an enum that helps identify the reason as to why an inventory action may have failed. `Inventory.Error.None` means the action was performed successfully.

## Reloading

Inventory reloads occur automatically when necessary. However, you can manually reload the inventory with the `ReloadPage` method.

```csharp
myInventory.ReloadPage();
```

While `ReloadPage` only reloads the current page, you have the option of reloading the entire inventory UI structure with the `Refresh` method. This is more costly.

```csharp
myInventory.Refresh();
```

## Size & Pagination

The grid size and the number of pages of the inventory can be edited at runtime.

```csharp
myInventory.GridSize = new Vector2Int(10, 10); // Set to 100 slots per page
myInventory.Pages = 10; // Set to 10 pages (note: cannot be higher than MaxPages)
```

You can set the current page with the `CurrentPage` property.

```csharp
myInventory.CurrentPage = 2; // Go to page 2
```

Alternatively, you can use `NextPage` to increment the page number by `1`.

```csharp
myInventory.NextPage();
```

Use `PreviousPage` for the opposite effect.

```csharp
myInventory.PreviousPage();
```

## Item Management

### Add Items

Use the `AddItem` method to add a specific `ItemStack` to the inventory. This method returns an [#inventory-error](#inventory-error "mention").

```csharp
myInventory.AddItem(myItemStack);
```

The `AddItems` method adds a list of specified items.

```csharp
Dictionary<ItemStack, Inventory.Error> failedToAdd = myInventory.RemoveItems(myItemStacks);
```

This method returns a `Dictionary<ItemStack, Inventory.Error>` which contains all items and the reason for why they were unable to be added to  the inventory. If the dictionary is empty, all items were successfully added.

The `SetItems` method sets the inventory's item list. This will completely remove all previous items.&#x20;

```csharp
Dictionary<ItemStack, Inventory.Error> failedToAdd = myInventory.SetItems(myItemStacks);
```

This method returns a `Dictionary<ItemStack, Inventory.Error>` which contains all items and the reason for why they were unable to be added to the inventory. If the dictionary is empty, all items were successfully added.

### Remove Items

Use the `RemoveItem` method to remove a specific `ItemStack` from the inventory. This method returns an [#inventory-error](#inventory-error "mention").

```csharp
myInventory.RemoveItem(myItemStack);
```

The `RemoveItems` method removes a list of specified items.

```csharp
Dictionary<ItemStack, Inventory.Error> failedToRemove = myInventory.RemoveItems(myItemStacks);
```

This method returns a `Dictionary<ItemStack, Inventory.Error>` which contains all items and the reason for why they were unable to be removed from the inventory. If the dictionary is empty, all items were successfully removed.

Alternatively, you can remove the stack amount of a specific item or item kind with `RemoveStackAmount` method. You can pass an `Item`, item ID, item name, or an `ItemStack` which represents the item that should be removed, followed by an integer amount which represents the amount of the item that should be removed. This method returns an [#inventory-error](#inventory-error "mention").

```csharp
myInventory.RemoveStackAmount("My Item", 5);
```

You also have the option of removing all items from the inventory with the `Clear` method.

```csharp
myInventory.Clear();
```

### Drop

The `DropItem` method removes an `ItemStack` from the inventory and creates an `ItemDrop` instance of it in the game world. The item does not necessarily need to exist in the inventory.

```csharp
myInventory.DropItem(myItemStack);
```

You can specify where the item should drop by passing a `Vector3` world position. If no position is specified, the item will spawn near the position of the inventory.

This method returns an [#inventory-error](#inventory-error "mention").

### Pickup

The `Pickup` method adds an item to the inventory. This requires a reference to an `ItemDrop` or `List<ItemDrop>`. The `ItemDrop` instances are removed from the game world.

```csharp
Inventory.Error pickupError = myInventory.Pickup(myItemDrop);
```

This method returns an [#inventory-error](#inventory-error "mention").

Alternatively, you can use `PickupAllInRange` to pick up all items in range of the player. This requires `Inventool.playerTransform` to be  set.

```csharp
myInventory.PickupAllInRange();
```

### Getting Items

You can retreive the first item of a specific kind by its unlocalized name or item ID with the `GetItem` method.

```csharp
ItemStack myItem = myInventory.GetItem("My Item Name");
```

Use `GetItems` instead to get all items of the same kind.

```csharp
List<ItemStack> myItem = myInventory.GetItems("My Item Name");
```

Alternatively, you can use `GetItemByGuid` to get a specific **item instance**.

```csharp
ItemStack myItem = myInventory.GetItemByGUID(myStoredGuid);
```

This method is useful when multiple items share the same name. For example, if an inventory contains two items named "Sword", `GetItem` will return the first matching item, `GetItems` will return all matching items, and `GetItemByGuid` will return the specific item whose unique GUID matches the one provided.

### Containment Checking

You can check if an item exists in the inventory with the `Contains` method by passing an `ItemStack`, `Item`, item ID (`int`), or item name (`string`). The second parameter is the number of the item that should exist in the inventory for `Contains` to return `true`.

```csharp
if (myInventory.Contains("My Item", 2)) // If the total count of "My Item" is at least 2
{
    // Do something...
}
```

You can get the total count of an item of a specific kind with the `GetItemCount` method by passing the item name, ID, or an [Item](https://espergames.gitbook.io/inventool/scripting/items/item.md) object.

```csharp
int count = myInventory.GetItemCount("My Item");
```

### Rewards

{% hint style="info" %}
A reward item is an item with a loot box or recipe reference set in the [Items](https://espergames.gitbook.io/inventool/editors/object-manager/items.md#type) section.
{% endhint %}

You can use the `ReceiveRewards` method to receive the rewards from a reward `ItemStack`. The entire stack of the item will be consumed completely, providing rewards for each instance.

```csharp
myInventory.ReceiveRewards(myRewardItem);
```

## Currency Management

See [Currencies](https://espergames.gitbook.io/inventool/scripting/currencies.md) to get a better understanding of how to script with them.

You can get a list of all currencies supported by the inventory with the `Currencies` property. If a specific currency is not in that list, it means it's **not supported**, preventing the ability to store any amount of it.

To add or remove supported currencies, modify the `Currencies` list directly.

You can get a specific currency with the `GetCurrency` method by passing a `CurrencyIdentity`, `Currency`, currency ID (`int`), or currency name (`string`).

{% hint style="info" %}
`Currency` is a `struct`. Modifying this value will not change it in the inventory.
{% endhint %}

```csharp
Currency myCurrency = myInventory.GetCurrency("My Currency");
```

To add a specific amount of a currency, use the `TryEarn` method. This method will fail (return `false`) if the currency is not supported.

```csharp
myInventory.TryEarn(myCurrency);
```

You can use `SetCurrency` or `SetCurrencies` to override the previous currency amounts.

```csharp
myInventory.SetCurrency(myCurrency);
```

Use `TrySpend` to remove currency. This method returns `false` if the currency is unsupported or if there is insufficient currency available.

```csharp
myInventory.TrySpend(myCurrency);
```

To check if the inventory has enough currency without actually spending any, use `HasEnoughCurrency`.

```csharp
if (myInventory.HasEnoughCurrency(myCurrency))
{
    // Do something...
}
```

Use ResetCurrencies to set all currencies to `0`.

```csharp
myInventory.ResetCurrencies();
```

## Filtering

You can filter the displayed items by [Item Type](https://espergames.gitbook.io/inventool/scripting/items/item-type.md). Note that when a filter is set, some inventory functions, such as manual placement, are prevented.

The inventory can have multiple filters set at a time. Use the `AddFilter` method to add a single filter.

```csharp
myInventory.AddFilter(myItemType);
```

Use `RemoveFilter` for the opposite effect.

```csharp
myInventory.RemoveFilter(myItemType);
```

You can also filter by item name.

```csharp
myInventory.filterSettings.NameFilter = "My Filter String";
```

Use `ClearFilters` to remove all **item type filters**.

```csharp
myInventory.ClearFilters();
```

Use `ClearAllFilters` to remove both **item type and name filters**.

```csharp
myInventory.ClearAllFilters();
```

## Sub-Inventories

{% hint style="info" %}
Currency of sub-inventories is not considered in most cases. It's recommended to handle currency from the main (top-level) inventory only.
{% endhint %}

Most of the above functions apply to all sub-inventories when the method is called on the main inventory. For exmaple, if you add an item with the AddItem method, but the main inventory does not have enough room for it, it will be added to a sub-inventory if it has space.

You can access the full list of sub-inventories with the `SubInventory` property.

To get a specific inventory by its `GameObject` name, use the `GetInventory` method.

```csharp
Inventory mySubInventory = myInventory.GetInventory("My Sub Inventory");
```

## Saving

See [Inventory](https://espergames.gitbook.io/inventool/scripting/saving-and-loading/inventory.md).

## Events

| Name                | Type                           | Description                                                                                                                    |
| ------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ |
| onItemAdded         | UnityEvent\<ItemStack>         | A callback for when an item is added to the inventory. This accepts 1 argument: the added item (Item Stack).                   |
| onItemRemoved       | UnityEvent\<ItemStack>         | A callback for when an item is removed from the inventory. This accepts 1 argument: the removed item (Item Stack).             |
| onItemDropped       | UnityEvent\<ItemStack>         | A callback for when an item is dropped. This accepts 1 argument: the item dropped (ItemStack).                                 |
| onItemPickedUp      | UnityEvent\<ItemStack, string> | A callback for when an item is picked up. This accepts 2 arguments: the item picked up (ItemStack), the item drop ID (string). |
| onRecipeReceived    | UnityEvent\<Recipe>            | A callback for when a recipe is received. This accepts 1 argument: the received recipe.                                        |
| onCurrencyAdded     | UnityEvent\<Currency>          | A callback for when currency is added. This accepts 1 argument: the amount added (Currency).                                   |
| onCurrencyRemoved   | UnityEvent\<Currency>          | A callback for when currency is removed. This accepts 1 argument: the amount removed (Currency).                               |
| onChanged           | UnityEvent                     | A callback for when any item or currency is added or removed.                                                                  |
| onMaxWeightExceeded | UnityEvent                     | A callback for when the weight limit is exceeded.                                                                              |
| onMaxWeightExited   | UnityEvent                     | A callback for when the weight limit is exited.                                                                                |
| onPageReloaded      | UnityEvent                     | A callback for when the current page is reloaded.                                                                              |
