The creator-centric platform for building live games in Unity

Easily add social, commerce, and content management features to your live game with low-code, drag-and-drop prefabs fully integrated with your Unity workflow.

Adding Currency

Overview

The purpose of this guide is for game makers to add new content of an existing type provided by Beamable.

Here is everything needed to use the Currency in the "Beamable SDK for Unity".

Game Maker User Experience

During development, the game maker's user experience is as follows.

Steps

Follow these steps to get started.

Step 1. Create Currency

Here is the process to add a new object with existing content type of "Currency".

Step

Detail

  1. Open the Content Manager Window

• Unity → Window → Beamable → Open Content Manager

  1. Create the "Currency" content
• Select the content type in the list • Press the "Create" button • Populate the content name
  1. Open the Unity Project Window

• Unity → Window → General→ Project

  1. Select the "Currency" asset

• Search by the name given in step #3
• View the asset in the Unity Inspector Window

  1. Populate all data fields

Note: The icons can be set to any valid Unity’s Addressable object.

  1. Save the Unity Project

• Unity → File → Save Project

Best Practice: If you are working on a team, commit to version control in this step.

  1. Publish the content

• Press the "Publish" button in the Content Manager Window

Step 2. Grant Currency

Now that the currency content is setup, update the active player's currency via InventoryInventory - A collection of Beamable items owned by the active player.

Here the game maker will choose either 2a or 2b.

Step 2a. Grant Currency (Portal)

Step

Detail

  1. Locate a player's DBIDDBID - The database identification. Beamable generates an anonymous account for the player when the project first runs

• See "Verify Success" on Step 1 - Getting Started

  1. Open the "Toolbox" Window

• Unity → Window → Beamable → Open Beamable Toolbox

  1. Open the "Portal" Window

• Click the "Open Portal" Button

  1. Navigate to desired player's Inventory

• Select the active realm
• Select the "Players" tab
• Search by the DBID from step #1
• Press the "Inventory" button
• Press the "Add Currency" button

  1. Add the Currency
• Select the desired currency in the dropdown • Press the "Add" button

Step 2a. Grant Currency (InventoryService)

Name

Detail

  1. Add custom C# code

• See API below for more info

API

Here are API highlights for InventoryUpdateBuilder which builds a list of one or more currency operations to execute, and InventoryService which executes the currency operations.

Note: This Beamable system handles player inventory and also player currency.

Add Currency

public async void AddCurrency()
{
   // Resolve Reference
   var currencyContentPrimary =  await _currencyRefPrimary.Resolve();
   
   // List the operations
   InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
   inventoryUpdateBuilder.CurrencyChange(currencyContentPrimary.Id, +1);

   // Execute the operations
   await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
   {
      Debug.Log($"AddCurrency() success.");
               
   });
}

Remove Currency

public async void RemoveCurrency()
{
   // Resolve Reference
   var currencyContentPrimary =  await _currencyRefPrimary.Resolve();
   
   // List the operations
   InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
   inventoryUpdateBuilder.CurrencyChange(currencyContentPrimary.Id, -1);

   // Execute the operations
   await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
   {
      Debug.Log($"RemoveCurrency() success.");
               
   });
}

Example

📘

Beamable SDK Examples

This and all examples are available for download at GitHub.com/Beamable_SDK_Examples

In the InventoryCurrencyExample.cs, the user can click buttons to add, remove, and trade between available currencies.

Game View

Inspector

The Example in the Unity Inspector WindowThe Example in the Unity Inspector Window

The Example in the Unity Inspector Window

Code

using System.Collections.Generic;
using System.Linq;
using Beamable.Common.Api.Inventory;
using Beamable.Common.Content;
using Beamable.Common.Inventory;
using Beamable.Examples.Shared;
using UnityEngine;
using UnityEngine.Events;

namespace Beamable.Examples.Services.InventoryService.InventoryCurrencyExample
{
   [System.Serializable]
   public class InventoryCurrencyExampleEvent : UnityEvent<InventoryCurrencyExampleData> { }

   /// <summary>
   /// Demonstrates <see cref="InventoryService"/>.
   /// </summary>
   public class InventoryCurrencyExample : MonoBehaviour
   {
      //  Events  ---------------------------------------
      [HideInInspector]
      public InventoryCurrencyExampleEvent OnRefreshed = new InventoryCurrencyExampleEvent();
      
      //  Fields  ---------------------------------------
      [SerializeField] private CurrencyRef _currencyRefPrimary = null;
      [SerializeField] private CurrencyRef _currencyRefSecondary = null;
      
      private IBeamableAPI _beamableAPI;
      private const int CurrencyDeltaPerClick = 1;
      private const string ContentType = "currency";
      private readonly InventoryCurrencyExampleData _inventoryCurrencyExampleData = new InventoryCurrencyExampleData();
         
      //  Unity Methods  --------------------------------
      protected void Start()
      {
         Debug.Log($"Start()");

         SetupBeamable();
      }

      //  Methods  --------------------------------------
      private async void SetupBeamable()
      { 
   
         _beamableAPI = await Beamable.API.Instance;
            
         Debug.Log($"beamableAPI.User.id = {_beamableAPI.User.id}");

         _inventoryCurrencyExampleData.CurrencyContentPrimary = 
            await _currencyRefPrimary.Resolve();
         
         _inventoryCurrencyExampleData.CurrencyContentSecondary = 
            await _currencyRefSecondary.Resolve();

         // All currencies (Available in game)
         _beamableAPI.ContentService.Subscribe(ContentService_OnChanged);
         
         // Filtered currencies (Owned by current player)
         _beamableAPI.InventoryService.Subscribe(ContentType, InventoryService_OnChanged);

      }

     
      public void Refresh()
      {
         string refreshLog = $"Refresh() ...\n" +
                             $"\n * ContentType = {ContentType}" +
                             $"\n * ContentCurrencyNames.Count = {_inventoryCurrencyExampleData.ContentCurrencyNames.Count}" +
                             $"\n * InventoryCurrencyNames.Count = {_inventoryCurrencyExampleData.InventoryCurrencyNames.Count}\n\n";
            
         //Debug.Log(refreshLog);

         OnRefreshed?.Invoke(_inventoryCurrencyExampleData);
      }
      

      public async void AddPrimaryCurrency()
      {
         InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentPrimary.Id, 
            CurrencyDeltaPerClick);

         // Add
         await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
         {
            Debug.Log($"#1. PLAYER AddPrimaryCurrency2() success.");
                     
         });
      }

      
      public async void RemovePrimaryCurrency()
      {
         InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
         
         // Remove
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentPrimary.Id, 
            -CurrencyDeltaPerClick);

         await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
         {
            Debug.Log($"#2. PLAYER RemovePrimaryCurrency() success.");
                     
         });
      }

     
      public async void TradePrimaryToSecondary()
      {
         InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
         
         // Remove Primary
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentPrimary.Id, 
            -CurrencyDeltaPerClick);
         
         // Add Secondary
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentSecondary.Id, 
            CurrencyDeltaPerClick);

         await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
         {
            Debug.Log($"#3. PLAYER TradePrimaryToSecondary() success.");
                     
         });
      }
      
     
      public async void TradeSecondaryToPrimary()
      {
         InventoryUpdateBuilder inventoryUpdateBuilder = new InventoryUpdateBuilder();
         
         // Remove Secondary
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentSecondary.Id, 
            -CurrencyDeltaPerClick);
         
         // Add Primary
         inventoryUpdateBuilder.CurrencyChange(_inventoryCurrencyExampleData.CurrencyContentPrimary.Id, 
            CurrencyDeltaPerClick);

         await _beamableAPI.InventoryService.Update(inventoryUpdateBuilder).Then(obj =>
         {
            Debug.Log($"#4. PLAYER TradeSecondaryToPrimary() success.");
                     
         });
      }
      
      
      public void ResetPlayer()
      {
         ExampleProjectHacks.ClearDeviceUsersAndReloadScene();
         Debug.Log($"#5. ResetPlayer() success.");
      }

      
      //  Event Handlers  -------------------------------
      private void ContentService_OnChanged(ClientManifest clientManifest)
      {
         _inventoryCurrencyExampleData.IsChangedContentService = true;
         
         // Filter to ONLY CurrencyContent
         List<ClientContentInfo> clientContentInfos =  clientManifest.entries.Where((clientContentInfo, i) => 
               ExampleProjectHelper.IsMatchingClientContentInfo(clientContentInfo, ContentType)).ToList();
         
         Debug.Log($"GAME - ContentService_OnChanged, " +
                   $"currencies.Count = {clientContentInfos.Count}");
         
         _inventoryCurrencyExampleData.ContentCurrencyNames.Clear();
         foreach (ClientContentInfo clientContentInfo in clientContentInfos)
         {
            string currencyName = ExampleProjectHelper.GetDisplayNameFromContentId(clientContentInfo.contentId);
            string currencyNameFormatted = $"{currencyName}";
            _inventoryCurrencyExampleData.ContentCurrencyNames.Add(currencyNameFormatted);
         }
         
         // Alphabetize
         _inventoryCurrencyExampleData.ContentCurrencyNames.Sort();
         
         Refresh();
      }
      
     
      private void InventoryService_OnChanged(InventoryView inventoryView)
      {
         _inventoryCurrencyExampleData.IsChangedInventoryService = true;
         
         Debug.Log($"PLAYER - InventoryService_OnChanged, " +
                   $"currencies.Count = {inventoryView.currencies.Count}");

         _inventoryCurrencyExampleData.InventoryCurrencyNames.Clear();
         foreach (KeyValuePair<string, long> kvp in inventoryView.currencies)
         {
            string currencyName = ExampleProjectHelper.GetDisplayNameFromContentId(kvp.Key);
            string currencyNameFormatted = $"{currencyName} x {kvp.Value}";
            _inventoryCurrencyExampleData.InventoryCurrencyNames.Add(currencyNameFormatted);
         }
         
         // Alphabetize
         _inventoryCurrencyExampleData.InventoryCurrencyNames.Sort();
         
         Refresh();
      }
   }
}

Advanced

This section contains any advanced configuration options and workflows.

See Content » Code (Advanced) for more info.

Updated about a month ago


Adding Currency


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.