question

walldiv avatar image
walldiv asked

CloudScript - Setting Values in ItemInstance.CustomData

Hey guys! I have some cloud script i'm trying to do to increment a variable in an ItemInstance.CustomData. I"ve read a GREAT deal on this topic, how to access and write to CustomData - which as i understand it is a DICT - but doing the following (from the below code example) isnt working.

EXAMPLE: JSON PARSE CODE

CODE:

  var MatchedItem;
  var MatchedPerkLevel;
  
  var dataPayload = {};
  var keyString = "CurrentUpgradeLevel";
  
  
  for (var i = 0; i < RetInv.Inventory.length; i++)  //iterate returned inventory, find itemids matching
  {
    if(RetInv.Inventory[i].ItemId == PerkID)
    {
      //RetVal = Convert.ToInt32(RetInv[i].Inventory.ItemId);
      MatchedItem = RetInv.Inventory[i];
      var ItemCustomData = MatchedItem.CustomData;
      var ItemCustomDataUnEncoded = JSON.parse(JSON.Stringify(ItemCustomData));
      MatchedPerkLevel = Number(ItemCustomDataUnEncoded.CurrentUpgradeLevel);
      MatchedPerkLevel += 1;
      break;
    }
  }
  
  dataPayload[keyString] = MatchedPerkLevel;
  
  return {PerkLevel: MatchedPerkLevel};
CloudScript
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

·
walldiv avatar image
walldiv answered

Solved this with some trial & Error - final code below:

var MatchedItem;
  var MatchedPerkLevel;
  
  var dataPayload = {};
  var keyString = "CurrentUpgradeLevel";
  
  
  for (var i = 0; i < RetInv.Inventory.length; i++)  //iterate returned inventory, find itemids matching
  {
    if(RetInv.Inventory[i].ItemId == PerkID)
    {
      //RetVal = Convert.ToInt32(RetInv[i].Inventory.ItemId);
      MatchedItem = RetInv.Inventory[i];
      if(MatchedItem.CustomData["CurrentUpgradeLevel"] < 3)
      {
       	MatchedPerkLevel = Number(MatchedItem.CustomData["CurrentUpgradeLevel"]) + 1;
        dataPayload[keyString] = MatchedPerkLevel;
  		var updaterequest = {Data:dataPayload, PlayFabId: PlayerID, CharacterId: CharacterID, ItemInstanceId: MatchedItem.ItemInstanceId };
  		var funct = server.UpdateUserInventoryItemCustomData(updaterequest);
      	break;
      }
    }
  }
  
  
  
  //log.info(ThisData);
  return {PerkLevel: MatchedPerkLevel};
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.