question

entelicon avatar image
entelicon asked

CloudScript Data Problem

Hey everyone, I'm trying to create a data object in TypeScript using variable information. My code for this is:

handlers.VerifyComponent = function (args) {
    var activePreset = args.currentPreset;
    var componentName = args.componentName;
    var componentType = args.componentType;

    var userInventory = server.GetUserInventory({
        PlayFabId: currentPlayerId,
    });

    for (var i = 0; i < userInventory.Inventory.length; i++) {
        if (userInventory.Inventory[i].ItemId == componentName) {
            var characters = server.GetAllUsersCharacters({
                PlayFabId: currentPlayerId
            });
            for (var i = 0; i < characters.Characters.length; i++) {
                if (characters.Characters[i].CharacterName == activePreset) {
                    var character = characters.Characters[i];
                    server.UpdateCharacterReadOnlyData({
                        PlayFabId: currentPlayerId,
                        CharacterId: character.CharacterId,
                        Data: {
                            componentType: componentName
                        }
                    });
                }
            }
            return true;
        }
    }
    return false;
};


This however doesn't work, and I don't know how to check CloudScript errors. It I replace the data to something like "Hat":"WizardHat" it works fine. Not sure if it has to do with how variable data is handled. Any help would be great, thanks!
data
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

·
brendan avatar image
brendan answered

The problem is purely due to how JavaScript tries to be "helpful" with the JSON data. We have a write-up of how to use dynamic Keys in writing data in this post: https://community.playfab.com/questions/496/206963508-Writing-to-a-dynamic-Key-in-Cloud-Script.html.

5 comments
10 |1200

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

entelicon avatar image entelicon commented ·

Update: your method works, but I've tracked down the final problem being with me getting the correct character. I don't see anything wrong with my logic as it worked before. Any ideas?

Just for clarification, this is the code I'm talking about:

for (var i=0; i < characters.Characters.length; i++) {
                if (characters.Characters[i].CharacterName === activePreset) {


0 Likes 0 ·
brendan avatar image brendan entelicon commented ·

What Title ID and PlayFab ID are you testing, and what are you passing in for the arguments to the call?

0 Likes 0 ·
entelicon avatar image entelicon brendan commented ·

1588

414F207D0770A2BF

csRequest.FunctionParameter = new { componentName = "Engine2", activePreset = "Preset1", componentType = "Engine" };
0 Likes 0 ·
Show more comments
Show more comments

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.