question

blazej avatar image
blazej asked

Help converting code from cloudscript

Hello everyone!

Like in the title, would it be possible to change the code so that all this happens in playerData and not in entityObject? (So that it creates key and value there?)

Thank you very much in advance for your help :)

function AddGifterToList(TID) {
    var target = {
        Id: TID,
        //Type:"title_player_account"
    }


var objectsResult= entity.GetObjects({Entity: target})
var object=objectsResult.Objects["gifts"].DataObject
object["FriendsWhoSendGift"].push({"":currentPlayerId})//add new element to an array,Gift is an KVP array


    var apiResult = entity.SetObjects({
        Entity: target,
        Objects: [
            {
                ObjectName: "gifts",
                DataObject: object
            }
        ]
    });
};




handlers.CreateObject=function(args,context)
{
    var targetPlayer = {
        Id: args.TargetId,
        //Type:"title_player_account"
    }




 var objectsResult= entity.GetObjects({Entity: targetPlayer})
//Determine if there is a corresponding Object in Objects, if not, create an Object
if(!objectsResult.Objects["gifts"]) { 
     var apiResult = entity.SetObjects({
        Entity: targetPlayer,
        Objects: [
            {
                ObjectName: "gifts",
                DataObject: {"FriendsWhoSendGift":[{"":currentPlayerId}]}
            }
        ]
    });
}
   else 
   {
       AddGifterToList(args.TargetId);
   }
}
Player DataCloudScript
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

·
Rick Chen avatar image
Rick Chen answered

You can use the UpdateUserData API in your CloudScript to update the user data. Here is an example:

server.UpdateUserData(
            {
                "PlayFabId": currentPlayerId,
                "Data": { "Key": “Value” }
            }
        );

This is a server API, therefore you can call it with the “server” predefined object. And it will take the PlayFabId, not EntityId.

3 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.

blazej avatar image blazej commented ·

What if I want to add more values just like below?

var objectsResult= entity.GetObjects({Entity: target})var object=objectsResult.Objects["gifts"].DataObjectobject["FriendsWhoSendGift"].push({"":currentPlayerId})//add new element to an array,Gift is an KVP array

I mean doing something like this:

"FriendsWhoSendGift": playerID, playerID, playerID

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ blazej commented ·

I am not sure if I understand you. But you can add more Key/Value pairs in the "Data" object to update multiple key/value pairs in one call. For example,

server.UpdateUserData(
            {
                "PlayFabId": currentPlayerId,
                "Data": { "Key1": “Value1”,
"Key2": “Value2”,

"Key3": “Value3”

 }
            }
        );
0 Likes 0 ·
blazej avatar image blazej Rick Chen ♦ commented ·

I guess that's not what I meant. It's about adding a value to an existing key, for example here is the JSON of this key:

{
  "friendId1": XXXXXXXX,
  "friendId2": XXXXXXXX,
  "friendId3": XXXXXXXX,
  "friendId4": XXXXXXXX,
  "friendId5": XXXXXXXX,
  "friendId6": XXXXXXXX,
  "friendId7": XXXXXXXX,
  "friendId8": XXXXXXXX,
}

And I mean adding another "friendId", value to the existing key, so that the other values will stay. Could you please give an example of how I could do this? Thank you in advance :)

0 Likes 0 ·

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.