---
updatedAt: 2025-11-11T03:19:32.000Z
agentTools:
  projectIndex: https://beamable2.readme.io/llms.txt
---

# Inventory - Code

Store virtual items in player inventory [ECON-Inventory-03]

> 📘 Beamable SDK Examples
>
> • The following example code is available for download at <a target="_blank" href="https://www.Github.com/beamable/Beamable_SDK_Examples/">GitHub.com/Beamable\_SDK\_Examples</a>

### Code

Here are snippets from <a target="_blank" href="https://github.com/beamable/Beamable_SDK_Examples/tree/master/client/Assets/Examples/Runtime/Services/InventoryServiceExample/InventoryItemExample/Scripts/">`InventoryItemExample.cs`</a>;

#### Add Item

Give the active player a new **Inventory Item**.

```csharp C#
public async void AddOneItem()
{
  	// acquire a context
  	var ctx = await BeamContext.Default.Instance;
  
    // grant an item
    await ctx.Inventory.Update(builder => builder.AddItem("item.hat"));
}
```

#### Get Items

Get a list for the active player of all owned **Inventory Items**.

```csharp C#
public async void GetItems()
{
  	// acquire a context
  	var ctx = await BeamContext.Default.Instance;
  
  	// GetItems() allows a ItemRef to specify which type of items to get
    var items = ctx.Inventory.GetItems(); 
  
  	// wait for the items to be updated
    await items.Refresh();
    foreach (var item in items)
    {
      Debug.Log($"item id=[{item.ItemId}] type=[{item.ContentId}]");
    }
}
```

#### Delete Item

Remove from the active player an **Inventory Item**.

```csharp
public async void DeleteOneItem()
{
    var ctx = await BeamContext.Default.Instance;
    var items = ctx.Inventory.GetItems();
    await items.Refresh();

    var itemToDelete = items[0];
    await ctx.Inventory.Update(builder => builder.DeleteItem(itemToDelete.ContentId, itemToDelete.ItemId));
}
```

#### Delayed Updates

Every time the `Update` method is called, a network request will be sent to Beamable. In some circumstances this will produce too much network traffic. In the Unity SDK, the `UpdateDelayed` function can be used to batch requests made in quick succession.

```csharp
public async void Update()
{
	var ctx = await BeamContext.Default.Instance;

	ctx.Inventory.UpdateDelayed(b => b.CurrencyChange("currency.gems", 3));
	ctx.Inventory.UpdateDelayed(b => b.CurrencyChange("currency.coins", 3));
	ctx.Inventory.UpdateDelayed(b => b.CurrencyChange("currency.dollars", 3));

	await ctx.Inventory.WaitForDelayedUpdate();
}
```

#### Subscribe

Observe changes (ex. add/remove) for the active player of all owned **Inventory Items**.

```csharp C#
private async void ListenForInventory()
{ 
    var ctx = await BeamContext.Default.Instance;

    var items = ctx.Inventory.GetItems();
    await items.Refresh();

    foreach (var item in items)
    {
      item.OnUpdated += () =>
      {
        Debug.Log($"Item updated {item.ItemId}");
      };
    }

    items.OnUpdated += () =>
    {
      Debug.Log("Inventory updated");
    };

    items.OnElementsAdded += newItems =>
    {
      Debug.Log("Added items");
      foreach (var item in newItems)
      {
        item.OnUpdated += () =>
        {
          Debug.Log($"Item updated {item.ItemId}");
        };
      }
    };

    items.OnElementRemoved += removedItems =>
    {
      Debug.Log($"Removed {removedItems.Count()} items");
    };
}

```

#### Inventory Updates

The InventoryUpdateBuilder is a class used to modify client-writable items and currencies. It is used to stack multiple updates into one transaction, and applying all of the changes with a single call. It is useful for limiting the frequency or amount of API calls made to the server, especially in environments where changes are fast-paced, such as idle games.

```csharp
private async Task UpdateWithBuilder()
{
    //Create a new instance of an InventoryUpdateBuilder
    var updateBuilder = new InventoryUpdateBuilder();
    //Add some properties to our new item
    var properties = new Dictionary<string, string>
    {
        {"rarity", "common"},
        {"damage", "10"}
    };
    var contentId = "items.DefaultSword";
    //Add a new update to the builder, using the variables we declared earlier
    updateBuilder.AddItem(contentId, properties);

    var currencyId = "currency.Gold";
    var amount = 100;
    //Currency updates can be added as well
    updateBuilder.CurrencyChange(currencyId, amount);
    //Apply the changes, also sending the data to the server
    await _beamContext.Inventory.Update(updateBuilder);
}
```