Inventory #

Read more about the Inventory system in Hiro here.

Functions #

List #

List the items defined as well as the computed item sets for the user by ID.

1
2
3
4
5
6
7
userId := "userId"
category := "weapons"

items, itemSets, err := systems.GetInventorySystem().List(ctx, logger, nk, userId, category)
if err != nil {
  return err
}

ListInventoryItems #

List the items which are part of a user’s inventory by ID.

1
2
3
4
5
6
7
userId := "userId"
category := "weapons"

inventory, err := systems.GetInventorySystem().ListInventoryItems(ctx, logger, nk, userId, category)
if err != nil {
  return err
}

ConsumeItems #

Deduct the item(s) from the user’s inventory and run the consume reward for each one, if defined.

1
2
3
4
5
6
7
8
9
userId := "userId"
itemIds := map[string]int64{"itemId_001": 2, "itemId_002": 5}
instanceIds := map[string]int64{"instanceId_001": 1, "instanceId_002": 6, "instanceId_003": 3}
overConsume := false

updatedInventory, rewards, instanceRewards, err := systems.GetInventorySystem().ConsumeItems(ctx, logger, nk, userId, itemIds, instanceIds, overConsume)
if err != nil {
  return err
}

GrantItems #

Add the item(s) to a user’s inventory by ID.

1
2
3
4
5
6
7
8
userId := "userId"
itemIds := map[string]int64{"itemId_001": 1, "itemId_002": 2}
ignoreLimits := false

updatedInventory, newItems, updatedItems, notGrantedItemIds, err := systems.GetInventorySystem().GrantItems(ctx, logger, nk, userId, itemIds, ignoreLimits)
if err != nil {
  return err
}

UpdateItems #

Update the properties which are stored on each item by instance ID for a user.

1
2
3
4
5
6
7
8
9
instanceIds := map[string]*hiro.InventoryUpdateItemProperties{"itemId_001": {
  StringProperties:  map[string]string{"crafted_by": "username"},
  NumericProperties: map[string]float64{"rank": 5},
}}

updatedInventory, err := systems.GetInventorySystem().UpdateItems(ctx, logger, nk, userId, instanceIds)
if err != nil {
  return err
}

Hooks #

SetOnConsumeReward #

Set a custom reward function which will run after an inventory item’s consume reward is rolled.

1
2
3
4
5
6
systems.GetInventorySystem().SetOnConsumeReward(OnConsumeReward)

func OnConsumeReward(ctx context.Context, logger runtime.Logger, nk runtime.NakamaModule, userID, sourceID string, source *hiro.InventoryConfigItem, rewardConfig *hiro.EconomyConfigReward, reward *hiro.Reward) (*hiro.Reward, error) {
	// Modify reward or take additional actions.
	return reward, nil
}