Inventory - Code

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

📘

Beamable SDK Examples

The following example code is available for download at GitHub.com/Beamable_SDK_Examples

Code

Here are snippets from InventoryItemExample.cs;

Add Item

Give the active player a new Inventory Item.

public async void AddOneItem()
{
    // Give an item (To current player from items available in game)
    await _beamContext.Api.InventoryService.AddItem(_itemToAdd.Id, new Dictionary<string, string> 
    ()).Then(obj =>
    {
        Debug.Log($"#3. PLAYER - InventoryService, AddOneItem = {_itemToAdd.Id}");        
    });
}

Get Items

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

public async void GetItems()
{
    var allOwnedItems = await _beamContext.Api.InventoryService.GetCurrent();

    foreach (var inventoryItem in allOwnedItems.items)
    {
        foreach (var itemView in inventoryItem.Value)
        {
            Debug.Log("inventoryObject: " + itemView.id);
        }
    }
}

Delete Item

Remove from the active player an Inventory Item.

public async void DeleteOneItem()
{
    string contentId = _itemToDelete.Id;

    var allOwnedItems = await _beamContext.Api.InventoryService.GetCurrent();

    if (allOwnedItems.items == null || allOwnedItems.items.Count == 0)
    {
        Debug.Log($"#4. PLAYER DeleteOneItem() failed. Player has no items yet.");
        return;
    }
   
    long itemIdToDelete = 0;
    foreach (var item in allOwnedItems.items)
    {
        //Checks the CONTENT id
        if (item.Key == _itemToDelete.Id)
        {
            //Stores the INVENTORY id
            itemIdToDelete = item.Value[0].id;
            break;
        }
     }
         
     if (itemIdToDelete == 0)
     {
         Debug.Log($"#4. PLAYER DeleteOneItem() failed. Player has no items of that type yet.");
         return;
     }
         
     await _beamContext.Api.InventoryService.DeleteItem(contentId, itemIdToDelete).Then(obj =>
     {
         Debug.Log($"#4. PLAYER DeleteOneItem() success. 1 player item for {contentId} is deleted.");
     });
}

Subscribe

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

private async void SetupBeamable()
{ 
   
    _beamContext = BeamContext.Default;
    await _beamContext.OnReady;
            
    Debug.Log($"beamContext.PlayerId = {_beamContext.PlayerId}");

    Armor armorToAdd = await _itemToAdd.Resolve();
    _inventoryItemExampleData.ItemToAddName = armorToAdd.Name;

    Armor armorToDelete = await _itemToDelete.Resolve();
    _inventoryItemExampleData.ItemToDeleteName = armorToDelete.Name;

    // All items (Available in game)
    _beamContext.Api.ContentService.Subscribe(ContentService_OnChanged);

    // Filtered items (Owned by current player)
    _beamContext.Api.InventoryService.Subscribe(ContentType, InventoryService_OnChanged);

}

private void ContentService_OnChanged(ClientManifest clientManifest)
{
    //Debug.Log($"#1. GAME - ContentService, count = {clientManifest.entries.Count}");
         
    _inventoryItemExampleData.ContentObjectNames.Clear();
    foreach (ClientContentInfo clientContentInfo in clientManifest.entries)
    {
        string contentName = 
            ExampleProjectHelper.GetDisplayNameFromContentId(clientContentInfo.contentId);
        string contentItemName = $"{contentName} x 1";
        _inventoryItemExampleData.ContentObjectNames.Add(contentItemName);
    }

    _inventoryItemExampleData.IsChangedContentService = true;
         
    Refresh();
}

private void InventoryService_OnChanged(InventoryView inventoryView)
{
    //Debug.Log($"#2. PLAYER - InventoryService, count = {inventoryView.items.Count}");
    
    _inventoryItemExampleData.InventoryItemNames.Clear();
    foreach (KeyValuePair<string, List<ItemView>> kvp in inventoryView.items)
    {
        string inventoryItemName = $"{kvp.Key} x {kvp.Value.Count}";
        _inventoryItemExampleData.InventoryItemNames.Add(inventoryItemName);
    }
         
    _inventoryItemExampleData.IsChangedInventoryService = true;
         
    Refresh();
}

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.

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.Api.InventoryService.Update(updateBuilder);
}