すべてを統合する - 統計データを追跡し表示する - (Unreal Engine モジュール)
Connect the UI to display user's stats
In this tutorial, you will connect the stats Profile menu widget to the stats implementation you created. You are going to get a reference to the StatsEssentialsSubsystem_Starter
subsystem, trigger the Query stats functionality to get the local user's stats, and display it.
Open
AccelByteWars.sln
in Visual Studio. From the Solution Explorer, open the theStatsProfileWidget_Starter
widget class CPP file.In the
StartQueryLocalUserStats()
function definition you created earlier, add the code below. This function will be responsible for starting the query task and setting up the callback.void UStatsProfileWidget_Starter::StartQueryLocalUserStats()
{
const int32 LocalUserNum = GetOwningPlayer()->GetLocalPlayer()->GetControllerId();
const bool bStarted = EssentialsSubsystem->QueryLocalUserStats(
LocalUserNum,
{
UStatsEssentialsSubsystem_Starter::StatsCode_HighestElimination,
UStatsEssentialsSubsystem_Starter::StatsCode_KillCount,
UStatsEssentialsSubsystem_Starter::StatsCode_HighestSinglePlayer,
UStatsEssentialsSubsystem_Starter::StatsCode_HighestTeamDeathMatch
},
FOnlineStatsQueryUsersStatsComplete::CreateUObject(this, &ThisClass::OnQueryLocalUserStatsComplete));
// Show loading
const bool bLoading = Ws_Outer->GetActiveWidget() == W_LoadingOuter;
Ws_Outer->SetActiveWidget((bLoading || bStarted) ? W_LoadingOuter : W_FailedOuter);
}Still in the
StatsProfileWidget_Starter
widget class CPP file, navigate toOnQueryLocalUserStatsComplete()
function and add the below implementation. This function will display the result received from the Query task to the widget.void UStatsProfileWidget_Starter::OnQueryLocalUserStatsComplete(
const FOnlineError& ResultState,
const TArray<TSharedRef<const FOnlineStatsUserStats>>& UsersStatsResult)
{
// clear previous entries
Deb_StatsList->Reset();
if (!ResultState.bSucceeded)
{
Ws_Outer->SetActiveWidget(W_FailedOuter);
return;
}
for (const TSharedRef<const FOnlineStatsUserStats>& UsersStats : UsersStatsResult)
{
for (const TTuple<FString, FVariantData>& Stat : UsersStats->Stats)
{
// by default, AB OSS store stats value as float
float StatValue;
Stat.Value.GetValue(StatValue);
// Add entry object
const UStatsProfileWidgetEntry* WidgetEntry = Deb_StatsList->CreateEntry<UStatsProfileWidgetEntry>();
WidgetEntry->Setup(FText::FromString(Stat.Key), FText::AsNumber(StatValue));
}
}
// Hide loading
Ws_Outer->SetActiveWidget(Deb_StatsList);
}Compile your project and make sure there are no errors.
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.