Adding Stats

Overview

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

Use Cases

  1. Data Store - A simple place to read/write info (Ex. How many characters does the player own?)
  2. Targeting - StatStat - Active data entity used for read/write of player states are the vector for player segmentation (Ex. A/B testing, targeted offer, focused message campaign, announcement for subset of player-base)

Supported Data Types

  • String - For common alphanumeric data storage
  • Numeric - Supports numeric evaluations on the back-end (<, >, =) and a fast, atomic increment API

Public vs Private

  • Public StatStat - Active data entity used for read/write of player state - Visible to owning player and others (Ex. Player Alias, Player Avatar)
  • Private StatStat - Active data entity used for read/write of player state - Visible only to the owning player (Ex. "How many characters do I have?")

📘

Best Practice

These hints make efficent use of concepts and workflows.

Stats are designed for simple data types; string or numeric

  • Do not store C# objects
  • Do not store Json blobs

Steps

Follow these steps to get started.

1. Create Stat Type

Step

Detail

  1. Create the StatStat - Active data entity used for read/write of player state

• Unity → Assets → Create → Beamable → Stat

  1. Name the asset file
  1. Populate all data fields

Best Practice: It is a best practice to match the Stat Key value with the asset file name

  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.

2. Set and Get Stats

There are two main methods for interacting with StatStat - Active data entity used for read/write of player states; 1. Via the StatBehaviour and 2. raw C# coding. Game makers can use either/both methods to meet the needs of the game project.

1. Using StatBehaviour

This method offers higher ease-of-use.

Setup

Unity Hierarchy

Unity Inspector

Code

📘

Beamable SDK Examples

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

using Beamable.Stats;
using UnityEngine;

namespace Beamable.Examples.Services.StatsService
{
   /// <summary>
   /// Demonstrates <see cref="StatsService"/>.
   /// </summary>
   public class StatBehaviourExample : MonoBehaviour
   {
      //  Fields  ---------------------------------------
      [SerializeField]
      private StatBehaviour _myStatBehaviour = null;

      //  Unity Methods  --------------------------------
      protected void Start()
      {
         Debug.Log($"Start()");

         //Async refresh value
         _myStatBehaviour.OnStatReceived.AddListener(MyStatBehaviour_OnStatReceived);

         // Set Value
         _myStatBehaviour.SetCurrentValue("0");
         _myStatBehaviour.SetCurrentValue("1");

         // Get Value
         Debug.Log($"_statsBehaviour.value = {_myStatBehaviour.Value}");
      }

      //  Event Handlers  -------------------------------
      private void MyStatBehaviour_OnStatReceived(string value)
      {
         // Observe Value Change
         Debug.Log($"MyStatBehaviour_OnStatReceived() value = {_myStatBehaviour.Value}");
      }
   }
}

2. Using Raw C# Coding

This method offers higher flexibility.

Setup

Unity Hierarchy

Unity Inspector

Code

📘

Beamable SDK Examples

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

using System.Collections.Generic;
using UnityEngine;

namespace Beamable.Examples.Services.StatsService
{
    /// <summary>
    /// Demonstrates <see cref="StatsService"/>.
    /// </summary>
    public class StatCodingExample : MonoBehaviour
    {
        //  Unity Methods  --------------------------------
        protected void Start()
        {
            Debug.Log($"Start()");

            SetupBeamable();
        }

        //  Other Methods   ------------------------------
        private async void SetupBeamable()
        {
            var beamableAPI = await Beamable.API.Instance;

            Debug.Log($"beamableAPI.User.id = {beamableAPI.User.id}");
            
            string statKey = "MyExampleStat";
            string access = "public";
            string domain = "client";
            string type = "player";
            long id = beamableAPI.User.id;

            // Set Value
            Dictionary<string, string> setStats =
                new Dictionary<string, string>() { { statKey, "99" } };

            await beamableAPI.Stats.SetStats(access, setStats);

            // Get Value
            Dictionary<string, string> getStats = 
                await beamableAPI.Stats.GetStats(domain, access, type, id);

            string myExampleStatValue = "";
            getStats.TryGetValue(statKey, out myExampleStatValue);
         
            Debug.Log($"myExampleStatValue = {myExampleStatValue}");
        }
    }
}

Advanced

This section contains any advanced configuration options and workflows.

Analytics Events & Stats

Beamable supports both analytics events and stats.

While the use cases for analytics events and stats are often different, there are indeed common workflows where both are used in concert. For example. Beamable automatically tracks an analytics event (deep history) every time a StatStat - Active data entity used for read/write of player state is changed (fast speed). This is the best of both worlds!

See Analytics for more info.


Did this page help you?