Player balances
Use the SDK to manage player currency balances.
Read time 3 minutesLast updated a day ago
The
PlayerBalancesGetBalancesAsync
Retrieve the currency balances for the current user. Takes an optionalGetBalancesOptionsGetBalancesOptionsThese methods return a// Optional, defaults to 20GetBalancesOptions options = new GetBalancesOptions{ ItemsPerFetch = 5};GetBalancesResult getBalancesResult = await EconomyService.Instance.PlayerBalances.GetBalancesAsync(options);List<PlayerBalance> firstFiveBalances = getBalancesResult.Balances;// do something with your balancesif (getBalancesResult.HasNext) { getBalancesResult = await getBalancesResult.GetNextAsync(options.ItemsPerFetch); List<PlayerBalance> nextFiveBalances = getBalancesResult.Balances; // do something with your balances}
GetBalancesResultGetBalancesOptions
The options object for aGetBalancesAsync- : An int. Defaults to 20. Use this to set the maximum number of balances to fetch per call between 1 and 100 inclusive.
ItemsPerFetch
GetBalancesResult
AGetBalancesResult- : A
Balanceswith the currently fetched balances.List<PlayerBalance>
- : This method asynchronously fetches more results. It has one optional parameter to limit the amount of results fetched (this can be between 1 and 100 inclusive, default is 20). It will return a new result, which contains both the original items and the newly fetched items in its
GetNextAsync(int itemsToFetch = 20)list.Balances
SetBalanceAsync
Sets the balance of the specified currency to the specified value. This method optionally takes aSetBalancesOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int newAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";SetBalanceOptions options = new SetBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.SetBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.SetBalanceAsync(currencyID, newAmount, options);
SetBalanceOptions
The options object for aSetBalanceAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
IncrementBalanceAsync
Increments the balance of the specified currency by the specified value. This method optionally takes aIncrementBalancesOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int incrementAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";IncrementBalanceOptions options = new IncrementBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.IncrementBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.IncrementBalanceAsync(currencyID, newAmount, options);
IncrementBalanceOptions
The options object for aIncrementBalanceAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
DecrementBalanceAsync
Decrements the balance of the specified currency by the specified value. This method optionally takes aDecrementBalanceOptionswriteLockwriteLockwriteLockstring currencyID = "GOLD_BARS";int decrementAmount = 1000;string writeLock = "someLockValueFromPreviousRequest";DecrementBalanceOptions options = new DecrementBalanceOptions{ WriteLock = writeLock};PlayerBalance newBalance = await EconomyService.Instance.PlayerBalances.DecrementBalanceAsync(currencyID, newAmount);// ORPlayerBalance otherNewBalance = await EconomyService.Instance.PlayerBalances.DecrementBalanceAsync(currencyID, newAmount, options);
DecrementBalanceOptions
The options object for aDecrementBalanceAsync- : A string. Defaults to
WriteLock. Use this to set a write lock for optimistic concurrency. See Write Lock.null
PlayerBalance
A player balance represents a single currency balance for a player. It has the following fields:- : The ID of the currency this balance represents.
CurrencyId - : The integer amount of this currency the player has.
Balance - : The current
WriteLockstring.writeLock - : The date this balance was created. It is an EconomyDate object.
Created - : The date this balance was modified. It is an EconomyDate object.
Modified
GetCurrencyDefinitionAsync
This is a convenience method to get the currency definition for the currency associated with this balance. It returns a CurrencyDefinition.PlayerBalance myPlayerBalance = ... // Get a player balance from one of the above methodsCurrencyDefinition currencyDefForMyPlayerBalance = myPlayerBalance.GetCurrencyDefinitionAsync();
BalanceUpdated
This event can be subscribed to in order to be notified when the SDK updates the balance of a particular currency. The subscriber is passed the currency ID of the balance that was updated.EconomyService.Instance.PlayerBalances.BalanceUpdated += currencyID => { Debug.Log($"The currency that was updated was {currencyID}");};