question

Rustam Mamedov avatar image
Rustam Mamedov asked

How to update player data in CloudCode correctly?

Hi!

I try to update some values in player data on login. When the player logs the first time I add some initial values to data. Then I want to add let's say some energy each new login. But if I update energy values all other values are gone.

handlers.InitNewPlayerData = function (playerData, gameConfig){
    var date = new Date();
    var now = date.getTime();
    
    playerData.Data.registerTimestamp = now;
    playerData.Data.softCurrency = gameConfig.accountConfig.startSoftCurrency;
    playerData.Data.energy = gameConfig.accountConfig.startEnergy;
    
    return playerData;
}


handlers.OnLogin = function (args, context) {
    var playerData = server.GetUserInternalData({
            PlayFabId: currentPlayerId
    });
    
    var titleData = server.GetTitleInternalData({});
    var gameConfig = JSON.parse(titleData.Data.config);
    
    if (!playerData.Data.hasOwnProperty("registerTimestamp")) {
        playerData = handlers.InitNewPlayerData(playerData, gameConfig);
        server.UpdateUserInternalData({
	        "PlayFabId":currentPlayerId,
	        "Data":playerData.Data
	    });
	// First time it stores correctly. I see registerTimestamp, energy and softCurrency in GameManager
    } else {
        playerData.Data.energy = parseInt(playerData.Data.energy.Value)+1;
        log.debug("saving playerData:  "+JSON.stringify(playerData.Data));
        
	// "Message": "saving playerData:  {\"energy\":21,\"registerTimestamp\":{\"Value\":\"1584385244061\",\"LastUpdated\":\"2020-03-16T19:00:44.081Z\",\"Permission\":\"Private\"},\"softCurrency\":{\"Value\":\"10\",\"LastUpdated\":\"2020-03-16T19:00:44.081Z\",\"Permission\":\"Private\"}}",


        server.UpdateUserInternalData({
    	    "PlayFabId":currentPlayerId,
    	    "Data":playerData.Data
    	});
    	    
    	playerData = server.GetUserInternalData({
            PlayFabId: currentPlayerId
        });
        log.debug("loading playerData:  "+JSON.stringify(playerData.Data));  
        
        //"Message": "loading playerData:  {\"energy\":{\"Value\":\"21\",\"LastUpdated\":\"2020-03-16T19:00:47.895Z\",\"Permission\":\"Private\"}}",      
		// I can see only energy value in GameManager. Where are "registerTimestamp" and "softCurrency"? 
    }
    
};

Thanks!

10 |1200

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

Sarah Zhang avatar image
Sarah Zhang answered

Generally, UpdateUserInternalData won’t do the replacement for a specific player’s internal data, it will only update the changed ones. The reason causes this issue should be the incorrect data format in the request body of the second Update API call. The request body of your API call should be this one.

 "Data": {
        "energy": 21,
        "registerTimestamp": {
            "Value": "1584385244061",
            "LastUpdated": "2020-03-16T19:00:44.081Z",
            "Permission": "Private"
        },
        "softCurrency": {
            "Value": "10",
            "LastUpdated": "2020-03-16T19:00:44.081Z",
            "Permission": "Private"
        }
    }

If the value of K/V pairs is the JSON Object, the data would have null-values and will be removed. API reference UpdateUserInternalData mentions it. In the above request body, the only correct K/V pair is “energy”:21. If you want to update a JSON as a value you need to convert it to a string via JSON.stringify. The correct format should be this one.

  "Data": {
        "energy": 21,
        "registerTimestamp": "{\"Value\":\"1584385244061\",\"LastUpdated\":\"2020-03-16T19:00:44.081Z\",\"Permission\":\"Private\"}",
        "softCurrency": "{\"Value\":\"10\",\"LastUpdated\":\"2020-03-16T19:00:44.081Z\",\"Permission\":\"Private\"}"
    }

But for your case, you can modify the code to update the “energy” K/V pair only.

var energyValue = parseInt(playerData.Data.energy.Value) + 1;
server.UpdateUserInternalData({
    "PlayFabId": currentPlayerId,
    "Data": {
        "energy": energyValue
    }
});
10 |1200

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

Rustam Mamedov avatar image
Rustam Mamedov answered

Thank you! It helped.

10 |1200

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

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.