Skip to main content

Consuming Assets

Any AssetDefinition can be marked as consumable. These are special types of assets upon which a player can invoke the consumeAsset mutation. Upon consumption, the asset is removed from the player's inventory.

A canonical example of consumable assets involves invoking "external" effects that aren't tracked in Subroutine. For instance, a Healing Potion could be a consumable asset, adding a special effect of restoring hit points on the client side when the mutation successfully passes.

Another common example of consuming an asset could be "power-ups". In a game featuring mines or factories, a power-up could temporarily boost the speed at which resources are produced in those facilities. Such power-ups could easily be modeled in Subroutine using consumable assets.

In this section, we will create and grant a consumable asset to the player, and then have the player consume it from the client side.

Creating consumable assets

Creating consumable assets is identical to creating any other asset definition. This can be done directly in the Subroutine Dashboard under the Assets tab. When creating an asset, ensure to check the box labeled Consumable. For more detailed steps, follow the Asset Definitions guide

Next, we'll need to grant some of this asset to the user. Please note that this guide assumes you're using a test player. If you're not, you can create a test player in the Players tab.

You can grant assets to the test player directly from the Player's detail view in Players tab.

Player consuming the assets

We've now created a consumable asset definition and granted some holdings to our test player. Here's how to consume it:

Client.Player.GetInventory(new PlayerAPI.GetInventoryProps
{}, (response) =>
{
// We get all the user holdings, and we filter it down to the asset that can be unsealed
var potions = response.Holdings.First((asset) =>
{
// Or change to the name you chose for this asset.
return asset.node.assetDefinition.name == "Healing Potion";
}).node.instances.edges.First();

if (potions != null)
{
Debug.Log("We need healing! We are drinking one dose of healing potion");

Client.Player.ConsumeAsset(new ConsumeAssetProps
{
AssetId = potions.node.id,
Quantity = "1"
}, (response) =>
{
Debug.Log(response.id);
// Now you can invoke the healing effect on the client side, the potion has been consumed.
});
}
});