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()
{
// 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.
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.
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.
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.
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.OnElementRemoved += 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.
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);
}
Updated about 1 month ago