Search Unity

Resolved Get inventory of a specific player?

Discussion in 'Economy' started by Corva-Nocta, Feb 9, 2023.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    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:
    Code (csharp):
    1.  
    2. GetInventoryOptions options = new GetInventoryOptions
    3. {
    4.     ItemsPerFetch = 5
    5. };
    6. 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:
    Code (csharp):
    1.  
    2. const { InventoryApi } = require("@unity-services/economy-2.3");
    3. module.exports = async ({params, context, logger}) => {
    4. const { projectId, playerId } = context;
    5. const inventory = new InventoryApi(context);
    6. const result = await inventory.getPlayerInventory({ projectId, playerId });
    7. return result.data;
    8. }
    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.
     
  2. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    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!
     
    Laurie-Unity likes this.
  3. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    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.
    Code (CSharp):
    1. /*
    2. *  -------- GetOtherPlayersInventory------------------------------
    3. *  Load another player's inventory data from Economy
    4. * ----------------------------------------------------------------
    5. */
    6.  
    7. const { InventoryApi } = require("@unity-services/economy-2.4");
    8. module.exports = async ({ params, context, logger }) => {
    9.  
    10.   const {projectId} = context
    11.   const {otherPlayerId} = params
    12.  
    13.   // otherPlayerId input parmameter present?
    14.   logger.info("Script parameters: " + otherPlayerId);
    15.   const inventory = new InventoryApi(context);
    16.   logger.info("Authenticated within the following context: " + JSON.stringify(context));
    17.  
    18.   // Attempt to get other player's data from Economy
    19.   const result = await inventory.getPlayerInventory({ projectId, playerId : otherPlayerId });
    20.  
    21.   logger.info(JSON.stringify(result.data));
    22.  
    23.   // Return the JSON result to the client
    24.   return {
    25.     otherPlayerId  : otherPlayerId,
    26.     inventoryItems : result.data.results
    27.   };
    28. };
    upload_2023-2-15_11-42-4.png
     
    Corva-Nocta likes this.
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    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.
     
    Last edited: Feb 15, 2023
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    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!
     
  6. Mid_D_Man

    Mid_D_Man

    Joined:
    Jan 26, 2022
    Posts:
    22
    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#
     
    Last edited: Mar 14, 2023
  7. FabrizioRagaglia

    FabrizioRagaglia

    Joined:
    Apr 19, 2021
    Posts:
    4
    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