Skip to main content
This topic provides example code for handling an event-based stat change. The examples are C++ code fragments that use the flat C API of the XBOX services API (XSAPI). XSAPI delivers the stat updates through the Real-Time Activity (RTA) service and manages the RTA subscriptions for you.

Before you begin

The code fragments share one scope and assume the following:
  • xboxLiveContext is an XblContextHandle for the signed-in user. To create one, call XblContextCreateHandle.
  • xboxUserId is the uint64_t XBOX user ID (XUID) of the player whose stats you want updates for.
  • scid is a const char* that contains the Service Configuration ID (SCID) for your title.
  • TotalPuzzlesSolved is an example stat name. Replace it with the name of an event-based stat that’s configured for your title.
  • LogToScreen is a printf-style logging function in your app.
To get notifications about event-based stat changes
  1. Add a stat-changed handler.
  2. Track the stats that you want updates for.
To clean up
  1. Stop tracking stats.
  2. Remove a stat-changed handler.

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. If xboxLiveContext or the handler is null, XblUserStatisticsAddStatisticChangedHandler returns 0 and doesn’t register the handler.

Track the stats that you want updates for

Your handler receives updates only for stats that you track. Call XblUserStatisticsTrackStatistics with the users and stat names that you want updates for. You can change the set of tracked stats at any time, independently of your handlers.
XblUserStatisticsSubscribeToStatisticChange and XblUserStatisticsUnsubscribeFromStatisticChange are deprecated. They still work, but they’ll be removed in a future release, and depending on your compiler settings, calls to them can produce deprecation warnings. Use XblUserStatisticsTrackStatistics and XblUserStatisticsStopTrackingStatistics instead.

Stop tracking stats

When you no longer need updates for specific stats, call XblUserStatisticsStopTrackingStatistics with the same users, SCID, and stat names that you tracked.
Alternatively, to stop updates for all the stats that you track for a set of users, call XblUserStatisticsStopTrackingUsers instead of XblUserStatisticsStopTrackingStatistics.
XSAPI counts tracking requests. If you track the same stat for a user more than once, call a stop-tracking function the same number of times to stop the updates.

Remove a stat-changed handler

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

See also

Last modified on October 29, 2026