question

saitmonato avatar image
saitmonato asked

Unity :public static void GetUserStatistics ( ) isnt showing anything to me in the console, what am i missing?,Trying to call user statistics but cant seem to: public static void GetUserStatistics ( ) doesnt show me anything

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using System;
 
public class PlayFabManager : MonoBehaviour
    {
 
    // this is a sorted, collated structure built from playerInventory. By default, this will only grab items that are in the primary catalog
    public static Dictionary<string, int> virtualCurrency;
    public static List<ItemInstance> playerInventory = new List<ItemInstance> ( );
    public static Dictionary<string, int> userStatistics = new Dictionary<string, int> ( );
    public string PlayFabId;
 
    //aggregation of player characters
    public static List<CharacterResult> playerCharacters = new List<CharacterResult> ( );
    public static Dictionary<string, List<string>> characterAchievements = new Dictionary<string, List<string>> ( );
    public static Dictionary<string, Dictionary<string, int>> characterStatistics = new Dictionary<string, Dictionary<string, int>> ( );
 
    void OnEnable ( )
        {
        Login ( "EFDE" );
 
        }
    IEnumerator Start ( )
        {
        yield return new WaitForSeconds ( 15 );
       // ClientGetTitleData ( );
       // GetUserData ( );     
     GetUserStatistics ( );
        }
      
 
    void Login ( string titleId )
        {
        LoginWithCustomIDRequest request = new LoginWithCustomIDRequest ( )
            {
            TitleId = titleId,
            CreateAccount = true,
            CustomId = SystemInfo.deviceUniqueIdentifier
            };
 
        PlayFabClientAPI.LoginWithCustomID ( request, ( result ) =>
        {
            PlayFabId = result.PlayFabId;
            Debug.Log ( "Got PlayFabID: " + PlayFabId );
 
            if ( result.NewlyCreated )
                {
                Debug.Log ( "(new account)" );
                }
            else
                {
                Debug.Log ( "(existing account)" );
                }
        },
        ( error ) =>
        {
            Debug.Log ( "Error logging in player with custom ID:" );
            Debug.Log ( error.ErrorMessage );
        } );
        }
    public void ClientGetTitleData ( )
        {
        var getRequest = new PlayFab.ClientModels.GetTitleDataRequest ( );
        PlayFabClientAPI.GetTitleData ( getRequest, ( result ) =>
        {
            Debug.Log ( "Got the following titleData:" );
            foreach ( var entry in result.Data )
                {
                Debug.Log ( entry.Key + ": " + entry.Value );
                }
        },
          ( error ) =>
          {
              Debug.Log ( "Got error getting titleData:" );
              Debug.Log ( error.ErrorMessage );
          } );
        }
    void GetUserData ( )
        {
        GetUserDataRequest request = new GetUserDataRequest ( )
            {
            PlayFabId = PlayFabId,
            Keys = null
            };
 
        PlayFabClientAPI.GetUserData ( request, ( result ) =>
        {
            Debug.Log ( "Got user data:" );
    
            if ( ( result.Data == null ) || ( result.Data.Count == 0 ) )
                {
                Debug.Log ( "No user data available" );
                }
            else
                {
                foreach ( var item in result.Data )
                    {
                    Debug.Log ( "    " + item.Key + " == " + item.Value.Value );
                    }
                }
        }, ( error ) =>
        {
            Debug.Log ( "Got error retrieving user data:" );
            Debug.Log ( error.ErrorMessage );
        } );
        }
 
    #region User Statistics
    public static void GetUserStatistics ( )
        {
        GetPlayerStatisticsRequest request = new GetPlayerStatisticsRequest ( );
        PlayFabClientAPI.GetPlayerStatistics ( request, OnGetUserStatisticsSuccess, OnGetUserStatisticsError );
        }
 
    private static void OnGetUserStatisticsSuccess ( GetPlayerStatisticsResult result )
        {
        //TODO update to use new 
 
        PF_Bridge.RaiseCallbackSuccess ( result.Statistics.ToString(), PlayFabAPIMethods.GetUserStatistics, MessageDisplayStyle.none );
        foreach ( var each in result.Statistics )
            userStatistics [ each.StatisticName ] = each.Value;
        }
 
    private static void OnGetUserStatisticsError ( PlayFabError error )
        {
        PF_Bridge.RaiseCallbackError ( error.ErrorMessage, PlayFabAPIMethods.GetUserStatistics, MessageDisplayStyle.error );
        }
 
    public static void UpdateUserStatistics ( Dictionary<string, int> updates )
        {
        var request = new UpdatePlayerStatisticsRequest ( );
        request.Statistics = new List<StatisticUpdate> ( );
 
        foreach ( var eachUpdate in updates ) // Copy the stats from the inputs to the request
            {
            int eachStat;
            userStatistics.TryGetValue ( eachUpdate.Key, out eachStat );
            request.Statistics.Add ( new StatisticUpdate { StatisticName = eachUpdate.Key, Value = eachUpdate.Value } ); // Send the value to the server
            userStatistics [ eachUpdate.Key ] = eachStat + eachUpdate.Value; // Update the local cache so that future updates are using correct values
            }
 
        PlayFabClientAPI.UpdatePlayerStatistics ( request, OnUpdateUserStatisticsSuccess, OnUpdateUserStatisticsError );
        }
 
    private static void OnUpdateUserStatisticsSuccess ( UpdatePlayerStatisticsResult result )
        {
        PF_Bridge.RaiseCallbackSuccess ( "User Statistics Loaded", PlayFabAPIMethods.UpdateUserStatistics, MessageDisplayStyle.none );
        GetCharacterStatistics ( ); // Refresh stats that we just updated
        }
 
    private static void OnUpdateUserStatisticsError ( PlayFabError error )
        {
        PF_Bridge.RaiseCallbackError ( error.ErrorMessage, PlayFabAPIMethods.UpdateUserStatistics, MessageDisplayStyle.error );
        }
 
    public static void GetCharacterStatistics ( )
        {
        var characterIDs = new List<string> ( );
        foreach ( var each in playerCharacters )
            {
            characterIDs.Add ( each.CharacterId );
            }
 
        var request = new ExecuteCloudScriptRequest
            {
            FunctionName = "GetCharacterStatistics",
            FunctionParameter = new { CharacterId = characterIDs },
            };
        PlayFabClientAPI.ExecuteCloudScript ( request, OnGetCharacterStatisticsSuccess, PF_Bridge.PlayFabErrorCallback );
        }
 
    private static void OnGetCharacterStatisticsSuccess ( ExecuteCloudScriptResult result )
        {
        if ( !PF_Bridge.VerifyErrorFreeCloudScriptResult ( result ) )
            return;
 
        characterStatistics = PlayFab.Json.JsonWrapper.DeserializeObject<Dictionary<string, Dictionary<string, int>>> ( result.FunctionResult.ToString ( ) );
        PF_Bridge.RaiseCallbackSuccess ( "", PlayFabAPIMethods.GetCharacterStatistics, MessageDisplayStyle.none );
        }
 
    public static void UpdateCharacterStatistics ( string charID, Dictionary<string, int> updates )
        {
        Dictionary<string, int> activeStats;
        characterStatistics.TryGetValue ( charID, out activeStats );
 
        if ( activeStats != null )
            {
            foreach ( var each in updates )
                {
                if ( activeStats.ContainsKey ( each.Key ) )
                    {
                    activeStats [ each.Key ] += each.Value;
                    }
                else
                    {
                    activeStats.Add ( each.Key, each.Value );
                    }
                }
 
 
 
            var request = new ExecuteCloudScriptRequest
                {
                FunctionName = "UpdateCharacterStats",
                FunctionParameter = new { CharacterId = charID, CharacterStatistics = activeStats },
                };
            PlayFabClientAPI.ExecuteCloudScript ( request, OnUpdateCharacterStatisticsSuccess, PF_Bridge.PlayFabErrorCallback );
            }
        }
    private static void OnUpdateCharacterStatisticsSuccess ( ExecuteCloudScriptResult result )
        {
        if ( !PF_Bridge.VerifyErrorFreeCloudScriptResult ( result ) )
            return;
 
        Debug.Log ( "Stats Saved!" );
        PF_Bridge.RaiseCallbackSuccess ( "", PlayFabAPIMethods.UpdateCharacterStatistics, MessageDisplayStyle.none );
        }
    }
#endregion
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.

saitmonato avatar image saitmonato commented ·

not seeing an edit button so sorry for the double post in here..

0 Likes 0 ·
brendan avatar image brendan saitmonato commented ·

No worries - I deleted the duplicate code.

0 Likes 0 ·

1 Answer

·
brendan avatar image
brendan answered

Which PlayFab ID are you testing with in your title? What do you see coming back from the call when you step into the success/failure callbacks in the debugger?

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

saitmonato avatar image saitmonato commented ·
In:

private static void OnGetUserStatisticsSuccess ( GetPlayerStatisticsResult result )
 {
 //TODO update to use new 
 
 PF_Bridge.RaiseCallbackSuccess ( result.Statistics.ToString(), PlayFabAPIMethods.GetUserStatistics, MessageDisplayStyle.none );
 foreach ( var each in result.Statistics )
 userStatistics [ each.StatisticName ] = each.Value;
 }

 PF_Bridge.RaiseCallbackSuccess ( result.Statistics.ToString(), PlayFabAPIMethods.GetUserStatistics, MessageDisplayStyle.none );

 result.Statistics finds the statisticsname called "xp" with the value 0

if thats the case what do I do to "use"  this info , if not with?:


    public static void GetUserStatistics ( )        {        GetPlayerStatisticsRequest request = new GetPlayerStatisticsRequest ( );        PlayFabClientAPI.GetPlayerStatistics ( request, OnGetUserStatisticsSuccess, OnGetUserStatisticsError );        }
0 Likes 0 ·
brendan avatar image brendan saitmonato commented ·

So, it sounds like you've confirmed that you are successfully getting the statistics back for the player. I'm not clear on your question on how you "use" that information, however. Statistics are numeric values that you can use to store data about anything relevant to your players - experience points (as in your case), level, etc. Your use of the statistics is going to be very specific to your title's needs. Can you clarify what information you're looking for?

0 Likes 0 ·
saitmonato avatar image saitmonato brendan commented ·

For example here: I do private static void OnGetUserStatisticsSuccess ( GetPlayerStatisticsResult result )

 {
 //TODO update to use new 
 
 PF_Bridge.RaiseCallbackSuccess ( result.Statistics.ToString(), PlayFabAPIMethods.GetUserStatistics, MessageDisplayStyle.none );
 foreach ( var each in result.Statistics )
 {
 userStatistics [ each.StatisticName ] = each.Value;
 Debug.Log ( each.StatisticName + each.Value);
 } 
 
 }


with the Debug.Log ( each.StatisticName + each.Value); result i could display this on unities UI as text for example, but I wouldnt want to assign that on the line where the debug.log is, I think? so I was wondering what a proper way would be to use to access that result output in other methods

0 Likes 0 ·
Show more comments
saitmonato avatar image saitmonato commented ·

AH! god im so stupid,thank you, thats one step forward haha

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.