0

所以我正在使用 Steamworks(排行榜),但我遇到了一些奇怪的问题。当我触发我的函数以获得分数时,从调试中我知道它工作得很好。但是第一次函数运行后我的数组总是返回默认值。在我第二次触发函数后,一切都很好。我试图追查这个问题,但我失败了。

这是我在这种情况下使用的整个代码:

统计结构

 USTRUCT(BlueprintType)
    struct FScorePackage
    {
        GENERATED_BODY()

            UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Leaderboard")
            FString PlayerName = "working";

        UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Leaderboard")
            int32 Rank = 0;

        UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Leaderboard")
            int32 Score = 0;

    };

向 Steam 发送请求的函数:.h

UFUNCTION(BlueprintCallable, Category = "Steam|Leaderboard", meta = (Latent, LatentInfo = "LatentInfo", HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject"))
        TArray<FScorePackage> DownloadScoresAroundUser(UObject* WorldContextObject, int AboveUser, int BelowUser, struct FLatentActionInfo LatentInfo);

.cpp

TArray<FScorePackage> USteamLeaderboard::DownloadScoresAroundUser(UObject* WorldContextObject, int AboveUser, int BelowUser, struct FLatentActionInfo LatentInfo)
{
    if (!m_CurrentLeaderboard)
    {
        return Scores;
    }

    if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject))
    {
        FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
        if (LatentActionManager.FindExistingAction<SteamLeaderboardLatentClass>(LatentInfo.CallbackTarget, LatentInfo.UUID) == NULL)
        {
            // load the specified leaderboard data around the current user
            SteamAPICall_t hSteamAPICall = SteamUserStats()->DownloadLeaderboardEntries(m_CurrentLeaderboard, k_ELeaderboardDataRequestGlobalAroundUser, -AboveUser, BelowUser);
            m_callResultDownloadScore.Set(hSteamAPICall, this,&USteamLeaderboard::OnDownloadScore);
            LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, new SteamLeaderboardLatentClassScores(LatentInfo));

            return Scores;
        }
        return Scores;
    }
    return Scores;
}

现在来自 Steam 的回调函数:.h

void OnDownloadScore(LeaderboardScoresDownloaded_t *pResult, bool bIOFailure);
    CCallResult <USteamLeaderboard, LeaderboardScoresDownloaded_t> m_callResultDownloadScore;

.cpp

void USteamLeaderboard::OnDownloadScore(LeaderboardScoresDownloaded_t *pCallback, bool bIOFailure)
{
    if (!bIOFailure)
    {
        m_nLeaderboardEntries = __min(pCallback->m_cEntryCount, 30);

        for (int index = 0; index < m_nLeaderboardEntries; index++)
        {
            SteamUserStats()->GetDownloadedLeaderboardEntry(pCallback->m_hSteamLeaderboardEntries, index, &m_leaderboardEntries[index], NULL, 0);
        }
        TranslateEntries();
        scores = true;


    }
}

最后是在数组中写入分数的函数:

。H

UFUNCTION(BlueprintCosmetic, Category = "Steam|Leaderboard")
         TArray<FScorePackage> TranslateEntries();

.cpp

    TArray<FScorePackage> USteamLeaderboard::TranslateEntries()
    {
FScorePackage ThisScore;
        Scores.Init(ThisScore, 30);

        for (int i = 0; i < 30; i++)
        {
            ThisScore.PlayerName = GetSteamName(m_leaderboardEntries[i].m_steamIDUser);
            ThisScore.Rank = m_leaderboardEntries[i].m_nGlobalRank;
            ThisScore.Score = m_leaderboardEntries[i].m_nScore;
            Arrayas[i] = ThisScore;
        }
        return Scores;

    }

Scores 数组只是静态 TArray 分数,而 scores=true 仅用于在调用 DownloadScoresAroundUser 后进行潜在检查以继续使用函数:)

我的正常流程是: 1.我已经有了排行榜的句柄。2.我打电话给 DownloadScoresAroundUser。3.Flow 进入潜在状态,由于 score=false 而无法继续。4.在我从蒸汽 OnDownloadScore 火灾中得到回调后,给我所有需要的信息(检查是否确实如此!)。5.然后我调用 TranslateEntries 来获取所有带有名称的分数并在数组中排名。6.然后我打印整个数组(在虚幻中使用中断包)并获取我的结构的默认值。7.在我再次触发整个周期后,我得到了正确的值。

如果需要任何进一步的信息,请告诉我:)

4

2 回答 2

1

这有点猜测,但您似乎遇到了延迟问题。当您请求下载乐谱时,这是一个不会阻塞的耗时调用。您设置了一个回调,当分数准备好时将调用该回调,然后返回现有的空Scores对象。

当您拨打第二个电话时,已经有足够的时间下载和Scores填充乐谱,因此它会返回一些乐谱。

请注意,您有一个潜在的竞争条件,当您的回调填充该向量时,DownloadScoresAroundUser可以在哪里访问(返回) 。Scores

这是一种可能的解决方案。在分数完成加载之前,DownloadScoresAroundUser返回一个空分数(或者可能是一个指示正在加载分数的分数)。加载并Scores填充分数后,它将返回这些分数。此外,回调(除了填充Scores)可以以某种方式通知调用者DownloadScoresAndUser新的分数可用。他们可以通过再次调用以获取更新的分数并刷新显示来对此做出响应。

于 2017-07-11T01:50:17.493 回答
0

Translateentries 将数据从 0 复制到 30,但实际上只有“Callback->m_cEntryCount”被初始化。所以如果它<在30,从“Callback->m_cEntryCount”到30的数据可能是错误的。你能打印出这个变量“in SteamLeaderboard::OnDownloadScore”的值吗?

于 2017-07-10T22:06:52.710 回答