数组仅在第一次返回错误值
Array returns bad values only first time
所以我正在使用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"吗?
- (C++)分析树以计算返回错误值的简单算术表达式
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- C++中函数的向量返回类型引发错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- glad 导致 glfwSwapBuffers 返回错误消息
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- QNX Momentics: GoogleMock - 返回有价值,在函数中返回 void 错误
- 我的模板类方法返回错误类型?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 替换WCHAR_T错误返回存储地址
- 错误:void 值未被忽略,因为它应该被忽略,错误:返回语句具有值
- 带有值的递归阶乘错误返回语句,在函数中返回 'void' [-fallowive]
- 存储在静态常量整数 (C++) 中的对数函数的错误返回值
- 更丰富的错误返回消息用于提升::精神::qi 解析
- C++初级逻辑错误-返回0
- 由于进程很多,pthread_create的错误返回代码是 35 错误,我用过pthread_exit应该杀死线程不是吗?
- c++错误处理返回值错误返回
- systememerror:没有异常集的错误返回