【问题标题】:Integer value resetting to zero after function call (Playfab-Unity)函数调用后整数值重置为零(Playfab-Unity)
【发布时间】:2020-05-06 17:52:24
【问题描述】:

我需要从 Playfab 排行榜列表中获得最高分并将其显示在 Unity 中。我可以得到最高分,然后将其分配给全局变量。我的函数返回这个变量,但它总是返回为零。排行榜中的最高分值为 50。

// GETS THE QUIZ'S, THAT THE USER HAS CLICKED, HIGHEST SCORE VALUE FROM THE PLAYFAB LEADERBOARD.
    public int GetLeaderboardList()
    {
        var request = new GetLeaderboardRequest();
        request.StartPosition = 0;
        request.StatisticName = QuizButtonClicked.clickedButtonQuiz.name; // the button that the user has clicked.
        request.MaxResultsCount = 20;
        PlayFabClientAPI.GetLeaderboard(request, (result) =>
        {
            topScoreOnLeaderboard = result.Leaderboard.ElementAt(0).StatValue;
            Debug.LogError(result.Leaderboard.ElementAt(0).StatValue); // 1- Result is 50.
            Debug.LogError(topScoreOnLeaderboard); // 2- Result is 50.

        }, (error) =>
        {
            Debug.LogError(error.GenerateErrorReport());
        });
        Debug.LogError(topScoreOnLeaderboard); // 3- But here it gives 0.
        return topScoreOnLeaderboard;
    }

编辑:调试日志顺序为 3-1-2。

【问题讨论】:

  • PlayFabClientAPI.GetLeaderboard() 会在继续执行之前等待响应吗?
  • @Jake 是的,会的。

标签: c# azure unity3d


【解决方案1】:

Debug log order is 3-1-2.

这听起来像PlayFabClientAPI.GetLeaderboard() 是异步执行的或在后台线程中执行的。

线条

Debug.LogError(topScoreOnLeaderboard); // 3- But here it gives 0.
return topScoreOnLeaderboard;

只是在结果实际到达之前到达。


您必须等到结果到达才能继续。

我会使用它,例如通过Action<int> 喜欢

public void GetLeaderboardListAsync(Action<int> onResult)
{
    var request = new GetLeaderboardRequest();
    request.StartPosition = 0;
    request.StatisticName = QuizButtonClicked.clickedButtonQuiz.name; // the button that the user has clicked.
    request.MaxResultsCount = 20;
    PlayFabClientAPI.GetLeaderboard(request, (result) =>
    {
        var resultValue = result.Leaderboard.ElementAt(0).StatValue;
        Debug.Log(resultValue);
        onResult?.Invoke(resltValue);
    }, (error) =>
    {
        Debug.LogError(error.GenerateErrorReport());
    });
}

然后改为将您调用它的代码更改为类似

GetLeaderboardListAsync(OnLeaderBoardTopAvailable);


private void OnLeaderBoardTopAvailable(int topValue)
{
    // Whatever should happen once you have the index
}

顺便说一句:小心Debug.LogError,因为它还可能阻止代码进一步执行。而是使用始终继续执行代码的Debug.LogWarning

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 2022-01-19
    • 2019-12-01
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多