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
Answer by eswitzer07 · Jun 14, 2019 at 05:52 PM
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} };
Answer by Marcus Nixon · Jun 14, 2019 at 04:25 PM
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.
Yes, that's not my issue. The issue is passing multiple statistics into the cloudscript and updating them
In your CloudScript function, try using this request:
server.UpdatePlayerStatistics({ PlayFabId : currentPlayerId, Statistics : dataPayload });