All Collections
Portal Pages
Design | Gamification | Statistic Events
Design | Gamification | Statistic Events

Trigger custom events that add user currencies and stats

Paul Winterhalder avatar
Written by Paul Winterhalder
Updated over a week ago

Design

Stats Events are a powerful design-level abstraction for manipulating brainCloud Player Statistics. Think of them as macros, which when triggered, cause changes to multiple player stats to be triggered at once. Employing Stats Events in your application allows your coders to work at a higher level, triggering stats events, while your designer is free to determine and tune the individual stats themselves.

For example, a Stat Event that triggers upon vanquishing a Boss NPC could: 

  • Award Player XP

  • Award currency

  • Increment a Boss Vanquish stat for rank on a Leaderboard

  • Award an Achievement

Implementation

To create a quest, go to the Statistics Events page on the dashboard.

Creating a statistic event

  • Click the [Add Item] button with the plus icon.

  • Enter an Event Name and Description. You can use the TriggerStatsEvent API call to call the stat event via its event name. A stat event can be called multiple times.

  • Enter an Achievement the Stat event rewards.

  • Enter any XP, Currencies, User and Global Stats the stat event rewards. Given a stat even can be reward multiple times, you can reward the same stat event with a multiplier when making the API call. ex. a stat that awarded 10 XP and 100 coins, would award 50 XP and 500 coins, with a times five multiplier.

  • [Save] your new User Stat Event!

Writing the code

Now that you created statistic events, you will want to trigger them for your players.

In your client code

  • Add the brainCloud client to your app.

  • Pair the client with the dashboard.

  • Trigger your stat event with TriggerStatsEvent call.

  • Notify the player of any important rewards that the stat trigger event responded with.

private BrainCloudWrapper _bc;
private string _wrapperName = "default";
void Start() {
    // Unity
    GameObject go = new GameObject();
    _bc = go.AddComponent<BrainCloudWrapper>();
    _bc.WrapperName = _wrapperName; // optionally set a wrapper-name
    _bc.Init(); // extra data, such as: _appId, _secret and _appVersion, is taken from the brainCloud Unity Plugin.
    DontDestroyOnLoad(go); // keep the brainCloud game object through scene changes
 
   
    _bc.AuthenticateAnonymous((response, cbObject) => { TriggerStatEvent(); });
}
void TriggerStatEvent()
{
    var eventName = "loot_pack_small_found";
    var eventMultiplier = 3;
   
    _bc.PlayerStatisticsEventService.TriggerStatsEvent(eventName, eventMultiplier, (response, cbObject) =>
    {
        var jsonMessage =
            (Dictionary<string, object>) BrainCloud.JsonFx.Json.JsonReader.Deserialize(response);
        var jsonData = (Dictionary<string, object>) jsonMessage["data"];
        var rewards = (Dictionary<string, object>) jsonData["rewards"];
       
        var playerAchievements = (string[]) rewards["playerAchievements"];
        foreach (var achievement in playerAchievements)
        {
            // You earned the loot achievement!
            Debug.Log("You earned the " + achievement + " achievement!");
        }
        // You earned 30 XP!
        Debug.Log("You earned " + rewards["experiencePoints"] + " XP!");
       
        var currency = (Dictionary<string, object>) rewards["currency"];
        foreach (var reward in currency)
        {
            // You earned 750 medals!
            Debug.Log("You earned " + reward.Value + " " + reward.Key + "!");
        }
    });  
}

Did this answer your question?