Handling an event-based stat change

This topic provides example code for handling an event-based stat change.

To get notifications about event-based stat changes

  1. Add a Stat-changed handler
  2. Subscribe to a specific stat change

To clean up

  1. Remove a Stat-changed handler
  2. Unsubscribe from a specific stat change

Add a Stat-changed handler

Register an event handler for statistic change notifications by using the following example code. The event handler receives an XblStatisticChangeEventArgs object.

Flat C API

void* context{ nullptr };
XblFunctionContext statisticChangedFunctionContext = XblUserStatisticsAddStatisticChangedHandler(
    xboxLiveContext,
    [](XblStatisticChangeEventArgs eventArgs, void* context) 
    {
        // Handle stat change. 
        LogToScreen("Statistic changed callback: stat changed (%s = %s)",
            eventArgs.latestStatistic.statisticName, 
            eventArgs.latestStatistic.value); 
    },
    context
);

For more information, see the following:

Subscribe to a specific stat change

Subscribe to statistic update notifications via the XblUserStatisticsSubscribeToStatisticChange handler by using the following example code.

Flat C API

XblRealTimeActivitySubscriptionHandle subscriptionHandle{ nullptr };

HRESULT hr = XblUserStatisticsSubscribeToStatisticChange(
    xboxLiveContext,
    xboxUserId,
    scid,
    statisticName.c_str(),
    &subscriptionHandle
);

Remove a Stat-changed handler

Remove an event handler for statistic change notifications by using the following example code.

Flat C API

XblUserStatisticsRemoveStatisticChangedHandler(
        xboxLiveContext,
        statisticChangedFunctionContext
);

Unsubscribe from a specific stat change

Unsubscribe a previously created statistic change subscription by using the following example code.

Flat C API

hr = XblUserStatisticsUnsubscribeFromStatisticChange(
    xboxLiveContext,
    statisticChangeSubscriptionHandle
);