What's the best way to search through a players inventory?

I've been running into some trouble with searching through a players inventory using CloudCode.

const playerInventory = await inventoryApi.getPlayerInventory({ projectId, playerId, inventoryItemIds: itemSearch });

This line seems like it should return all inventory items that contain the specific ID... but it appears that if any of those items are more than 20 items down the list, they won't be returned. I think when using the inventoryItemIds parameter, the pagnation limit shouldn't apply...

But since it does, how are you supposed to write this line to be able to search through the entire list of player inventory items for any containing the ID's? I see that you can adjust the limit up to a max of 100, which is fine for my use case for now... but what happens if the player has over 100 items? How would you iterate through the entire inventory to make sure you return all of the items being searched for?

Any insight would be much appreciated!

Hi Cameron,

When I have done this from C# client side code I have just iterated over the the results until I have them all, as shown in the SDK GetInventoryAsync method docs


// Player Inventory
GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(null);
List<PlayersInventoryItem> firstItems = inventoryResult.PlayersInventoryItems;

// do something with your items

while (inventoryResult.HasNext)
    inventoryResult = await inventoryResult.GetNextAsync();
    List<PlayersInventoryItem> nextItems = inventoryResult.PlayersInventoryItems;

    // do something with your items

I’ll need to check the implementation from Cloud Code for you though. I think you need to pass the playersInventoryItemId of the last item received in the after parameter of your next GetPlayerInventory call, to get the next page of results, until there are no results left.

check out these links

I’ll double check and get back to you

I've checked the Cloud Code implementation, it was as described in my previous post. You grab the playersInventoryItemIdof the last item received and pass it in the afterprameter of your next request, until there are no results left.


*  -------- Cloud Code Script ----------------
*  Get Player Inventory - iterate over pages
* --------------------------------------------
const { InventoryApi } = require("@unity-services/economy-2.4");

module.exports = async ({ params, context, logger }) => {

  const {
  } = context;

  const inventoryApi = new InventoryApi(context);

  // Default to pages of 20 inventory items if none specified as input params.pageSize
  const pageSize = params.pageSize != undefined ? params.pageSize : 20 ;
  let pages = 0 ;
  let inv = [];

  // get a page worth of inventory items
  let playerInventory = await inventoryApi.getPlayerInventory({projectId, playerId, limit:pageSize});
  // When we have results
  while( playerInventory.data.results.length > 0) {
    // Add them to the local inv array
    playerInventory.data.results.forEach( i => inv.push(i)); 
    // Grab the playersInventoryItemId of the last one
    lastPlayersInventoryItemId = playerInventory.data.results[playerInventory.data.results.length -1].playersInventoryItemId;
    // Then go looking for another page worth
    playerInventory = await inventoryApi.getPlayerInventory({projectId, playerId, limit: pageSize, after: lastPlayersInventoryItemId});
    pages ++;

  // Return the JSON result to the client
  return {
    inventory: inv,
    pages: pages

I hope that helps