question

joshsagarold avatar image
joshsagarold Deactivated asked

Cloudscript update multiple statistics

Hi, I'm wanting to update x amount of statistics via unity and cloudscript.

Forgetting the callbacks, pseudo-code, this doesn't work. I want to be able to update any number of statistics in one call (within the limit) without knowing which statistics prior.

public void UpdatePlayerStatistics(string[] names, int[] values)
    {
        List<StatisticUpdate> myUpdate = new List<StatisticUpdate>
        {
            new StatisticUpdate
            {
                StatisticName=names[0], Value = value[0]
            },
            new StatisticUpdate
            {
                StatisticName=names[1], Value = value[1]
            }
        };


        ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
        {
            FunctionName = "UpdateStatisticData",
            FunctionParameter = new
            {
                data = myUpdate
            }
        };
}
handlers.UpdateStatisticData = function(args) {
    try {
        
        var dataPayload = args.data;
 
        var UpdatePlayerStatisticsRequest = {
            "PlayFabId": currentPlayerId,
            "Data": dataPayload
        }
        
        var result = server.UpdatePlayerStatistics( UpdatePlayerStatisticsRequest );
    } catch(e) {
        var retObj = {};
        retObj["errorDetails"] = "Error: " + e;
        log.info(e);
        return retObj;
    }
}

Thank you

CloudScriptLeaderboards and Statistics
10 |1200

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

eswitzer07 avatar image
eswitzer07 answered

We built some custom cloud script for this on our project. You can pass in a basic map / dictionary of key values (as delta change, ie "Kills:5" - this indicates the player got 5 kills in the match, and we want to add 5 to their kill statistic). This can also be done with cloudscript rules + triggering a statistic increment.This snippet handles getting the original stats (this script is for character level statistics), and then applying the delta to save as the new value:

handlers.AddDeltaStats = function (args, context) {
    // Description:
    //          Method for setting the stats of a character
    // Arguments:
    //          See Below. Format as {"Kills":23}
 
    var GetStatistic = function (statObj, statName) {
        for (key in statObj)
        {
            if (key == statName){
                return statObj[key] || null;                
            }
        } 
        // In the event that the statistics array provided didn't have what we were looking for
        // we return null as a last ditch effort
        return null;
    };
    
    var CharacterID = args.CharacterID
    var PlayFabID = currentPlayerId
	    
    // Get our old character stat values and store them as OldData


    var OldDataRequest = {PlayFabId:PlayFabID, CharacterId:CharacterID}
    var OldData = server.GetCharacterStatistics(OldDataRequest)
    //log.debug(OldData)


    var NewRequest = {
        PlayFabId: PlayFabID,
        CharacterId : CharacterID,          
        CharacterStatistics: { }
    };
    
    for (item in args)
    {
        if (item != "PlayFabID" && item != "CharacterID") // Filter out arguments not to add to stats
        {
            NewRequest.CharacterStatistics[item] = Number(args[item]) + Number(GetStatistic(OldData.CharacterStatistics,item) || 0);
        }    
    }


    server.UpdateCharacterStatistics(NewRequest);
    //return{NewStatistics: updatedStats}


};
10 |1200

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

Marcus Nixon avatar image
Marcus Nixon answered

Hi Josh,

Are you calling the PlayFabClientAPI.ExecuteCloudScript API call? I see that you are creating the ExecuteCloudScriptRequest, but this request needs to be passed to ExecuteCloudScript in order to execute cloud script functions.

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

joshsagarold avatar image joshsagarold commented ·

Yes, that's not my issue. The issue is passing multiple statistics into the cloudscript and updating them

0 Likes 0 ·
Marcus Nixon avatar image Marcus Nixon joshsagarold commented ·

In your CloudScript function, try using this request:

server.UpdatePlayerStatistics({
	PlayFabId : currentPlayerId,
        Statistics : dataPayload
});

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.