question

terapico avatar image
terapico asked

UpdateUserInternalData: key doesn't exist after calling this

When I call the functions below, in succession, the server calls are successful (I can see that in my jsonResponse variables), but the GetUserInternalData function returns:

"result":{"PlayFabId":"","DataVersion":9,"Data":{}}

  1. Data is always empty.

  2. DataVersion increments each time I call postInternal.

  3. If I enter myKey into the playfab admin web interface for the same user account, getInternal will return it.

  4. Also, if I enter myKey into the web interface, if I call postInternal, myKey then disappears from the web interface, which may mean that it is deleted OR that it becomes private.

  5. -

How do I get getInternal to work with data posted by postInternal?

 function postInternal() {
     data[myKey] = {
         "r1" : "1",
         "r2" : "2"
     };
    
     var postVar = {
         PlayFabId: currentPlayerId,
         Data: data
     };
     var result = server.UpdateUserInternalData(postVar);
     ...
     return jsonResponse;
 }

 function getInternal() {
     var postVar_get = {
         PlayFabId: currentPlayerId,
         Keys: [myKey]
     };
     var result = server.GetUserInternalData(postVar_get);
     ...
     return jsonResponse;
 }
Player DataCloudScript
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.

terapico avatar image terapico commented ·

In the original statement, some text was removed upon submission as html. The GetUserInternalData function returns (insert actual master player id for ABC123): "result":{"PlayFabId":"ABC123","DataVersion":9,"Data":{}}

0 Likes 0 ·
terapico avatar image
terapico answered

THIS IS THE ANSWER

  1. if you put what you think is a variable name into the json as a key, it will become a string. See code 1 problem and code 1 fix.

  2. if playerData = server.GetUserInternalData, then

    a. playerData.Data["keyName"] = {"Value":"the_stored_value","LastUpdated":"2023-05-04T23:30:48.358Z","Permission":"Private"} (your values such as the time/date string will vary)

    b. and the value of a key is playerData.Data["keyName"].Value.

  3. Saving complex objects in a key does not work, so it seems like these internal data keys can only hold a single string value - not a json object (unless the json object is stringified). See code 3

Code 1 problem:

     var myKey = "testKey";
     Data: { myKey: testVal_new }  // will become an object named Data, and Data will have a key called "myKey" (not "testKey").

Code 1 fix:

 var data = {};
 data[myKey] = testVal_new;
 server.UpdateUserInternalData({
     PlayFabId: currentPlayerId,
     Data: data
 });

Code 3:

 var data = {};
 data[myKey] = { val1: testVal_new, val2:testVal_new+1};  // does not work: has to be simple string, not object
 //  also does not work: data[myKey] = { "val1": testVal_new, "val2":testVal_new+1};
 server.UpdateUserInternalData({
     PlayFabId: currentPlayerId,
     Data: data
 });
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.

terapico avatar image terapico commented ·

This is really the answer (multi faceted). But I can't mark my own answer as the answer.

0 Likes 0 ·
Gosen Gao avatar image
Gosen Gao answered

To call Player Data Management - Update User Internal Data - REST API (PlayFab Server) | Microsoft Learn, the value for a key must be a string. PlayFab CloudScript(Legacy) has a built-in sample function called processPlayerMove, which shows you how to call this API, you can have a look.

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.

terapico avatar image terapico commented ·

Thank you. I missed posting that detail, but it is a string in my code. I have at the top of the cloudscript:

 var myKey = "myActualKeyString";
0 Likes 0 ·
Gosen Gao avatar image Gosen Gao terapico commented ·

Sorry for the confusion, the value means key \ value pair's value have to be a string.

0 Likes 0 ·
terapico avatar image terapico commented ·

From the processPlayerMove function, it looks like:

 server.UpdateUserInternalData({
     PlayFabId: currentPlayerId,
     Data: {
         last_move_timestamp: new Date(now).toUTCString(),
         last_move: JSON.stringify(playerMove)
     }
 });

and

 var playerData = server.GetUserInternalData({
     PlayFabId: currentPlayerId,
     Keys: ["last_move_timestamp"]
 });

So looking at last_move_timestamp, the getter is a literal string, but the updater is an object key without quotes on it?

I will try this next chance I get.

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.