Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Feedback Directly getting a specific resource

Discussion in 'Economy' started by DavidZobrist, May 27, 2022.

  1. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    170
    Hi,

    I seem only find an api call to get all player resources instead of a specific one. Since I do not want to rely on the current order I receive them in and just use the array index, I have to search the array with the "find" function.

    Code (JavaScript):
    1. const playerCurrencies = await currencies.getPlayerCurrencies(projectId, playerId);
    2.   const currentWood = playerCurrencies.data.results.find(x => x.currencyId == 'WOOD').balance;
    3.   const currentLoyality = playerCurrencies.data.results.find(x => x.currencyId == 'LOYALITY').balance;

    Is this the best way to do so or am I missing something?
    I would have expected something like playerCurrencies.data.results['WOOD'];
    This does not work since the id is part of the object instead of beeing a pointer to it.
     
    Last edited: May 27, 2022
  2. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    170
    Code (JavaScript):
    1.      {'LOYALITY': {
    2.       balance: 0,
    3.       writeLock: '1',
    4.       created: { date: '2022-05-26T23:24:47Z' },
    5.       modified: { date: '2022-05-26T23:24:47Z' }
    6.     }}
    7.  
    Something like this, you would lose the explicit stating of currencyId but would gain the ability to directly point to the resource by name.
    But maybe using the index is the better way for performance since its not a string comparison.
    Usability wise I just missed the option to the resource by name via the api.
    Just thoughts..
     
  3. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    170
    Side node:
    I really start to love the unity game services. The amount of complexity economy specially takes away is awesome. Thanks!
     
    SebT_Unity likes this.
  4. DavidZobrist

    DavidZobrist

    Joined:
    Sep 3, 2017
    Posts:
    170
    I found this in client documentation now so I assume it will also work in the cloud code version (js).

    Code (CSharp):
    1. public async Task<CurrencyDefinition> GetCurrencyAsync(string id)
    How ever this would result in multiple calls which isnt really the solution for the usability issue above.
    But a good solution where we only have to check a single resource.