question

leeharvey avatar image
leeharvey asked

how to subtract 1 playfab virtual currency each time a player hits a play game button

Hi all total noob here keen to learn more.

I have followed the moralis skyrim shop demo and setup playfab for my store. I am now looking to go a step further and in one of my stores (Item Shop) I have a button on a canvas named (trade Button), I am trying when the button is pressed to call the subtract api call to playfab and have 1 token removed from the player.

I have tried numerous ways. Ideally it would be in the script below but if easier then can be in an empty object as its own script and dragged into the onclick event on the button itself.

Can someone help with the code? - Thank you

    using System;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    using PlayFab;
    using PlayFab.ClientModels;

    namespace Web3_Skyrim
    {
        public class ItemShop : ShopBase
        {
            public static Action<int, int> OnTradeExecuted;

            private PlayFabVirtualCurrency _playFabVirtualCurrency;
            
            [Header("Header UI")]
            [SerializeField] private TextMeshProUGUI crystalBalanceLabel;

            


            private int _currentCrystalBalance;
            

            #region UNITY_LIFECYCLE

            private void Awake()
            {
                title.text = "ItemShop";   
            }

            protected override void OnEnable()
            {
                base.OnEnable();
                ResetData();
                
                // First we create a new instance and we subscribe to the needed event
                _playFabVirtualCurrency = new PlayFabVirtualCurrency();
                _playFabVirtualCurrency.OnGetBalanceSuccess += OnGetBalanceSuccessHandler;
                
                // Now we call the GetBalance function. We will get the balance on the event that we just subscribed
                _playFabVirtualCurrency.GetBalance();
            }

            private void OnDisable()
            {
                ResetData();
                
                _playFabVirtualCurrency.OnGetBalanceSuccess -= OnGetBalanceSuccessHandler;
            }

            #endregion

            public void OnTradeButtonPressed()
            {
            
            }

            #region EVENT_HANDLERS

            private void OnGetBalanceSuccessHandler(int currencyBalance)
            {
                _currentCrystalBalance = currencyBalance;
                crystalBalanceLabel.text = _currentCrystalBalance.ToString();

            }

            private void ResetData()
            {
            
            }
        }
    }
    #endregion

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Gosen Gao avatar image
Gosen Gao answered

It seems like your issue is more like a Unity Script or UI issue, if so, we recommend you contacting Unity for professional support. As for subtracting virtual currency, since we should not handle the virtual currency on the client, we recommend that you can implement an Azure Function to subtract virtual currency and then you can call ExecuteFunction when the button is pressed to execute the Azure Function.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.