数组仅在第一次返回错误值

Array returns bad values only first time

本文关键字:错误 返回 第一次 数组      更新时间:2023-10-16

所以我正在使用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;
};

向蒸汽发送请求的函数: .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;
}

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

我的正常流程是: 1.我已经有了排行榜的把柄。 2.我打电话给DownloadScoresAroundUser。 3.流进入潜伏状态,无法进行分数=假。 4.在我收到来自 steam OnDownloadScore 的回电后,给我所有需要的信息(检查是否真的,它确实如此! 5.然后我调用翻译条目来获取数组中带有名称和排名的所有分数。 6.然后我打印整个数组(在虚幻引擎中使用中断包(并获取结构的默认值。 7.再次触发整个循环后,我得到了正确的值。

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

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

当您进行第二次调用时,已经过去了足够的时间来下载分数并填充Scores,因此它会返回一些分数。

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

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

转换条目将数据从 0 复制到 30,但实际上只初始化了"回调>m_cEntryCount"。因此,如果它在 30 <,则从"回调>m_cEntryCount"到 30 的数据可能是错误的。你能打印出这个变量的值"在SteamLeaderboard::OnDownloadScore"吗?