Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
17
Indexable
Never
using UnityEngine;
using HutongGames.PlayMaker;
using System.Threading.Tasks;
using TcgEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.ScriptControl)]
    [Tooltip("Set custom coins value in UserData and save.")]
    public class SetCustomCoinsValue : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The value of coins to set.")]
        public FsmInt coinsAmount;

        private bool userDataLoaded = false;

        public override async void OnEnter()
        {
            // Load UserData asynchronously if not already loaded
            if (!userDataLoaded)
            {
                await LoadUserDataAsync();
            }

            if (Authenticator.Get().UserData != null)
            {
                // Get the UserData from the Authenticator
                UserData userData = Authenticator.Get().UserData;

                // Set the custom coins amount in UserData
                userData.coins = coinsAmount.Value;

                // Save the updated UserData asynchronously
                bool saveResult = await Authenticator.Get().SaveUserData();

                // You can handle the result here if needed
                if (saveResult)
                {
                    Debug.Log("UserData saved successfully.");
                }
                else
                {
                    Debug.LogWarning("UserData failed to save.");
                }
            }
            else
            {
                Debug.LogWarning("UserData not available. Make sure you are logged in.");
            }

            // Exit the action
            Finish();
        }

        private async Task LoadUserDataAsync()
        {
            // Load UserData asynchronously
            await Authenticator.Get().LoadUserData();

            // Mark UserData as loaded
            userDataLoaded = true;
        }
    }
}