Put it all together - Wallet Essentials - (Unreal Engine module)
Connect the UI to display player's wallet info
-
Open the
WalletBalanceWidget_StarterCPP file, navigate to theNativeOnActivated()function, and replace the existing implementation with the code below. This updated code binds theShowBalance()function to theWalletEssentialsSubsystem_Starter's response delegate, which you implemented on the previous page. With this setup, the wallet info will be refreshed each time theWalletBalanceWidget_Starterwidget is reactivated.void UWalletBalanceWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
WalletSubsystem = GetGameInstance()->GetSubsystem<UWalletEssentialsSubsystem_Starter>();
ensure(WalletSubsystem);
WalletSubsystem->OnQueryOrGetWalletInfoCompleteDelegates.AddUObject(this, &ThisClass::ShowBalance);
// Update balance.
W_Root->ClearChildren();
CurrencyBalanceEntryMap.Empty();
UpdateBalance(ECurrencyType::COIN);
UpdateBalance(ECurrencyType::GEM);
} -
Navigate to the
NativeOnDeactivated()function and replace the existing implementation with the code below. This code clears the binding to theWalletEssentialsSubsystem_Starter's response delegate, ensuring no updates are triggered when the widget is deactivated.void UWalletBalanceWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
WalletSubsystem->OnQueryOrGetWalletInfoCompleteDelegates.RemoveAll(this);
} -
Navigate to the
UpdateBalance()function and replace the existing implementation with the code below. This code calls theQueryOrGetWalletInfoByCurrencyCode()function from theWalletEssentialsSubsystem_Starter, which you implemented on the previous page.void UWalletBalanceWidget_Starter::UpdateBalance(const ECurrencyType CurrencyType)
{
const FString CurrencyCode = FPreConfigCurrency::GetCodeFromType(CurrencyType);
UWalletBalanceWidgetEntry* Entry = CreateWidget<UWalletBalanceWidgetEntry>(this, CurrencyBalanceClass);
ensure(Entry);
Entry->Setup(FText::FromString("..."), CurrencyType);
W_Root->AddChild(Entry);
CurrencyBalanceEntryMap.Add(CurrencyCode, Entry);
// Always retrieve from backend to make sure data is updated.
WalletSubsystem->QueryOrGetWalletInfoByCurrencyCode(GetOwningPlayer(), CurrencyCode, true);
}
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.