question

Valentin D avatar image
Valentin D asked

Read item custom data with cloudscript

Hello!

I'm trying to use a Cloudscript function to save my inventory and update the character read only data, but when I can't access item's custom data to add item stats to the character.

function SaveInventory(args) {
    args.CurrentPlayerData = !args.CurrentPlayerData ? {} : args.CurrentPlayerData;
    var characterData = args.CurrentPlayerData.characterData;
    var item = characterData.Head;
    //var itemID = GetItemByID(item);
    var itemCustomData = JSON.parse(item.CustomData);
    var head = itemCustomData.damage;
    characterData.Defense += head;


    // API params
    var updateDataRequest = {
        PlayFabId: currentPlayerId,
        CharacterId: args.CurrentPlayerData.CharacterId,
        Data: { CharacterData: JSON.stringify(characterData) },
        Permission: "Public"
    };
    server.UpdateCharacterReadOnlyData(updateDataRequest);
}

In characterData I have a string containing the itemID which is the same as the on in Playfab, so I guess I can skip the GetItemByID line. The script fails at the JSON.parse, any ideas on why this fails?

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

·
Sarah Zhang avatar image
Sarah Zhang answered

The CustomData of the specific inventory item instance is the JSON object that needn’t be parsed. You can access the K/V pairs of it directly. It’s our test function, you can refer to it to modify the code.

handlers.GetInventoryData = function (args, context) {
    var GetInventoryResult = server.GetUserInventory(
        {
            PlayFabId: currentPlayerId
        }
    );
    var InvertoryItemData = GetInventoryResult.Inventory[0].CustomData;
    var damage = InvertoryItemData.damage;
    // return the value of "damage" field
    return damage;
}
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.