Put it all together - Store Item Purchase - (Unreal Engine module)
Connect the UI to item purchase
-
Open the
ItemPurchaseWidget_Starter
CPP file, navigate to theNativeOnActivated()
function, and replace the existing implementation with the code below. This new implementation binds theOnPurchaseComplete()
function to theStoreItemPurchaseSubsystem_Starter
's response delegate, which you implemented in the previous page, as soon as the widget is activated.void UItemPurchaseWidget_Starter::NativeOnActivated()
{
Super::NativeOnActivated();
W_Parent = GetFirstOccurenceOuter<UStoreItemDetailWidget>();
ensure(W_Parent);
StoreItemDataObject = W_Parent->StoreItemDataObject;
ensure(StoreItemDataObject);
PurchaseSubsystem = GetGameInstance()->GetSubsystem<UStoreItemPurchaseSubsystem_Starter>();
ensure(PurchaseSubsystem);
// ...
// Setup UI
SetupPurchaseButtons(StoreItemDataObject->GetPrices());
Ws_Root->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Not_Empty);
Tb_Success->SetVisibility(ESlateVisibility::Collapsed);
Tb_Error->SetVisibility(ESlateVisibility::Collapsed);
Ss_Amount->SetSelectedIndex(0);
Ss_Amount->OnSelectionChangedDelegate.AddUObject(this, &ThisClass::UpdatePrice);
// Show amount if consumable
Ss_Amount->SetVisibility(StoreItemDataObject->GetIsConsumable() ? ESlateVisibility::Visible : ESlateVisibility::Collapsed);
// Setup delegate
PurchaseSubsystem->OnCheckoutCompleteDelegates.AddUObject(this, &ThisClass::OnPurchaseComplete);
// ...
// Set focus
if (W_PurchaseButtonsOuter->HasAnyChildren())
{
W_PurchaseButtonsOuter->GetChildAt(0)->SetUserFocus(GetOwningPlayer());
}
FTUESetup();
} -
Navigate to the
NativeOnDeactivated()
function and replace the existing implementation with the code below. This implementation clears the binding to theStoreItemPurchaseSubsystem_Starter
's response delegate to ensure nothing is triggered when the widget is deactivated.void UItemPurchaseWidget_Starter::NativeOnDeactivated()
{
Super::NativeOnDeactivated();
PurchaseSubsystem->OnCheckoutCompleteDelegates.RemoveAll(this);
Ss_Amount->OnSelectionChangedDelegate.RemoveAll(this);
// ...
} -
Navigate to the
OnClickPurchase()
function and replace the existing implementation with the code below. This new code calls theCreateNewOrder()
function from theStoreItemPurchaseSubsystem_Starter
, which you implemented in the previous page.void UItemPurchaseWidget_Starter::OnClickPurchase(const int32 PriceIndex) const
{
Ws_Root->SetWidgetState(EAccelByteWarsWidgetSwitcherState::Loading);
// ...
// Otherwise, fallback to use AccelByte platform to purchase the item.
PurchaseSubsystem->CreateNewOrder(
GetOwningPlayer(),
StoreItemDataObject,
PriceIndex,
GetSelectedAmount());
}
Resources
- The files used in this tutorial section are available in the Byte Wars GitHub repository.