question

blazej avatar image
blazej asked

How can i get data from object and add another line in cloud script?

Hello All!

Like in the title, could you please fix my code so that I can add a line to the JSON of an existing object?

Thank you very much in advance for your help :)

handlers.makeEntityAPICall = function (args) {
 
// create an entity key, set another player's key.
    var target = {
        Id: args.TargetId,
        Type:"title_player_account"
    }


    var apiResult = entity.SetObjects({
        Entity: target,
        Objects: [
            {
                ObjectName: "gifts",
                DataObject: {
                    //I want the content of the object here, and below I want to add line to that
                    GifterID: currentPlayerId,
                }
            }
        ]
    });
    return {
        setResult: apiResult.SetResults[0].SetResult
    };
};
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

·
Xiao Zha avatar image
Xiao Zha answered

If I am not understand wrong , you want to add a new line to the object, you have to update the entire object. You can first call GetObject API in Could Script to get the object and modify one of the fields or add a new filed, then you can call SetObject API to update the entire object with the modified object. Here is the code example you can refer to:

handlers.SetObject=function(args,context)
{
    // create an entity key, set another player's key.
    var targetPlayer = {
        Id: args.TargetId,
        Type:"title_player_account"
    }
var objectsResult= entity.GetObjects({Entity: targetPlayer})
var object=objectsResult.Objects["gifts"].DataObject
//object[“Test”]+=”test” add new string to the original filed
//object[“Test0”]=”Test0” create a new filed 
 object["Gift"].push({"3":"Shield"})//add new element to an array,Gift is an KVP array
  var apiResult = entity.SetObjects({
        Entity: targetPlayer,
        Objects:  [
            {
                ObjectName: "gifts",
                DataObject: object
            }
        ]
    });
    return {
        setResult: apiResult.SetResults[0].SetResult
    }; 
}

Please let me know if I misunderstand.

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 ·

Yes! That's exactly what I meant, thank you, but I have one more question, would you be able to make an example where this function would also create an object if there is no one + a function that would delete the object / clear it to zero?

Thank you very much in advance! I am very weak at JS haha

0 Likes 0 ·
Xiao Zha avatar image Xiao Zha blazej commented ·

>> this function would also create an object if there is no one

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: {},
                DeleteObject:args.delete
            }
        ]
    });
}}

>> a function that would delete the object

handlers.DeleteObject=function(args, context)
{
     var targetPlayer = {
        Id: args.TargetId,
        Type:"title_player_account"
    }
//Set the DataObjet filed to null and the DeleteObject filed to true
      object=null 
    var apiResult = entity.SetObjects({
        Entity: targetPlayer,
        Objects:  [
            {
                ObjectName: "gifts",
                DataObject: object,
                DeleteObject:args.delete
            }
        ]
    });}
0 Likes 0 ·
blazej avatar image blazej Xiao Zha commented ·

Cool!! This is exactly what I needed. Thank you very much for your help, greetings nice and warm :)

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.