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

Put it all together - Store Item Purchase - (Unreal Engine module)

Last updated on July 28, 2025

Connect the UI to item purchase

  1. Open the ItemPurchaseWidget_Starter CPP file, navigate to the NativeOnActivated() function, and replace the existing implementation with the code below. This new implementation binds the OnPurchaseComplete() function to the StoreItemPurchaseSubsystem_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();
    }
  2. Navigate to the NativeOnDeactivated() function and replace the existing implementation with the code below. This implementation clears the binding to the StoreItemPurchaseSubsystem_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);
    // ...
    }
  3. Navigate to the OnClickPurchase() function and replace the existing implementation with the code below. This new code calls the CreateNewOrder() function from the StoreItemPurchaseSubsystem_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