Skip to content

Template: Wheel Of Fortune

Screenshot

The Wheel of Fortune mechanic in games involves a spinning wheel divided into different sections, each representing a prize, reward, or action. Players spin the wheel, and the section the wheel lands on determines what the player receives or has to do next. This mechanic is often used in games as a way to distribute random rewards and can add an element of luck and excitement to the gameplay.

Wheels

The Wheel Of Fortune template allows you to create multiple configs. You can provide different rules for each of the configs. The configs are called Documents in Balancy. One of the parameters in Wheel of Fortune is Condition. We are going to use it to give different configs for players, depending on their level, but you can come up with any other segmentation.

Screenshot

Each document also has a parameter Drop, where you can specify the content of the wheel with all the chances. We are using Weight instead of chance because it's easier to work with. Meanwhile, engineers can always normalize weights to convert them to the chances. This is exactly what we are going to do in the code below.

Section for programmers

Extension for WheelOfFortune

using Balancy.Models.SmartObjects;

namespace Balancy.Models
{
    public static class WheelOfFortuneExtension
    {
        public static ItemWithAmount DropRandomItem(this WheelOfFortune wheel)
        {
            var allWeight = wheel.CalculateTotalDropWeight();

            var random = Balancy.Random.Range(0, allWeight);
            foreach (var dropInfo in wheel.Drop)
            {
                if (random < dropInfo.Weight)
                    return dropInfo.Item;

                random -= dropInfo.Weight;
            }

            return null;
        }

        private static int CalculateTotalDropWeight(this WheelOfFortune wheel)
        {
            var allWeight = 0;
            foreach (var dropInfo in wheel.Drop)
                allWeight += dropInfo.Weight;

            return allWeight;
        }
    }
}

Example

using System.Collections.Generic;
using Balancy;
using Balancy.Models;
using Balancy.Models.SmartObjects;
using Balancy.Models.SmartObjects.Conditions;
using Balancy.SmartObjects;

public class WheelExample : IConditionsListener
{
    private List<WheelOfFortune> _availableDailyBonuses;
    private WheelOfFortune _activeWheelOfFortune;//TODO store this value in profile

    public WheelExample()
    {
        Init();
    }

    private void Init()
    {
        _availableDailyBonuses = new List<WheelOfFortune>();

        foreach (var wheel in DataEditor.WheelOfFortunes)
            LiveOps.General.ConditionSubscribe(wheel.Condition, wheel, this, _activeWheelOfFortune == wheel ? PassType.True : PassType.False);
    }

    public void Clear()
    {
        _availableDailyBonuses.Clear();
        _availableDailyBonuses = null;
        _activeWheelOfFortune = null;
    }

    public void ConditionPassed(object data)
    {
        var bonus = data as WheelOfFortune;
        _availableDailyBonuses.Add(bonus);
        CheckBonus();
    }

    public void ConditionFailed(object data)
    {
        var bonus = data as WheelOfFortune;
        _availableDailyBonuses.Remove(bonus);

        if (_activeWheelOfFortune == bonus)
        {
            _activeWheelOfFortune = null;
            CheckBonus();
        }
    }

    private void CheckBonus()
    {
        var bonus = _availableDailyBonuses.Count > 0 ? _availableDailyBonuses[0] : null;
        if (bonus != null && _activeWheelOfFortune == null)
            _activeWheelOfFortune = bonus;
    }

    public WheelOfFortune GetActiveWheel()
    {
        return _activeWheelOfFortune;
    }

    public ItemWithAmount Spin()
    {
        return _activeWheelOfFortune?.DropRandomItem();
    }
}

What's next

  1. Import the Template.
  2. Deploy the changes.
  3. Open the Unity project and generate the code.
  4. Add the files to your project, written above.
  5. Initialize Balancy.
  6. Add the following code in the callback.
var wheel = new WheelExample();
for (int i = 0; i < 10; i++)
{
    var reward = wheel.Spin();
    Debug.Log($"{i}) REWARD = {wheel.GetActiveWheel()?.Name} : {reward?.Item.Name} = {reward?.Count}");
}