Stats - Guide

Store pieces of data about your players [PROF-Stats-02]

Follow these steps to get started:

1. Create Stat Type

StepDetail
1. Create the Stat

• Unity → Assets → Create → Beamable → Stat
2. Name the asset file
3. Populate all data fields

Best Practice: It is a best practice to match the Stat Key value with the asset file name
4. 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 Stats; 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 HierarchyUnity Inspector

Code

📘

Beamable SDK Examples

The following example code is 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 HierarchyUnity Inspector

Code

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 context = BeamContext.Default;
            await context.OnReady;

            Debug.Log($"context.PlayerId = {context.PlayerId}");

            string statKey = "MyExampleStat";
            string access = "public";
            string domain = "client";
            string type = "player";
            long id = context.PlayerId;

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

            await context.Api.StatsService.SetStats(access, setStats);

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

            string myExampleStatValue = "";
            getStats.TryGetValue(statKey, out myExampleStatValue);

            Debug.Log($"myExampleStatValue = {myExampleStatValue}");
        }
    }
}