Search Unity

Feature Request Cannot reduce currency amount to less than 0

Discussion in 'Economy' started by MiTschMR, Aug 7, 2022.

  1. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    489
    There are use cases where we need to reduce the amount of a currency by a certain amount. However, if the current amount has less than what it will be decremented by, an error is thrown:
    EconomyException: Not enough currency
    UnityEngine.Debug:LogError (object)
    Unity.Services.Economy.EconomyAPIErrorHandler:HandleException (Unity.Services.Economy.Internal.Http.HttpException`1<Unity.Services.Economy.Internal.Models.BasicErrorResponse>) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Exceptions/EconomyAPIErrorHandler.cs:73)
    Unity.Services.Economy.PlayerBalancesInternal/<DecrementBalanceAsync>d__8:MoveNext () (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/PlayerBalances.cs:199)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Unity.Services.Economy.Internal.Response`1<Unity.Services.Economy.Internal.Models.CurrencyBalanceResponse>>:SetException (System.Exception)
    Unity.Services.Economy.Internal.Apis.Currencies.CurrenciesApiClient/<DecrementPlayerCurrencyBalanceAsync>d__7:MoveNext () (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Apis/CurrenciesApi.cs:132)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Unity.Services.Economy.Internal.Http.HttpClientResponse>:SetResult (Unity.Services.Economy.Internal.Http.HttpClientResponse)
    Unity.Services.Economy.Internal.Http.HttpClient/<MakeRequestAsync>d__1:MoveNext () (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:39)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Unity.Services.Economy.Internal.Http.HttpClientResponse>:SetResult (Unity.Services.Economy.Internal.Http.HttpClientResponse)
    Unity.Services.Economy.Internal.Http.HttpClient/<CreateWebRequestAsync>d__3:MoveNext () (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:77)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Unity.Services.Economy.Internal.Http.HttpClientResponse>:SetResult (Unity.Services.Economy.Internal.Http.HttpClientResponse)
    Unity.Services.Economy.Internal.Http.HttpClient/<>c__DisplayClass3_0/<<CreateWebRequestAsync>b__0>d:MoveNext () (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:74)
    System.Threading.Tasks.TaskCompletionSource`1<Unity.Services.Economy.Internal.Http.HttpClientResponse>:SetResult (Unity.Services.Economy.Internal.Http.HttpClientResponse)
    Unity.Services.Economy.Internal.Http.UnityWebRequestHelpers/<>c__DisplayClass0_0:<GetAwaiter>b__0 (UnityEngine.AsyncOperation) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/UnityWebRequestHelpers.cs:34)
    UnityEngine.AsyncOperation:InvokeCompletionEvent ()

    HttpException`1: (422) HTTP/1.1 422 Unprocessable Entity
    Unity.Services.Economy.Internal.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.Economy.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/ResponseHandler.cs:112)
    Unity.Services.Economy.Internal.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.Economy.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/ResponseHandler.cs:224)
    Unity.Services.Economy.Internal.Apis.Currencies.CurrenciesApiClient.DecrementPlayerCurrencyBalanceAsync (Unity.Services.Economy.Internal.Currencies.DecrementPlayerCurrencyBalanceRequest request, Unity.Services.Economy.Internal.Configuration operationConfiguration) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Apis/CurrenciesApi.cs:131)
    Unity.Services.Economy.PlayerBalancesInternal.DecrementBalanceAsync (System.String currencyId, System.Int32 amount, Unity.Services.Economy.DecrementBalanceOptions options) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/PlayerBalances.cs:191)
    Rethrow as EconomyException: Not enough currency
    Unity.Services.Economy.PlayerBalancesInternal.DecrementBalanceAsync (System.String currencyId, System.Int32 amount, Unity.Services.Economy.DecrementBalanceOptions options) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/PlayerBalances.cs:199)
    MiTschMR.Runtime.EasyUGS.Economy.InstructionDecrementCurrencyBalance.Run (GameCreator.Runtime.Common.Args args) (at Assets/Plugins/GameCreator/Installs/EasyUGS.Economy@1.0.0/Runtime/VisualScripting/Instructions/InstructionDecrementCurrencyBalance.cs:32)
    UnityEngine.Debug:LogException(Exception)
    MiTschMR.Runtime.EasyUGS.Economy.<Run>d__4:MoveNext() (at Assets/Plugins/GameCreator/Installs/EasyUGS.Economy@1.0.0/Runtime/VisualScripting/Instructions/InstructionDecrementCurrencyBalance.cs:36)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetException(Exception)
    Unity.Services.Economy.<DecrementBalanceAsync>d__8:MoveNext() (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/PlayerBalances.cs:207)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetException(Exception)
    Unity.Services.Economy.Internal.Apis.Currencies.<DecrementPlayerCurrencyBalanceAsync>d__7:MoveNext() (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Apis/CurrenciesApi.cs:132)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(HttpClientResponse)
    Unity.Services.Economy.Internal.Http.<MakeRequestAsync>d__1:MoveNext() (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:39)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(HttpClientResponse)
    Unity.Services.Economy.Internal.Http.<CreateWebRequestAsync>d__3:MoveNext() (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:77)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(HttpClientResponse)
    Unity.Services.Economy.Internal.Http.<<CreateWebRequestAsync>b__0>d:MoveNext() (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/HttpClient.cs:74)
    System.Threading.Tasks.TaskCompletionSource`1:SetResult(HttpClientResponse)
    Unity.Services.Economy.Internal.Http.<>c__DisplayClass0_0:<GetAwaiter>b__0(AsyncOperation) (at Library/PackageCache/com.unity.services.economy@2.0.4/Runtime/Generated/Runtime/Http/UnityWebRequestHelpers.cs:34)
    UnityEngine.AsyncOperation:InvokeCompletionEvent()

    Is that desired to work like this or is it possible to set it 0 if the amount reaches a negative number? I honestly don't want to spend too many API calls (tell me whatever you want, I don't care) simply checking the amount left to decrement it properly.
     
  2. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Thanks for sharing your feedback, it's greatly appreciated.

    You make a very valid point, clamping the minimum value to zero rather than throwing an exception would be a better outcome. Your suggestion has been added to the list, hopefully we will be able to make this improvement in the near future. :)

    Thanks,
    L
     
    Tvtig and MiTschMR like this.