question

sc avatar image
sc asked

Way to use variables to set KVP data in cloud script?

Wondering if I could get some help trying to write some more flexible cloud script. I have a bunch of title KVPs that at startup I’d like to populate to each player’s data. Version A where I plug everything in manually works.

But I get errors trying the less verbose version B where I try to skip the temp variables and plug title data directly into player KVP.

I have a lot of these so ideally I’d be able to loop through and generate them on the fly. But bunch of errors with version C. Thoughts on ways I could use variables to generate KPS without plugging them in manually one by one? Thank you!

// VERSION A
// works fine

var allTitleData = server.GetTitleData({
});
var tempData00 = allTitleData.Data[“titleData00"];
var tempData01 = allTitleData.Data[“titleData01”];
var tempData01 = allTitleData.Data[“titleData02”];	
  


var updateUserDataResult = server.UpdateUserReadOnlyData({
        PlayFabId: currentPlayerId,
        Data: {
          	playerData00: titleData00,
          	playerData01: titleData01,
		playerData01: titleData02
        }
    });



// VERSION B
// errors
var allTitleData = server.GetTitleData({
});


var updateUserDataResult = server.UpdateUserReadOnlyData({
        PlayFabId: currentPlayerId,
        Data: {
          	playerData00: allTitleData.Data[“titleData00"];
          	playerData01: allTitleData.Data[“titleData01”];
		playerData01: allTitleData.Data[“titleData02”];
        }
    });



// VERSION C
// errors
var allTitleData = server.GetTitleData({
});
var totalNumData = 10;


var updateUserDataResult = server.UpdateUserReadOnlyData({
        PlayFabId: currentPlayerId,
        Data: {
          		// loop through and set all of our startup decks
  			for(var i = 0; i < totalNumData; i++)
			{
    			var twoDigitNum = i.ToString("D2");
      			var playerDataName = “playerData” + twoDigitNum;
     			var titleDataName = “titleData” + twoDigitNum;
      			playerDataName: titleDataName
			}
        }
    });

CloudScript
1 comment
10 |1200

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

sc avatar image sc commented ·

hm-- can't see where i can edit my post. in version A should be playerData00: tempData00 etc not titleData00

0 Likes 0 ·
brendan avatar image
brendan answered

The post mentioned (and please use this link: https://community.playfab.com/questions/496/206963508-Writing-to-a-dynamic-Key-in-Cloud-Script.html) is indeed the one that shows specifically how to use a variable as your key. So modifying the code above to show using an argument for the key would look like this:

handlers.writeData = function (args) {


    var allTitleData = server.GetTitleData({
    });


    var dataPayload = {};
    dataPayload[args.playerData00] = allTitleData.Data["titleData00"];
    dataPayload[args.playerData01] = allTitleData.Data["titleData01"];
    dataPayload[args.playerData02] = allTitleData.Data["titleData02"];


    var updateUserDataResult = server.UpdateUserReadOnlyData({
        PlayFabId: currentPlayerId,
        Data: dataPayload
    });
}

That way, you would be able to pass in whatever string you need for playerData00, etc. for the Key names in the FunctionParameter object when calling ExecuteCloudScript.

1 comment
10 |1200

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

sc avatar image sc commented ·

Ah!! Perfect!! Thank you so much! Just what I needed. Really very much appreciate your help!

0 Likes 0 ·
brendan avatar image
brendan answered

I'm assuming that these are transcription typos, but tThe most immediate things that jump our are:

  • In versions A and B, you've got playerData01 in there twice.
  • In version B, you've got semicolons at the end of the key/value pair data lines in the data.
  • In both B and C, you've got invalid quote marks - notice how some are more curly? This is a common issue with editing on iOS devices, and one to watch out for.

So B actually will work with those corrections. Here's how it should look:

var allTitleData = server.GetTitleData({
});

var updateUserDataResult = server.UpdateUserReadOnlyData({
        PlayFabId: currentPlayerId,
        Data: {
          	playerData00: allTitleData.Data["titleData00"],
          	playerData01: allTitleData.Data["titleData01"],
		playerData02: allTitleData.Data["titleData02"]
        }
    });

Meanwhile C won't work because you can't have code logic inside the Data section of the API call. If you want to programatically build the data before submitting it, you should do it before the API call.

10 |1200

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

sc avatar image
sc answered

Okay thanks for your answer. I apologize for the syntax errors -- I find trying to code without an IDE very challenging as a relative beginner.

So a follow up question then about version C above.

What I need is a way to pass my KVP's into cloud script as variables. I believe it has something to do with passing in Data as a JSON formatted set of Key/Value pairs. You mention as much in this thread here: https://support.playfab.com/hc/en-us/community/posts/212101647-UpdateUserReadOnlyData-return-InvalidRequest-even-if-User-Data-has-update-successfully-

But I'm having a hard time figuring out to do this because I can't find any clear examples.

I'd like my client script to for instance to be able to send playerData00 and allTitleData.Data["titleData00"] and then for the cloud script to set playerData00 to equal allTitleData.Data["titleData00"]

A clear walkthrough on that would be super appreciated!

1 comment
10 |1200

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

Joshua Strunk avatar image Joshua Strunk commented ·

Here is an untested (may have typos) example of setting in cloudscript a key:value pair on the currentPlayers ReadOnlyData.

Edit1: Here is the Unity C# client api function call to this cloudscript again untested.

*If any one finds an error in one of tests these and finds errors just post a comment and will edit the fixes.

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.