question

Cricket_FeVR avatar image
Cricket_FeVR asked

Error while using UpdateUserInventoryItemCustomData

I need to update user inventory item custom data through cloud script from the client 
side. The function parameter are the item id and checks with the item id in the user 
inventory if it matches update the current instance with the particular custom data. 
While i using this script it's always going to the else part and it givesthe error i 
attached with the image.




handlers.UpdateUserInventoryItemCustomData = function (args, context) {


 var inventory = server.GetUserInventory({ PlayFabId: currentPlayerId });
	for (var i=0; i<17; i++)
	{
		var itemInstance = inventory.Inventory[i].ItemInstanceId;
		var itemid = inventory.Inventory[i].ItemId;
		if(args.p1 == itemid)
		{
		var UpdateSquadRequest = server.UpdateUserInventoryItemCustomData({
			PlayFabId: currentPlayerId,
              		ItemInstanceId : itemInstance,
			Data : {
				Playing : "1",
                  		Order : "1"
			}
                });
		}
		else
		{
              	var UpdateSquadRequest = server.UpdateUserInventoryItemCustomData({
			PlayFabId: currentPlayerId,
			ItemInstanceId : itemInstance,
			Data : {
				Playing : "0"
			}
              });
		}
	}
    


    server.UpdateUserInventoryItemCustomData(UpdateSquadRequest);
}
unity3d
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

·
v-humcin avatar image
v-humcin answered

The error seems to be caused by trying to access an undefined object. From what I see it is likely caused by trying to access 17 items in the inventory, when there might be less items in the inventory.

It would be better practice to have the test expression of the for loop be based on actual length of the list, rather than a constant number, as in this example.

handlers.testInv = function (args, context){
  
      var inventory = server.GetUserInventory({ PlayFabId: currentPlayerId });
      var length = inventory.Inventory.length; 
      var count = 0;


      for(var i = 0; i < length; i++)
      {	
	    if (inventory.Inventory[i].ItemId == "Powerup0")
              count++;
      }
      return count;
}
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.