Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Method to fetch all currency balances at once

Discussion in 'Economy' started by BayouSoftware, Nov 12, 2021.

  1. BayouSoftware

    BayouSoftware

    Joined:
    Oct 7, 2012
    Posts:
    15
    Hi Im looking at creating a Gacha style mechanic as part of my game where the player collects shards which will unlock a skin after they have collected a certain amount. It seems to me that setting these shards up as a currency is the more logical choice, but there is no ability to fetch all of the players balances with one call so I will need to make 30+ calls to fetch all of the individual shard balances.

    Is this the incorrect approach? Should they be inventory items instead? As that seems inefficent.

    Also adding my voice to the people asking for a way of uploading items/currencies via json :)
     
  2. BayouSoftware

    BayouSoftware

    Joined:
    Oct 7, 2012
    Posts:
    15
    Just realised there is already a method for this its just in a different part of the documentation! :oops:
     
  3. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi,

    Thanks for checking out the Economy package and posting here. I'm glald you managed to find the answer to your question. For anybody else that is curious, the GetBalancesAsync SDK method will return all balances for the user, including those for currencies that have since been deleted.

    I've added your voice to the upload items/currencies via JSON feature request, thank you :)

    Please let us know if any other questions or observations come while you are testing the Economy features.
     
    FabriR and BayouSoftware like this.
  4. BayouSoftware

    BayouSoftware

    Joined:
    Oct 7, 2012
    Posts:
    15
    Hi Laurie, I was just working on the Gacha system and ran into the upper cap of 20 currencies, would you mind expanding this limit for me please? (I only need 23 currencies)

    Or do you think I should be approaching this differently, I had contemplated about saving the players shards using the Cloud save system, but using currencies seemed like it would need less work to set up and would have less potential points of failure.

    Cheers
     
  5. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi,

    Thanks for providing additional details on your use case. In general we would consider more than 20 currencies to be pretty excessive, but I think there is a better way to achieve your goal.

    Rather than thinking of each of your shards as a currency in it's own right, think of it more like a collectable inventory item that can be used buy, unlock, craft or exchange for another virtual inventory item.
    e.g.
    • 10 x carrots == 1 x bowl of carrot soup
    • 10 x Shard X == Gatcha X
    • 5 x Shard Y == Gatcha Y
    From there you can create a Virtual purchase definition that specifies the cost and reward for your virtual purchase
    e.g. Make Carrot Soup or Unlock Gatcha X

    Then use MakeVirtualPurchaseAsync to make a virtual purchase and update the player's inventory

    You might want to look at Remote Config & Cloud Code to handle the randomisation of the gatcha reward. This would allow you to remotely modify the gatcha logic without needing to republish your game and provide a secure server authoritative gatcha redemtion mechanism. When the player opens the unlocked gatcha to redeem their reward, Cloud code can perform the randomisation logic then remove the Gatcha item from the player's inventory and deposit the reward in to their inventory. There is a Cloud Code Gatcha example that shows how to achieve this.

    I hope that helps, please let me know if you have any follow on questions.

    It is really great to hear how you are using the new gaming services, thanks for sharing. One of our Gaming Services product managers may reach out to you by DM to find out how you are getting along, your use case and feedback.

    Thanks
     
    unity_Ctri likes this.
  6. BayouSoftware

    BayouSoftware

    Joined:
    Oct 7, 2012
    Posts:
    15
    Hi Im back on this, I've lowered my amount of currencies down to a more managable 15 currencies as advised; however when I get the players balances it still returns the currencies I have since deleted.

    This is leading to a problem with the HasNext flag, as thats set to true but when it calls GetNextAsync it doesnt add any more balances and just gets stuck in the loop until it hits the rate limit.

    Because its only returning the first 20 balances any balance that has a currency ID later in the alphabet than the first 20 is not being fetched.

    Im just using code based on the example code:

    Code (CSharp):
    1.  
    2.  GetBalancesResult playerBalancesResponse = await Economy.PlayerBalances.GetBalancesAsync();          
    3.        while (playerBalancesResponse.HasNext)
    4.         {
    5.             await playerBalancesResponse.GetNextAsync();
    6.         }
    7.  
    Ive definetly published my Dashboard changes to the currencies.

    Sorry if this doesnt make sense, my brain is fried atm.

    tldr: currencies that should have been deleted are bloating the response from GetBalancesAsync preventing me from accessing all of my 15 currencies

    Cheers,
     
    Last edited: Dec 7, 2021
  7. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi again,

    The first thing to point out, is that when you delete a currency from the Economy Dashboard, you are just deleting the currency definition, not the individual instances of the currency attached to each player.

    So, calling GetCurrenciesAsync will return all the currently configured currency definitions. But calling GetBalancesAsync for an individual player will return all their balances, including those for currencies that have since been deleted

    With regards to the 20 item issue. Can you confirm if changing the itemsPerFetch makes a difference and allows you to retrieve the balances of the 15 active currencies as well as any deleted ones?

    There is more information on this, and a note about the deleted currencies on the GetBalancesAsync method here

    I hope that helps.
     
  8. BayouSoftware

    BayouSoftware

    Joined:
    Oct 7, 2012
    Posts:
    15
    Ah thanks, I wondered if that was the case, but got a bit confused as I hadnt given them any of that currency, but of course the initial balance even when set to zero will create an instance of that currency.

    I'll just ignore those player accounts and make some new ones, now that my currency definitions are setup correctly.

    Thanks very much