メインコンテンツまでスキップ

すべてを統合する - 統計データを追跡し表示する - (Unreal Engine モジュール)

Last updated on May 30, 2024

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.

  1. Open AccelByteWars.sln in Visual Studio. From the Solution Explorer, open the the StatsProfileWidget_Starter widget class CPP file.

  2. 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);
    }
  3. Still in the StatsProfileWidget_Starter widget class CPP file, navigate to OnQueryLocalUserStatsComplete() 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);
    }
  4. Compile your project and make sure there are no errors.

Resources