# Inventory

**URL:** https://heroiclabs.com/docs/hiro/cpp/inventory/
**Keywords:** inventory, hiro
**Categories:** hiro, cpp, inventory

---


# Inventory

Read more about the Inventory system in Hiro [here](../../concepts/inventory/).

## List items

List all inventory items defined in the codex, optionally filtered by category.

```cpp
void onInventoryList(const Hiro::InventoryList& inventoryList)
{
    for (auto it = inventoryList.items.begin(); it != inventoryList.items.end(); it++)
    {
        std::cout << "Found codex item: " << it->second.name << '\n';
    }
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryListRequest request;
request.itemCategory = "weapons";

hiroClient->inventoryList(session, request, onInventoryList, onError);
```

## List user inventory items

List all inventory items owned by the player, optionally filtered by category.

```cpp
void onInventoryListInventory(const Hiro::InventoryList& inventoryList)
{
    for (auto it = inventoryList.items.begin(); it != inventoryList.items.end(); it++)
    {
        std::cout << "Found inventory item: " << it->second.name << '\n';
    }
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryListRequest request;
request.itemCategory = "weapons";

hiroClient->inventoryListInventory(session, request, onInventoryListInventory, onError);
```

## Consume inventory items

Consume one or more inventory items owned by the player.

```cpp
void onInventoryConsume(const Hiro::InventoryConsumeRewards& inventoryConsumeRewards)
{
    std::cout << "Successfully consumed inventory items!\n";
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryConsumeRequest request;
request.items.insert(std::make_pair<std::string, std::string>("health_potion", "1"));

hiroClient->inventoryConsume(session, request, onInventoryConsume, onError);
```

## Grant inventory items

Grant one or more inventory items to the player.

```cpp
void onInventoryGrant(const Hiro::InventoryUpdateAck& inventoryUpdateAck)
{
    for (auto it = inventoryUpdateAck.inventory.items.begin(); it != inventoryUpdateAck.inventory.items.end(); it++)
    {
        std::cout << "Found inventory item: " << it->second.name << '\n';
    }
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryGrantRequest request;
request.items.insert(std::make_pair<std::string, std::string>("bronze_sword", "1"));

hiroClient->inventoryGrant(session, request, onInventoryGrant, onError);
```

### Remove inventory items

To remove items from a user's inventory, without consuming them, use a negative grant value.

```cpp
void onInventoryGrant(const Hiro::InventoryUpdateAck& inventoryUpdateAck)
{
    for (auto it = inventoryUpdateAck.inventory.items.begin(); it != inventoryUpdateAck.inventory.items.end(); it++)
    {
        std::cout << "Found inventory item: " << it->second.name << '\n';
    }
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryGrantRequest request;
request.items.insert(std::make_pair<std::string, std::string>("bronze_sword", "-1"));

hiroClient->inventoryGrant(session, request, onInventoryGrant, onError);
```

## Update inventory items

Update the properties on one or more inventory items owned by the player.

```cpp
void onInventoryUpdate(const Hiro::InventoryUpdateAck& inventoryUpdateAck)
{
    for (auto it = inventoryUpdateAck.inventory.items.begin(); it != inventoryUpdateAck.inventory.items.end(); it++)
    {
        std::cout << "Found inventory item: " << it->second.name << '\n';
    }
}

void onError(const Nakama::NError& error)
{
    std::cout << Nakama::toString(error.code) << ": " << error.message << '\n';
}

Hiro::InventoryUpdateItemsRequest request;

Hiro::InventoryUpdateItemProperties itemProperties;
itemProperties.stringProperties.insert(std::make_pair<std::string, std::string>("example_property", "example_value"));
itemProperties.numericProperties.insert(std::make_pair<std::string, double>("numeric_property", 10));

request.itemUpdates.insert(std::make_pair("bronze_sword", itemProperties));

hiroClient->inventoryUpdate(session, request, onInventoryUpdate, onError);
```