Get inventory of a specific player?

Hey all, trying to get a server based multiplayer game working and I want to be able to grab the inventory of any given player at a time. How do I do this with the economy code? Or is this something I should be doing with cloud code?

The closest thing I can figure out is this code in the docs:

GetInventoryOptions options = new GetInventoryOptions
{
    ItemsPerFetch = 5
};
GetInventoryResult inventoryResult = await EconomyService.Instance.PlayerInventory.GetInventoryAsync(options);

is this where I would set the player ID that I want to get the inventory item of?

I can see that cloud code might be the better way to do this, but I’m having a bit of trouble figuring out the cloud code for it. The examples in the docs (and the ones you can open in unity) are… complex at best.

There is this code from the docs for cloud code:

const { InventoryApi } = require("@unity-services/economy-2.3");
module.exports = async ({params, context, logger}) => {
const { projectId, playerId } = context;
const inventory = new InventoryApi(context);
const result = await inventory.getPlayerInventory({ projectId, playerId });
return result.data;
}

But I am having some trouble with the Unity side and figuring out how I would send the playerID properly. This looks like the logically best way to go for it, but I don’t know if anyone has any better ideas. Or how to properly call cloud code like this.

Ok I think I got a basic part of the cloud code to work, going to post on cloud code section to see if I can get some help!

1 Like

Hi @Corva-Nocta

You were nearly there. Your code will retrieve the inventory for the current player. If you want to retrieve the inventory of another player in a multiplayer game, you will need to pass in the playerId of the other player. You can retrieve this from the params object.

e.g.

/*
*  -------- GetOtherPlayersInventory------------------------------
*  Load another player's inventory data from Economy
* ----------------------------------------------------------------
*/

const { InventoryApi } = require("@unity-services/economy-2.4");
module.exports = async ({ params, context, logger }) => {
 
  const {projectId} = context
  const {otherPlayerId} = params
 
  // otherPlayerId input parmameter present?
  logger.info("Script parameters: " + otherPlayerId);
  const inventory = new InventoryApi(context);
  logger.info("Authenticated within the following context: " + JSON.stringify(context));
 
  // Attempt to get other player's data from Economy
  const result = await inventory.getPlayerInventory({ projectId, playerId : otherPlayerId });
 
  logger.info(JSON.stringify(result.data));

  // Return the JSON result to the client
  return {
    otherPlayerId  : otherPlayerId,
    inventoryItems : result.data.results
  };
};

1 Like

Ah excellent! Appreciate the code help!

out of curiosity, and this is a long shot, is there a way to get the info from other projects? I see that there is a projectId, could I theoretically use a different projectId to grab info from other projects?

What I have right now is a server and client multiplayer, but they are completely separate projects. Just wondering if its possible to have all of the stored data on the server project, but the client project could still access some of it. Though I am wondering if that is a good idea security wise, so I’m not married to the idea. I don’t really need it, just more curious if it could work that way.

Is there a way to actually modify other player’s data? I want to run the cloud code on the server side of the game and have the server work with all the information. Is there a way to make this work? If I try doing other functions besides getPlayerInventory (like updateInventoryItem, or addInventoryItem) It says I am not permitted to access the other player’s data. Is there a way to be permitted?

Edit: Found the answer, actually needed to set the Economy from 2.3 to 2.4 and it is working!

Hey I have a question how can I use the unity economy to find a very specific player inventory item that the player only has one of so I can run some code if he does indeed have that inventory item, in c#

Hello, If I access the player’s economy inventory, I can access all the items they have available to purchase. that is, all virtual purchases