question

Grant McNamara avatar image
Grant McNamara asked

How do i get the player inventory from javascript?

I have spent hours trying to get data from my players inventory but i cant seen to figure out how to get it to work right. all it does is say undefined

                  function inv()          {            // save these values locally to ease use            // save title id            // build http request object for LoginWithCustomId                   var GetUserInventoryRequest = {};            OutputStatus("Logging in...");            PlayFabClientSDK.GetUserInventory(GetUserInventoryRequest, (response, error) => {                if(error)                {                  OutputError(error);                }                 else                {                  // display account details                  var result = response.data;                  var inv = result.Inventory                  var status = inv.ItemId;                  OutputStatus(status);                }            });          }; 
Player DatadataPlayer Inventory
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Made Wang avatar image
Made Wang answered

The callback value Inventory of the GetUserInventory method is an array, even if there is only one data inside. I modified your code, you can use the following code and refer to the api document for testing.

function inv()
{
    // save these values locally to ease use
    // save title id            
    // build http request object for LoginWithCustomId                   
    var GetUserInventoryRequest = {};            
    OutputStatus("Logging in...");            
    PlayFabClientSDK.GetUserInventory(GetUserInventoryRequest,
        (response, error) => 
        {                
            if(error)                
            {                  
                OutputError(error);
            }                 
            else                
            {                  

                // display account details                  
                //var result = response.data;                  
                //var inv = result.Inventory;                 
                //var status = inv.ItemId;                  
                //OutputStatus(status); 
                

                var result = response.data;             
                var inv = result.Inventory;
                for(var i=0;i<inv.length;i++)
                {
                    var status=inv[i].ItemId;
                    OutputStatus(status);
                }

            }            
        });          
}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.