Skip to main content

Unreal Engine Module - Track and display a player's high score - Put it all together

Last updated on January 13, 2024

Connecting UI with stats implementation

In this section, you will learn how to connect the stats Profile menu widget to the stats implementation that we created in the previous section. We are going to get a reference to the StatsEssentialsSubsystem_Starter subsystem, trigger the Query stats functionality to get the local user's stats and displays it.

  1. Open AccelByteWars.sln using your preferred IDE, then from Solution Explorer, open the StatsProfileWidget_Starter widget class CPP file.

  2. In the StartQueryLocalUserStats() function definition that we created earlier in section Stats profile menu add the following code. This function will be the one responsible to start 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 following implementation. This function will display the result received from 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. Build the AccelByteWars project, run the editor, then play the game in PIE mode. When you get to the main menu, you should see a new button called Profile. If you click that button, you should then see a new menu with a button called stats. Here, you will be presented with a list of the current local user's stats.

    Stats menu preview

  5. Congratulations! You have successfully implemented AGS's stats to the stats Profile menu widget.

Resources