Inventory #

Read more about the Inventory system in Hiro here.

Initializing the inventory system #

The inventory system relies on the Nakama System and an ILogger, both must be passed in as dependencies via the constructor.

1
2
var inventorySystem = new InventorySystem(logger, nakamaSystem);
systems.Add(inventorySystem);

Subscribing to changes in the inventory system #

You can listen for changes in the inventory system so that you can respond appropriately, such as updating the UI, by implementing the IObserver pattern, or use the SystemObserver<T> type which handles it for you.

1
2
3
4
5
var disposer = SystemObserver<InventorySystem>.Create(inventorySystem, system => {
    Instance.Logger.Info($"System updated.");

    // Update UI elements etc as necessary here...
});

Refreshing the inventory system #

To ensure the inventory system has the latest information from Nakama you can refresh it.

1
await inventorySystem.RefreshAsync();

Listing the available items #

You can list the available items in the inventory system.

1
2
3
4
5
6
var items = await inventorySystem.GetItemCodexAsync();

foreach (var item in items)
{
    Debug.Log($"Item {item.Id}: {item.Name} - {item.Description}");
}

You can optionally retrieve a filtered list of items by category.

1
2
var category = "<category1>";
var items = await inventorySystem.GetItemCodexAsync(category)

Listing the user’s inventory #

You can list a user’s inventory.

1
2
3
4
foreach (var item in inventorySystem.Items)
{
    Debug.Log($"Item {item.Id}: {item.Name} - {item.Description}");
}

Granting inventory items #

You can grant inventory items to the user.

1
2
3
4
5
6
7
var items = new Dictionary<string, long>
{
    { "<itemId1>", 1 },
    { "<itemId2>", 10 }
};

await inventorySystem.GrantItemsAsync(items);

Consuming items #

You can consume items or item instances for a user that have the consumable flag. You can specify whether or not the user is allowed to overconsume (use more than they have).

Setting overconsume to true can be useful when handling multiple device scenarios. This allows consumption of the item, even if the local device’s resource data is out of sync. However, once the item reaches zero, further consumption is prevented, irrespective of the overconsume setting. This provides developers flexibility in managing inventory inconsistencies across devices.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var itemsToConsume = new Dictionary<string, long>
{
    {"<consumableId1>", 2},
    {"<consumableId2>", 1}
};
var instancesToConsume = new Dictionary<string, long>
{
    {"<instanceId1>", 1},
    {"<instanceId2>", 1}
};

var overconsume = false;

await inventorySystem.ConsumeItemsAsync(itemsToConsume, instancesToConsume, overconsume);

Updating item properties #

You can update the properties on an item instance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
var updates = new Dictionary<string, UpdateInventoryItemProperties>
{
    {
        "itemId1",
        new UpdateInventoryItemProperties
        (
            // String Properties
            new Dictionary<string, string>
            {
                { "crafted_by", "username" }
            },
            // Numeric Properties
            new Dictionary<string, double>
            {
                { "rank",  5 }
            }
        )
    }
};

await inventorySystem.UpdateItemsAsync(updates);