Skip to content

Conditions

Conditional Logic can be applied to any document. Currently we use it to run GameEvents. Any condition starts with one of 2 logical operators And / Or. Then you can add any other types of conditions. The conditions can be as complex and nested as you want.

  • Dates Range, Day Of The Week, and Time Of The Day are conditions based on a specific time.
  • ABTest Condition checks if the user was assigned to a specific A/B Test and Variant.
  • Active Event triggers only if the specified GameEvent is active right now.
  • Segment Condition checks if the user belongs to a specific Segment.

We'll be adding new Conditions. Please let us know if you need some.

Section for programmers

GameEvent documents take care of the conditional logic, however, if you want to add conditions to your documents, here are what steps you need to do:

  • Let's say you have a Document called MyDocument with a conditional parameter Condition.
  • Use interface for your class IConditionsListener which will be responsible for the logic. Usually, it's some sort of a Manager.
  • Call the following method ConditionsManager.Subscribe(MyDocument.Condition, MyDocument, this, );
  • The default condition value (True or False) is the fourth parameter in the method above. Whenever the condition state changes one of the following methods from the interface IConditionsListener is called on your Manager:

    • public void ConditionPassed(object data);
    • public void ConditionFailed(object data);
  • The data parameter in the methods above is the second parameter you send to the ConditionsManager.Subscribe - it's MyDocument in our example.

public class Manager : IConditionsListener
{
    private void Start()
    {
        foreach (var ev in DataManager.GameEvents)
            ConditionsManager.Subscribe(ev.Condition, ev, this, IsGameEventActive(ev));
    }

    public void ConditionPassed(object data)
    {
        if (data is GameEvent gameEvent)
            StartEvent(gameEvent);
    }

    public void ConditionFailed(object data)
    {
        if (data is GameEvent gameEvent)
            StopEvent(gameEvent);
    }

    ....
    ....
    ....
}

Custom conditions

  1. Create a new Template inherited from CustomCondition to add your condition. For example TestCondition.
  2. Mark it partial to be able to add methods.
  3. After code is generated, add a new class inside of the Balancy folder (due to amsdef file), but outside of the AutoGeneratedCode folder (to avoid data loss after the next code generation). You can make a new folder Assets/Balancy/Custom for example, and put there all your custom scripts.

public partial class TestCondition : SmartObjects.Conditions.Custom
{
    public override bool CanPass()
    {
        //TODO return true or false  
    }

    protected override void SubscribeForPrivateInfo()
    {
        //TODO subscribe for all the required events and call OnUpdate() method to force conditions to recalculate
    }

    protected override void UnsubscribeFromPrivateInfo()
    {
        //TODO clean up all subscriptions
    }
}

Back: Overview