Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by JoshSagarOld · Jun 14, 2019 at 01:14 PM · CloudScriptLeaderboards and Statistics

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

Comment

People who like this

0 Show 0
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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}


};
Comment

People who like this

0 Show 0 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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.

Comment

People who like this

0 Show 2 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoshSagarOld · Jun 14, 2019 at 10:01 PM 0
Share

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

avatar image Marcus Nixon JoshSagarOld · Jun 17, 2019 at 03:22 PM 0
Share

In your CloudScript function, try using this request:

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    1 Person is following this question.

    avatar image

    Related Questions

    best practice for saving statistics for leaderboards 1 Answer

    Leaderboard related Questions 1 Answer

    Can Cloud Code Access the Admin API 1 Answer

    CloudScript is not returning the right value of player statistics 1 Answer

    Cloudscript memory limits (Player profile feature) 0 Answers

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges