'UWP Xbox Live Leaderboards

I am trying to incorporate leaderboards into my UWP game. I am using C# and XAML to create my game. I have created a high score leaderboard and have pushed it with the test button in the dash board.

I have pushed a record to the leaderboard by calling:

_stats.SetStatisticIntegerData(XboxUser, "score", XP);
_stats.RequestFlushToService(XboxUser, true);

I receive a successful response from the server.

When I try to retrieve the leaderboard from the server by the following:

_stats.GetLeaderboard(XboxUser, "score", new LeaderboardQuery());

I get back a successful response.

The problem is I do not receive any rows with my response. I get back one column with the displayName of High Score, but no rows.

Does anyone know what I am doing incorrectly?

UPDATE A bit more information:

I have created a leaderboard with the ID of score.

when I call the following:

var val = _stats.GetStatistic(XboxUser, "score");

It throws the following exception:

The parameter is incorrect.

Stat not found in document

When I store the data into the leaderboard I get the following:

Static Event:

ErrorCode: 0

ErrorMessage: ""

EventArgs: null

EventType: StatisticUpdateComplete

User: my User



Solution 1:[1]

I have the answer! Before calling add static or getting the Leaderboard you have to wait until the StatisticEventType.LocalUserAdded Event completes.

Solution 2:[2]

_stats.DoWork is not an async method or do method It's like a check method, it just checks the result of the previous action so you have to put this DoWork in-game Frame or DispatcherTimer to check the result again and again. Until you have StatisticEventType.GetLeaderboardComplete result that you can have the LeaderBoard result. here is part of my code using a DispatcherTimer

enter code here
    public static DispatcherTimer timer = new DispatcherTimer();

    static void GetLeaderboard(string leaderBoardName)
    {
        if (!is_online) return;


        var statManager = StatisticManager.SingletonInstance;
        LeaderboardQuery query = new LeaderboardQuery
        {
            SkipResultToMe = true,
            //SkipResultToRank = 100,
            MaxItems = 5,
        };
        statManager.GetLeaderboard(xboxUser, leaderBoardName, query);

        timer.Interval = TimeSpan.FromSeconds(0.05);
        timer.Start();
        timer.Tick += (s, e) =>
        {

            IReadOnlyList<StatisticEvent> statEvents = statManager.DoWork();
            //In practice this should be called everyupdate frame

            foreach (StatisticEvent statEvent in statEvents)
            {
                if (statEvent.EventType == StatisticEventType.GetLeaderboardComplete
                && statEvent.ErrorCode == 0)
                {
                    LeaderboardResultEventArgs leaderArgs = (LeaderboardResultEventArgs)statEvent.EventArgs;
                    LeaderboardResult leaderboardResult = leaderArgs.Result;
                    foreach (LeaderboardRow leaderRow in leaderboardResult.Rows)
                    {
                        Debug.WriteLine(string.Format("Rank: {0} | Gamertag: {1} | {2}:{3}\n\n", leaderRow.Rank,
                        leaderRow.Gamertag, "test", leaderRow.Values[0]));
                    }
                    timer.Stop();
                }

                Debug.WriteLine(statEvent.EventType.ToString());
                Debug.WriteLine(statEvent.ErrorCode);

            }

            Debug.WriteLine(statEvents.Count);

        };


        
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Travis Pettry
Solution 2 Mohi